我使用了API的spring引导和集成的Outh2 spring安全性。我有更多的API端点,从/ API /v1/ .I开始,除了API /api/v1/test-data之外,需要对所有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_ACCESS")可以处理"/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);
}
}如何将"/api/v1/test-data“从"hasRole”茅斯检查中排除?
发布于 2018-02-27 18:49:20
交换你的规则:
.antMatchers("/api/v1/test-data").permitAll()
.antMatchers("/api/v1/**").hasRole("API_ACCESS")命令很重要。
https://stackoverflow.com/questions/49015355
复制相似问题