温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

spring boot security设置忽略地址不生效的解决方法

发布时间:2021-07-19 17:17:52 来源:亿速云 阅读:1312 作者:chen 栏目:开发技术

这篇文章主要讲解了“spring boot security设置忽略地址不生效的解决方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“spring boot security设置忽略地址不生效的解决方法”吧!

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注解

具体原因,之后有空研究一下.

感谢各位的阅读,以上就是“spring boot security设置忽略地址不生效的解决方法”的内容了,经过本文的学习后,相信大家对spring boot security设置忽略地址不生效的解决方法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI