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");
    }
}
相关推荐
二进制person1 分钟前
JavaEE进阶 --Spring Framework、Spring Boot和Spring MVC(1)
spring boot·spring·java-ee
编程学习0013 分钟前
记一次Java面试
java·面试
计算机学姐6 分钟前
基于SpringBoot+Vue的家政服务预约系统【个性化推荐+数据可视化】
java·vue.js·spring boot·后端·mysql·信息可视化·java-ee
一只大袋鼠6 分钟前
请求转发vs重定向、同源策略与跨域
java·javaweb·同源策略·请求转发·重定向
小胖java7 分钟前
基于LDA主题模型与情感分析的航空客户满意度分析
java·spring boot·spring
左左右右左右摇晃9 分钟前
Java并发——Lock锁
java·开发语言·笔记
森林里的程序猿猿10 分钟前
导致内存泄漏的ThreadLocal详解
java·jvm·数据结构
wangchunting11 分钟前
Java设计模式
java·单例模式·设计模式
Dream_sky分享17 分钟前
Excel模板下载(Resources目录下)
java·spring boot·后端
西海天际蔚蓝18 分钟前
线上环境接口访问转到本机的一套小工具
java·python