spring boot security设置忽略地址不生效的解决
作者:编码是个技术活 时间:2022-06-07 16:37:30
spring boot security设置忽略地址不生效
最近在试下微服务改造,出现这样一个问题所有请求都经过spring cloud gateway进行认证授权后再访问后端数据方服务,但有些需要合作机构回调,由于进行了security认证,最终的方案是对回调地址进行忽略auth认证。
最终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 {
/**表示所有的访问都必须进行认证处理后才可以正常进行*/
http.httpBasic().and().authorizeRequests().anyRequest().fullyAuthenticated();
/**所有的Rest服务一定要设置为无状态,以提升操作性能*/
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.csrf().disable();
}
}
这个过程遇到了几个问题:
1、继承WebSecurityConfigurerAdapter
后我们重写configure方法,这个方法需要注意:他有两个不同的参数。
HttpSecurity 及WebSecurity 作用是不一样的,WebSecurity 主要针对的全局的忽略规则,HttpSecurity主要是权限控制规则。
所以一开始用HttpSecurity是达不到忽略地址的目的。
protected void configure(HttpSecurity http){.......}
public void configure(WebSecurity web) {.........}
WebSecurity
全局请求忽略规则配置(比如说静态文件,比如说注册页面)、全局HttpFirewall配置、是否debug配置、全局SecurityFilterChain配置、privilegeEvaluator、expressionHandler、securityInterceptor、
HttpSecurity
具体的权限控制规则配置。
2、忽略不生效问题
web.ignoring().antMatchers("/pr/v1/prNotifyBack");
如上代码如果带上/pr就不会生效,访问依然会出现401错误。/pr是配置的项目路径。但带上项目路径就不生效,这个问题很疑惑。
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注入进来.
导致过滤器全局生效.
正常配置,应该手动new, 而且过滤器类不能加@Component注解
具体原因,之后有空研究一下.
来源:https://blog.csdn.net/wangchengaihuiming/article/details/100129838
标签:springboot,security,忽略,地址
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java+MySQL实现学生信息管理系统源码
2023-11-28 04:29:31
![](https://img.aspxhome.com/file/2023/6/59866_0s.jpg)
C#设置文件权限的方法
2022-08-31 22:35:42
C#使用回溯法解决背包问题实例分析
2023-11-22 20:21:22
基于ChatGPT+SpringBoot实现智能聊天AI机器人接口并上线至服务器的方法
2023-07-01 06:19:34
![](https://img.aspxhome.com/file/2023/5/68015_0s.png)
Java 根据网址查询DNS/IP地址的方法
2023-06-21 15:31:54
java如何通过IP解析地理位置
2021-09-11 08:01:07
![](https://img.aspxhome.com/file/2023/5/61535_0s.png)
java使用websocket,并且获取HttpSession 源码分析(推荐)
2023-08-04 17:38:05
java8 实现提取集合对象的每个属性
2023-10-17 19:37:27
Java Email邮件发送简单实现介绍
2023-10-07 01:05:11
![](https://img.aspxhome.com/file/2023/8/62838_0s.png)
浅谈Java中Int、Integer、Integer.valueOf()、new Integer()之间的区别
2023-10-29 20:08:53
![](https://img.aspxhome.com/file/2023/4/68154_0s.png)
java应用占用内存过高排查的解决方案
2023-09-21 12:47:30
![](https://img.aspxhome.com/file/2023/4/77464_0s.jpg)
SpringBoot如何获取Kafka的Topic列表
2023-11-26 16:01:52
![](https://img.aspxhome.com/file/2023/2/60242_0s.png)
详解java模板和回调机制
2023-08-13 15:33:46
使用纯Java实现一个WebSSH项目的示例代码
2023-03-11 11:32:20
![](https://img.aspxhome.com/file/2023/8/64148_0s.png)
java搜索无向图中两点之间所有路径的算法
2023-11-10 09:28:26
C#模拟Http与Https请求框架类实例
2023-02-10 16:28:11
Android RecyclerView使用ListAdapter高效刷新数据的操作方法
2023-06-24 22:22:09
![](https://img.aspxhome.com/file/2023/0/83550_0s.gif)
JavaWeb利用struts实现文件下载时改变文件名称
2023-10-24 11:26:53
![](https://img.aspxhome.com/file/2023/1/71621_0s.jpg)
java实战之桌球小游戏
2022-04-22 20:40:13
![](https://img.aspxhome.com/file/2023/6/66986_0s.jpg)
使用fastjson中的JSONPath处理json数据的方法
2021-12-14 09:09:58