当前位置:网站首页>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!
边栏推荐
- 算力顶天地,存力纳乾坤:国家超级计算济南中心的一体两面
- 【历史上的今天】7 月 29 日:Win10 七周年;微软和雅虎的搜索协议;微软发行 NT 4.0
- 已经删除了的SQL节点,有没有办法恢复
- 【斜率优化】$\text{Sol. LuoguP5504}$ 柠檬
- ARTS-第-25-期
- Security整各Gateway后配置不生效?
- The problem that crontab executes scheduled tasks and reports errors
- Dialogue with Academician Yu Fei of the Canadian Academy of Engineering: Looking for "Shannon's Theorem" in the field of AI
- KubeMeet 报名 | 「边缘原生」线上技术沙龙完整议程公布!
- P4775 [NOI2018] 情报中心(线段树合并)
猜你喜欢

UG安装出现Server Start Failed. The Server May Already Be Running!!解决方法

Virtink:更轻量的 Kubernetes 原生虚拟化管理引擎

In the Chinese ToB market, it is too difficult to choose the right supplier

如何实时计算日累计逐单资金流

Network Effects in Web3

Analyst: Baidu could become China's most valuable company by 2030

The service failure agent how to get things

本周投融报:CeFi积聚风投吸引力

支持向量机SVM

redis学习三redis里的list、set、hash、sorted_set、skiplist
随机推荐
centos7服务器安全策略
5年迭代5次,抖音推荐系统演进历程
字节跳动基于 Iceberg 的海量特征存储实践
The service failure agent how to get things
国产钡铼分布式IO模块如何与西门子PLC Profinet通讯
macro definition small method
华东师范大学副校长周傲英:数据赋能,从数据库到数据中台
不惧AMD及Arm挑战,英特尔称霸服务器市场的秘诀是什么?
LL(1),LR(0),SLR(1),LALR(1),LR(1)对比与分析
Xatlas source code parsing (7)
go的堆内存结构分析
【QT】Qt获取前几天/后几天的时间
593. 有效的正方形 改善丑陋的代码
P4775 [NOI2018] 情报中心(线段树合并)
String类型_static成员_动态内存分配_拷贝构造函数_const关键字_友元函数与友元类
62页智慧冷链产业园整体解决方案2022
【码蹄集新手村600题】pow()函数详解
Thread、Runnable、Callable、Future ... 的关系?
[Operation and maintenance] ssh tunneling relies on the 22 port of ssh to realize the interface service of accessing the remote server
scroll bar style