spring boot security設(shè)置忽略地址不生效的解決
spring boot security設(shè)置忽略地址不生效
最近在試下微服務(wù)改造,出現(xiàn)這樣一個(gè)問(wèn)題所有請(qǐng)求都經(jīng)過(guò)spring cloud gateway進(jìn)行認(rèn)證授權(quán)后再訪問(wèn)后端數(shù)據(jù)方服務(wù),但有些需要合作機(jī)構(gòu)回調(diào),由于進(jìn)行了security認(rèn)證,最終的方案是對(duì)回調(diào)地址進(jìn)行忽略auth認(rèn)證。
最終security主要代碼如下:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/v1/prNotifyBack");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
/**表示所有的訪問(wèn)都必須進(jìn)行認(rèn)證處理后才可以正常進(jìn)行*/
http.httpBasic().and().authorizeRequests().anyRequest().fullyAuthenticated();
/**所有的Rest服務(wù)一定要設(shè)置為無(wú)狀態(tài),以提升操作性能*/
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.csrf().disable();
}
}
這個(gè)過(guò)程遇到了幾個(gè)問(wèn)題:
1、繼承WebSecurityConfigurerAdapter
后我們重寫configure方法,這個(gè)方法需要注意:他有兩個(gè)不同的參數(shù)。
HttpSecurity 及WebSecurity 作用是不一樣的,WebSecurity 主要針對(duì)的全局的忽略規(guī)則,HttpSecurity主要是權(quán)限控制規(guī)則。
所以一開(kāi)始用HttpSecurity是達(dá)不到忽略地址的目的。
protected void configure(HttpSecurity http){.......}
public void configure(WebSecurity web) {.........}
WebSecurity
全局請(qǐng)求忽略規(guī)則配置(比如說(shuō)靜態(tài)文件,比如說(shuō)注冊(cè)頁(yè)面)、全局HttpFirewall配置、是否debug配置、全局SecurityFilterChain配置、privilegeEvaluator、expressionHandler、securityInterceptor、
HttpSecurity
具體的權(quán)限控制規(guī)則配置。
2、忽略不生效問(wèn)題
web.ignoring().antMatchers("/pr/v1/prNotifyBack");
如上代碼如果帶上/pr就不會(huì)生效,訪問(wèn)依然會(huì)出現(xiàn)401錯(cuò)誤。/pr是配置的項(xiàng)目路徑。但帶上項(xiàng)目路徑就不生效,這個(gè)問(wèn)題很疑惑。
server: port: 8089 servlet: context-path: /pr
SpringBoot SpringSecurity, web.ignore失效
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class CustomSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.csrf().disable()
.authorizeRequests()
.antMatchers("/api/**").authenticated()
.and()
.addFilterBefore(new TokenFilter(), UsernamePasswordAuthenticationFilter.class);
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring()
.antMatchers("/")
.antMatchers("/swagger-ui.html")
.antMatchers("/swagger-resources/**")
.antMatchers("/webjars/springfox-swagger-ui/**")
.antMatchers("/v2/api-docs/**");
}
}
這是修改后正常工作的配置文件
之前使用@component注解, 然后使用@Resource注入進(jìn)來(lái).
導(dǎo)致過(guò)濾器全局生效.
正常配置,應(yīng)該手動(dòng)new, 而且過(guò)濾器類不能加@Component注解
具體原因,之后有空研究一下.
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于Java 中的 Lambda 表達(dá)式
這篇文章主要介紹了關(guān)于Java 中的 Lambda 表達(dá)式,Lambda 表達(dá)式是 Java 涉足函數(shù)式編程的過(guò)程。它接受參數(shù)并將其應(yīng)用于表達(dá)式或代碼塊,下面一起進(jìn)入文章查看詳細(xì)內(nèi)容2021-11-11
SSM框架下實(shí)現(xiàn)登錄注冊(cè)的示例代碼
這篇文章主要介紹了SSM框架下實(shí)現(xiàn)登錄注冊(cè)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Springboot設(shè)置默認(rèn)訪問(wèn)路徑方法實(shí)現(xiàn)
這篇文章主要介紹了Springboot設(shè)置默認(rèn)訪問(wèn)路徑方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
SpringBoot配置線程池的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot配置線程池的實(shí)現(xiàn)示例,主要包括在Spring Boot中創(chuàng)建和配置線程池,包括設(shè)置線程池的大小、隊(duì)列容量、線程名稱等參數(shù),感興趣的可以了解一下2023-09-09

