SpringSecurity常见问题解决:设置忽略地址不生效的问题

一、设置忽略地址不生效的问题

  最近在试下微服务改造,出现这样一个问题所有请求都经过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:具体的权限控制规则配置。

  原文链接:https://blog.csdn.net/wangchengaihuiming/article/details/100129838

原文地址:https://www.cnblogs.com/goloving/p/14880698.html