溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何解決使用security和靜態(tài)資源被攔截的問題

發(fā)布時間:2021-08-26 13:55:14 來源:億速云 閱讀:171 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下如何解決使用security和靜態(tài)資源被攔截的問題,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

使用security和靜態(tài)資源被攔截

之前的博客中我給過如何在springboot中整合security,當(dāng)時寫的界面很簡單,沒有CSS樣式,更談不上靜態(tài)資源,而現(xiàn)在在實際開發(fā)過程中經(jīng)理讓我們用security來開發(fā),界面肯定不可能就是兩個輸入框,一個按鈕就完事啊,當(dāng)加上CSS樣式的時候問題就來了。

首先是CSS樣式?jīng)]辦法被加載,其次登錄之后跳轉(zhuǎn)的路徑出錯,隨機跳轉(zhuǎn)到一個CSS文件中,這讓我很煩惱,查了很多資料,也問了很多前輩之后終于解決了這個問題。

解決方法

下面我給出具體的代碼

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
private UserDetailsServiceImpl uds;
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
    .antMatchers("/login","/css/**","/image/*").permitAll()
    .anyRequest().authenticated().and().formLogin()
            .loginPage("/login").defaultSuccessUrl("/index").permitAll().and().logout().permitAll();
}
public void configure(AuthenticationManagerBuilder auth) throws Exception {     
    auth.userDetailsService(uds);
}
}

上面給出了不被攔截的一些靜態(tài)資源的路徑 **表示可以跨文件夾

如何解決使用security和靜態(tài)資源被攔截的問題

如何解決使用security和靜態(tài)資源被攔截的問題

Spring Security踩坑記錄(靜態(tài)資源放行異常)

問題描述

今天使用springboot整合springsecurity,出現(xiàn)靜態(tài)資源404的狀態(tài)

解決

1.首先嘗試使用網(wǎng)上的方法繼承 WebSecurityConfigurerAdapter

然后重寫public void configure(WebSecurity web)

@Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers(loadExcludePath());
    } 
    private String[] loadExcludePath() {
        return new String[]{
                "/",
                "/static/**",
                "/templates/**",
                "/img/**",
                "/js/**",
                "/css/**",
                "/lib/**"
        };
    }

照道理說。這應(yīng)該就可以了,然而我這里就是不能成功放行

2.于是我又重寫了方法 protected void configure(HttpSecurity http)
@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                //關(guān)閉跨域限制
                .csrf().disable()
                .authorizeRequests()
                 //在此處放行
                .antMatchers(loadExcludePath()).permitAll()
                .anyRequest().authenticated()//其他的路徑都是登錄后即可訪問
                .and()
                .formLogin()
//                .loginPage("/login")
//                .loginProcessingUrl("/doLogin")
                .successHandler(getAuthenticationSuccessHandler())
                .failureHandler(getAuthenticationFailureHandler())
//                .permitAll()
 
                .and()
                .logout()
                .permitAll()
 
                .and()
                .exceptionHandling().accessDeniedHandler(getAccessDeniedHandler());
    }

這里的重點是下面幾句(其他的配置可以忽略)

http
//關(guān)閉跨域限制
.csrf().disable()
.authorizeRequests()
//在此處放行
.antMatchers(loadExcludePath()).permitAll()
.anyRequest().authenticated()//其他的路徑都是登錄后即可訪問

然而盡管標(biāo)紅的地方也進行了放行,可是依然失敗。

到目前為止,應(yīng)該是已經(jīng)沒問題了,畢竟兩個方法中都進行了放行,可是靜態(tài)資源依舊404

3.最終發(fā)現(xiàn)是跨域配置和springsecurity產(chǎn)生了沖突

也就是我項目中在其他位置配置了跨域的內(nèi)容,如下

@Configuration
public class CORSConfiguration extends WebMvcConfigurationSupport { 
    @Override
    protected void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "HEAD", "POST","PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .exposedHeaders(
                        "access-control-allow-headers",
                        "access-control-allow-methods",
                        "access-control-allow-origin",
                        "access-control-max-age",
                        "X-Frame-Options")
                .allowCredentials(true)
                .maxAge(3600);
        super.addCorsMappings(registry);
    }
}

把 CORSConfiguration 注釋掉,最終問題解決~

以上是“如何解決使用security和靜態(tài)資源被攔截的問題”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI