首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring安全permitAll()与排除urls不匹配

Spring安全permitAll()与排除urls不匹配
EN

Stack Overflow用户
提问于 2018-02-27 18:16:14
回答 1查看 1.3K关注 0票数 0

我使用了API的spring引导和集成的Outh2 spring安全性。我有更多的API端点,从/ API /v1/ .I开始,除了API /api/v1/test-data之外,需要对所有api进行身份验证。

我的Resourceserver http配置如下。

代码语言:javascript
复制
@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_ACCESS")可以处理"/api/v1/“下的所有端点。

我的休息控制器是

代码语言:javascript
复制
@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);
    }

}

如何将"/api/v1/test-data“从"hasRole”茅斯检查中排除?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-27 18:49:20

交换你的规则:

代码语言:javascript
复制
            .antMatchers("/api/v1/test-data").permitAll()
            .antMatchers("/api/v1/**").hasRole("API_ACCESS")

命令很重要。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49015355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档