解決使用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)的路徑出錯,隨機(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)資源的路徑 **表示可以跨文件夾


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)紅的地方也進(jìn)行了放行,可是依然失敗。
到目前為止,應(yīng)該是已經(jīng)沒問題了,畢竟兩個方法中都進(jìn)行了放行,可是靜態(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 注釋掉,最終問題解決~
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
mybatis如何設(shè)置useGeneratedKeys=true
這篇文章主要介紹了mybatis如何設(shè)置useGeneratedKeys=true,具有很好的參考價值,希望對大家有所幫助。2022-01-01
springboot 返回json格式數(shù)據(jù)時間格式配置方式
這篇文章主要介紹了springboot 返回json格式數(shù)據(jù)時間格式配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
Java 是如何利用接口避免函數(shù)回調(diào)的方法
本篇文章主要介紹了Java 是如何利用接口避免函數(shù)回調(diào)的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
微服務(wù)中使用Maven BOM來管理你的版本依賴詳解
這篇文章主要介紹了微服務(wù)中使用Maven BOM來管理你的版本依賴,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Spring AOP結(jié)合注解實現(xiàn)接口層操作日志記錄
在項目開發(fā)中我們需要記錄接口的操作日志:包含請求參數(shù)、響應(yīng)參數(shù)、接口所屬模塊、接口功能描述、請求地址、ip地址等信息;實現(xiàn)思路很簡單就是基于注解和aop的方式去記錄日志,主要的難點在于日志表結(jié)構(gòu)、注解的設(shè)計已經(jīng)aop實現(xiàn)的一些比較好的實現(xiàn)方式的借鑒2022-08-08
Java Socket編程實現(xiàn)簡單的問候服務(wù)
這篇文章主要為大家介紹了Java Socket編程實現(xiàn)簡單的問候服務(wù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-01-01

