亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2021年08月26日 11:49:01   作者:malachi95  
這篇文章主要介紹了解決使用security和靜態(tài)資源被攔截的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

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

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

首先是CSS樣式?jīng)]辦法被加載,其次登錄之后跳轉(zhuǎn)的路徑出錯,隨機(jī)跳轉(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)資源的路徑 **表示可以跨文件夾

這是頁面靜態(tài)資源路徑

這是我的目錄結(jié)構(gòu)

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());
    }

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

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

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

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

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

也就是我項(xiàng)目中在其他位置配置了跨域的內(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 注釋掉,最終問題解決~

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論