OAuth2资源服务器白名单接口带token被拦截

复制代码
在资源服务器的配置中,添加了请求白名单,如下
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
    @Autowired
    private OAuth2Properties properties;
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/test/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .csrf().disable(); 
    }
    @Bean
    public RemoteTokenServices tokenServices() {
        RemoteTokenServices services = new RemoteTokenServices();
        services.setCheckTokenEndpointUrl(properties.getTokenInfoUri());
        services.setClientId(properties.getClientId());
        services.setClientSecret(properties.getClientSecret());
        return services;
    }
}

测试controller

复制代码
@RestController
@RequestMapping("/test")
public class TestController {
    @PostMapping("/test1")
    public String test1() {
        System.out.println(123);
        return "123";
    }
    @PostMapping("/test2")
    public String test2() {
        System.out.println(333);
        return "222333";
    }
}

当使用postman正常请求http://localhost:8109/test/test2时,能获取到返回结果

但当请求添加上请求头时(这里是前端做了统一的处理,到后端的请求会统一携带Authorization等token信息),但是对于我的资源服务接口来说,我不想管前端的请求是否携带请求头token,都想根据白名单不进行oauth2的鉴权操作,但是实际是这样还是会触发鉴权

可以通过重写WebSecurityConfigurerAdapter的 configure()方法,使白名单请求不受Spring Security的保护。这样即使请求中包含Authorization头,也不会触发鉴权(在资源服务器中添加)。

复制代码
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/test/test2");
    }
}
相关推荐
Smoothcloud润云7 分钟前
写实向生图智能体的工程实践:数据集取向、参数配置与两个纪实案例复现
服务器·数据库·人工智能·ai·ai编程·gpu算力
Resean022313 分钟前
SpringBoot 源码分析(二)自动装配原理
java·spring boot·后端·spring
智购科技自动贩卖机14 分钟前
自动售货机SaaS多租户数据库实战:分库分表与租户隔离~YH
android·java·linux·开发语言·javascript·网络
海宇大数据1 小时前
零信任架构实战:基于海宇企业股权变更构建自动化对公贷后股权异动监测网关
java·人工智能·架构·自动化
伊布拉西莫1 小时前
从静态参数到动态复用:Jenkins Parameters、Active Choices 与 Scriptler 实战指南
java·servlet·jenkins·devops
Yula99331 小时前
信创邮箱的“末端暴露”效应与兼容性收敛路径——基于亿邮全栈信创实践的工程分析
运维·服务器·网络·信创邮箱·兼容性收敛·邮件系统模块化接口·亿邮
青草地溪水旁1 小时前
缓冲头解剖:Linux 0.11 里那 36 字节如何管住 1KB 磁盘块
linux·运维·服务器
FYKJ_20102 小时前
Springcloud校园访客系统95346-计算机毕业设计、课程设计
java·spring boot·后端·spring·spring cloud·架构·课程设计
EatFan10 小时前
Spring Boot 4.0 升级清单:JDK 基线、Jakarta 命名空间、Observation API 与第三方组件兼容一次讲清
java·spring boot·后端
辰海Coding11 小时前
平台如何接入一台物联网设备?又该如何设计它的通信协议?
java·物联网