当前位置:网站首页>Security whole configuration does not take effect after the Gateway?
Security whole configuration does not take effect after the Gateway?
2022-07-29 19:04:00 【Low-level code migration engineer】
灵异事件?我的SecurityWhy does the configuration not take effect??Why do I keep jumping to the login page??
因为工作需要,项目中需要引入Spring Security这个安全框架(只是单纯的引入),It seemed so simple at first,After the introduction, put it on the startup classSecurityIs it okay to exclude the automatic configuration class??
说干就干,引入了Security依赖之后,我在@SpringBootApplication注解的参数exclude中将SecurityAutoConfigurationThis autoconfiguration class excludes,If the dependency of health check is introduced into the project, it is also necessary to addManagementWebSecurityAutoConfigurationThis autoconfiguration class excludes,因为在ManagementWebSecurityAutoConfigurationIn this automatic configuration class, he will turn onSecurity的自动配置.
弄完了,就这么简单!打包,发布,测试,没问题.
紧接着,It's time to add the gateway serviceSecurity依赖,有了前面的经验,CVDafa in one go,Then pack、发布、测试,漂亮,Like to ask to work overtime!!it actually let me log in?Why is it different from what I expected?
Then analyze the problem,Securityis a filter chain consisting of a set of filters,By default it will store the successful authentication information toSession对象中,next time according toSessionWhether there is personal information in the to determine whether you have been authenticated,I see it in other services as I did earlierSpringis not in the containerspringSecurityFilterChain这个对象的,But there is still in my gateway service!That road won't work,转换思维,Disable this authentication filter?How to let him fail?放行!!
Let all resources be released!
说干就干,于是就写了一个Security的配置类!
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig2 extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/**")
.permitAll()
.anyRequest()
.authenticated()
.and()
.csrf()
.disable()
.cors();
}
}
写完,心里美滋滋,Thinking about getting to work,打包、发布、测试,what?why do you want me to log in?Is there a problem with my packing posture??I repack、发布、测试,still want me to log in?I have a problem with this configuration?那行,I don't let all requests directly through the filter,I rewrote the config again!
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig2 extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/**")
.permitAll()
.anyRequest()
.authenticated()
.and()
.csrf()
.disable()
.cors();
}
}
又一次打包、发布、测试,Found the problem but still can't solve it!I'm in a hurry,It seems hopeless to fight the king tonight!
So I suddenly remembered that this is a gateway service!!Gateway跟SecuritySeems a bit incompatible!I didn't remember to think of it,在引入Gatewaywhen we need to excludespring-boot-starter-web这个依赖,because one of them iswebflux,一个是mvc,Thinking of this at this time,Shouldn't the configuration be different??Then read the official introduction,It turns out that there are other configuration methods!
So I re-wrote a configuration class
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebFluxSecurity
public class SecurityConfig {
@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity httpSecurity){
httpSecurity
.authorizeExchange()
.pathMatchers("/**").permitAll()
.pathMatchers(HttpMethod.OPTIONS).permitAll()
.anyExchange().authenticated()
.and()
.csrf()
.disable()
.cors();
return httpSecurity.build();
}
}
打包、发布、测试,OK,问题解决!下班了!
It's a bit miserable to be trapped by this problem!
边栏推荐
猜你喜欢

5年迭代5次,抖音推荐系统演进历程

xatlas源码解析(七)

实现get/post请求调用第三方接口

【深度学习】使用yolov5对数据进行预标注

MySQL 中的反斜杠 \\,真是太坑了

【码蹄集新手村600题】pow()函数详解

工业软件上云的矛与盾
![[Deep Learning] Use yolov5 to pre-label data](/img/8d/ca1802131da6d2cba437d05e9c288c.png)
[Deep Learning] Use yolov5 to pre-label data

倒计时1天! | 明日9点,这场精彩的Web3盛宴不容错过

One's deceased father grind English vocabulary training camp Day 17 】 -- espresso, ultimate, gradually, detect, dimension
随机推荐
一文了解信创背景下 SAN 存储转型路线
sticky定位后设置right:0不生效
[Operation and maintenance] ssh tunneling relies on the 22 port of ssh to realize the interface service of accessing the remote server
62页智慧冷链产业园整体解决方案2022
招聘|字节跳动云原生计算,期待你的加入
【WSL】wsl pip NewConnectionError
【历史上的今天】7 月 29 日:Win10 七周年;微软和雅虎的搜索协议;微软发行 NT 4.0
Mathematical Analysis_Proof_Two Important Limits (Tongji Version)
StarRocks 2.3 新版本特性介绍
闻泰科技携手格力集团收购欧菲光旗下广州得尔塔等资产
[Code Hoof Set Novice Village 600 Questions] Given an integer n, find all the values of x and y in floor(n/x)=y
整体系统地认识网络安全与发展
2022开放原子全球开源峰会OpenAnolis分论坛圆满落幕
白宫将举办半导体产业链CEO峰会,台积电、三星、格芯、英特尔等将参与
疫情驱动医疗数字化新业态,ActiveReports强力助推医院LIMS系统升级换代!
5年迭代5次,抖音推荐系统演进历程
请问一下,我这个调度任务显示执行成功,但是并没有成功,我这个任务是odps sql ins
One's deceased father grind English vocabulary training camp Day 】 16 - bankrupt, remain, regulate, the construct and reflect
KubeMeet 报名 | 「边缘原生」线上技术沙龙完整议程公布!
KubeZoo:字节跳动轻量级多租户开源解决方案