Java Spring Security permitAll()与排除URL不匹配

Java Spring Security permitAll()与排除URL不匹配,java,spring,spring-boot,spring-security,spring-security-oauth2,Java,Spring,Spring Boot,Spring Security,Spring Security Oauth2,我使用spring boot和集成的Outh2 spring安全性完成了API。 我有更多以/API/v1/开头的API端点。我需要验证除API/API/v1/测试数据之外的所有API 我的Resourceserver http配置如下所示 @Override public void configure(HttpSecurity http) throws Exception { http. anonymous().disable()

我使用spring boot和集成的Outh2 spring安全性完成了API。 我有更多以/API/v1/开头的API端点。我需要验证除API/API/v1/测试数据之外的所有API

我的Resourceserver http配置如下所示

@Override
    public void configure(HttpSecurity http) throws Exception {
       http.
                anonymous().disable()
                .authorizeRequests()
                .antMatchers("/api/v1/**").hasRole("API_ACCESS")
                .antMatchers("/api/v1/test-data").permitAll()
                .and().exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());
    } 
但是
.antMatchers(“/api/v1/test data”).permitAll()
不适用于我,而是
.antMatchers(“/api/v1/**”).hasRole(“api\u访问”)
适用于“/api/v1/”下的所有端点

我的休息控制器是

@RestController
@RequestMapping(path="/api/v1")
public class HotelController {

    @Autowired
    private HotelService service;
    @Autowired
    private APIAuthService authservice; 

    @GetMapping("/hotels")
    public ResponseEntity<Map<String,Object>> hotelsGet(@RequestParam(value = "page", defaultValue="1", required = false) Integer page, @RequestParam(value = "size", defaultValue="25", required = false) Integer size
            , @RequestParam(value = "orderby", defaultValue="hotelname", required = false) String orderby, @RequestParam(value = "order", defaultValue="asc", required = false) String order) {
        return this.service.list(page,size,orderby,order);
    }

    @GetMapping("/test-data")
    public String hotelsGetTest(@RequestParam(value = "page", defaultValue="1", required = false) Integer page, @RequestParam(value = "size", defaultValue="10", required = false) Integer size) {
        return "tested";
    }

    @GetMapping("/baseauth")
    public boolean baseauth(@RequestHeader(value = "authorization", required = true) String authString) {
        return this.authservice.isUserAuthenticated(authString);
    }

}
@RestController
@请求映射(路径=“/api/v1”)
公共级酒店管理员{
@自动连线
私人酒店服务;
@自动连线
私人APIAuthService授权服务;
@GetMapping(“/hotels”)
公共响应属性hotelsGet(@RequestParam(value=“page”,defaultValue=“1”,required=false)整数页面,@RequestParam(value=“size”,defaultValue=“25”,required=false)整数大小
,@RequestParam(value=“orderby”,defaultValue=“hotelname”,required=false)String orderby,@RequestParam(value=“order”,defaultValue=“asc”,required=false)String order){
返回此.service.list(页面、大小、订购者、订单);
}
@GetMapping(“/测试数据”)
公共字符串hotelsgetest(@RequestParam(value=“page”,defaultValue=“1”,required=false)整数页,@RequestParam(value=“size”,defaultValue=“10”,required=false)整数大小){
返回“已测试”;
}
@GetMapping(“/baseauth”)
公共布尔baseauth(@RequestHeader(value=“authorization”,required=true)字符串authString){
返回此.authservice.isUserAuthenticated(authString);
}
}
如何将“/api/v1/测试数据”从“hasRole”outh检查中排除?

交换规则:

            .antMatchers("/api/v1/test-data").permitAll()
            .antMatchers("/api/v1/**").hasRole("API_ACCESS")
顺序很重要。

交换规则:

            .antMatchers("/api/v1/test-data").permitAll()
            .antMatchers("/api/v1/**").hasRole("API_ACCESS")

顺序很重要。

您禁用了匿名访问,但也希望获得许可证。我认为这里存在冲突。@Vasan Syl的回答是正确的,但这是我面临的主要问题,我从您那里得到了解决方案,您禁用了匿名访问,但也希望获得许可。我认为这里有冲突。@Vasan Syl的回答是正确的,但这是我面临的主要问题,我从Hi@Syl那里得到了解决方案,谢谢。所有的控制器都是正确的,但正如@Vasan所说,anonymous().disable()和.permitAll()之间存在冲突。这个解决方案在Hi@Syl中,谢谢。所有的控制器都是正确的,但正如@Vasan所说,anonymous().disable()和.permitAll()之间存在冲突。而解决方案就在这里