springboot-security报错-Can‘t configure anyRequest after itself

配置前提

springboot2.4.1 与security

代码
 @Override
    protected void configure(HttpSecurity http) throws Exception {
        //通过配置实现,不需要JWT令牌就可以访问的接口
        for(String uri : jwtProperties.getPermitAllURI()){
            http
                    .authorizeRequests().antMatchers(uri).permitAll();
        }

        http
                .csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                .authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS).permitAll()
//                .antMatchers("/**").permitAll()
                .anyRequest().authenticated()//任何没匹配上antMatchers的,只需要用户被验证有token的
//                .anyRequest().access("@rabcService.hasPermission(request,authentication)")//可以放行经过验证有权限的用户
                .and()
                .cors()
                .and()
                .exceptionHandling()
                .accessDeniedHandler(restfulAccessDeniedHandler())//没有权限时自定义异常
                .authenticationEntryPoint(restAuthenticationEntryPoint());//没有token时自定义异常
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);

        //RBAC权限控制级别的接口权限校验
//        http
//                .authorizeRequests().anyRequest()
//                .access("@rabcService.hasPermission(request,authentication)");
    }

最底下的注释掉了,后来放到了中间。也就是如下两行,只能有一行。

                .anyRequest().authenticated()//任何没匹配上antMatchers的,只需要用户被验证有token的
//                .anyRequest().access("@rabcService.hasPermission(request,authentication)")//可以放行经过验证有权限的用户

因为底层源码的关系,一旦使用了anyRequest(),就不能再使用第二次了,否则会报错
在这里插入图片描述
报错代码:Can’t configure anyRequest after itself

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值