I did API's using spring-boot and integrated Outh2 spring security. I have more API end points starting with /api/v1/ .I need to authenticate all the API except the API /api/v1/test-data.
My Resourceserver http config is as follows.
@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());
}
But .antMatchers("/api/v1/test-data").permitAll()
is not working for me but .antMatchers("/api/v1/**").hasRole("API_ACCESS")
is working for all endpoints coming under "/api/v1/".
My rest controllers are
@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);
}
}
How to exclude "/api/v1/test-data" from "hasRole" outh check?