亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在Spring Boot和Spring Security應用程序中提供靜態Web資源

在Spring Boot和Spring Security應用程序中提供靜態Web資源

千巷貓影 2019-09-21 14:49:08
我正在嘗試開發Spring Boot Web應用程序并使用Spring Security Java配置對其進行保護。按照Spring博客中的建議,將我的靜態Web資源放置在“ src / main / resources / public ” 中后,我就可以獲得靜態資源。即https://localhost/test.html在瀏覽器中點擊確實提供html內容。問題在啟用Spring Security之后,訪問靜態資源URL需要身份驗證。我的相關的Spring Security Java配置看起來像這樣:@Override    protected void configure(HttpSecurity http) throws Exception {        // @formatter:off        http.            authorizeRequests()                .antMatchers("/","/public/**", "/resources/**","/resources/public/**")                    .permitAll()                .antMatchers("/google_oauth2_login").anonymous()                    .anyRequest().authenticated()                .and()                .formLogin()                    .loginPage("/")                    .loginProcessingUrl("/login")                    .defaultSuccessUrl("/home")                    .and()                    .csrf().disable()                    .logout()                        .logoutSuccessUrl("/")                        .logoutUrl("/logout") // POST only                .and()                    .requiresChannel()                    .anyRequest().requiresSecure()                .and()                    .addFilterAfter(oAuth2ClientContextFilter(),ExceptionTranslationFilter.class)                    .addFilterAfter(googleOAuth2Filter(),OAuth2ClientContextFilter.class)                .userDetailsService(userService);        // @formatter:on    }我應該如何配置antMatchers以允許將靜態資源放在src / main / resources / public中?
查看完整描述

3 回答

?
斯蒂芬大帝

TA貢獻1827條經驗 獲得超8個贊

有幾件事要注意:


Ant匹配器將根據請求路徑而不是文件系統上資源的路徑進行匹配。

放置的資源src/main/resources/public將從您的應用程序的根目錄開始。例如src/main/resources/public/hello.jpg將從http://localhost:8080/hello.jpg

這就是為什么您當前的匹配器配置不允許訪問靜態資源的原因。為了/resources/**工作,您必須將資源放入src/main/resources/public/resources并在處進行訪問http://localhost:8080/resources/your-resource。


使用Spring Boot時,您可能需要考慮使用其默認值,而不是添加額外的配置。春天開機后,默認,允許訪問/css/**,/js/**,/images/**,和/**/favicon.ico。例如,您可以擁有一個名為的文件src/main/resources/public/images/hello.jpg,而無需添加任何額外的配置,http://localhost:8080/images/hello.jpg無需登錄即可訪問該文件。您可以在允許對Bootstrap CSS文件進行訪問的Web方法安全性示例中看到此操作。無需任何特殊配置。


查看完整回答
反對 回復 2019-09-21
?
莫回無

TA貢獻1865條經驗 獲得超7個贊

 @Override

      public void configure(WebSecurity web) throws Exception {

        web

          .ignoring()

             .antMatchers("/resources/**"); // #3

      }

忽略任何以“ / resources /”開頭的請求。這與使用XML名稱空間配置時配置http @ security = none相似。


查看完整回答
反對 回復 2019-09-21
  • 3 回答
  • 0 關注
  • 910 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號