Spring 安全 permitAll() 不匹配排除 url
Spring Security permitAll() not matching for exclude urls
我使用 spring-boot 和集成的 Outh2 spring 安全性做了 API。
我有更多 API 以 /api/v1/ 开头的端点。我需要验证所有 API 除了 API /api/v1/test-data.
我的 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);
}
}
如何从 "hasRole" outh 检查中排除“/api/v1/test-data”?
交换你的规则:
.antMatchers("/api/v1/test-data").permitAll()
.antMatchers("/api/v1/**").hasRole("API_ACCESS")
顺序很重要。
我使用 spring-boot 和集成的 Outh2 spring 安全性做了 API。 我有更多 API 以 /api/v1/ 开头的端点。我需要验证所有 API 除了 API /api/v1/test-data.
我的 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);
}
}
如何从 "hasRole" outh 检查中排除“/api/v1/test-data”?
交换你的规则:
.antMatchers("/api/v1/test-data").permitAll()
.antMatchers("/api/v1/**").hasRole("API_ACCESS")
顺序很重要。