requestMatchers()方法与authorizeRequests()区别,ResourceServerConfigurerAdapter与WebSecurityConfigurerAdapt

一、requestMatchers()方法与authorizeRequests()区别

        1.理解这两个方法的区别首先要知道springsecurity中,每声明一个adapter实例,就会产生一条过滤器链,一个请求过来要走哪个过滤器链就是由requestMatchers()方法配置的url决定的。请求匹配上requestMatchers()配置的过滤器链后,在进一步的详细控制则是authorizeRequests()决定的。

        一句话概括就是requestMatchers()配置的是哪些url进行安全控制,authorizeRequests()配置的是如何进行控制

例如:

        下面这个adapter只对/api1/order/**和/api1/address/**两个url生效。(即当请求匹配这两个url其中之一时,才会进行安全控制,其他url可直接访问。)当一个请求的url匹配其中之一后,才会进入这个过滤器链。进入过滤器链后,匹配 /api1/order/**的请求全部需要认证后才能访问,而匹配/api1/address/bejing/**的请求可以直接访问

@Configuration
@EnableWebSecurity
public class MySecurityConfiguration extends WebSecurityConfigurerAdapter {




    @Override
    protected void configure(HttpSecurity http) throws Exception {

                http.requestMatchers()
                    .antMatchers("/api1/order/**","/api1/address/**")
                .and().authorizeRequests()
                    .antMatchers("/api1/order/**").authenticated()
                    .antMatchers("/api1/address/beijing/**").permitAll()
                .and()
                    .csrf().disable();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        AuthenticationManager manager = super.authenticationManagerBean();
        return manager;
    }
}

  二、ResourceServerConfigurerAdapter与WebSecurityConfigurerAdapt

        1.ResourceServerConfigurerAdapter是用于当使用spring的oath2时, 配置哪些url要进行oauth2认证

        2.WebSecurityConfigurerAdapter是用于当前这个项目本身的访问控制。

        3.ResourceServerConfigurerAdapter与WebSecurityConfigurerAdapter都有一个相同的方法,配置url的访问安全控制策略:    

 public void configure(HttpSecurity http) throws Exception {}

        如果在这个方法中配置了相同的url访问控制,会发现ResourceServerConfigurerAdapter配置控制策略生效,而WebSecurityConfigurerAdapter配置的策略不起作用。是因为ResourceServerConfigurerAdapter的order值小,优先级高。所以ResourceServerConfigurerAdapter起作用。

参考自己的文章:https://blog.csdn.net/join_null/article/details/119390298

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值