I'm using Spring Boot 2 and Spring Security 11 with Java 11. I'm creating an API-only application and trying to secure endpoints relating to "users" ...
@Configuration
@EnableWebSecurity
class SecurityConfiguration extends WebSecurityConfigurerAdapter {
...
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.exceptionHandling().authenticationEntryPoint(jwtUnAuthorizedResponseAuthenticationEntryPoint).and()
.sessionManagement().sessionCreationPolicy(
SessionCreationPolicy.STATELESS).and()
.authorizeRequests()
.antMatchers("/api/users").access("hasRole('ADMIN')")
.anyRequest().authenticated();
http
.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
http
.headers()
.frameOptions().sameOrigin()
.cacheControl(); //disable caching
}
I have this RestController for users ...
@RestController
@RequestMapping("/api/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public ResponseEntity<List<User>> find() {
List<User> foundUsers = userService.find();
return ResponseEntity.ok(foundUsers);
}
@GetMapping("/{id}")
public ResponseEntity<User> read(@PathVariable("id") UUID id) {
User foundUser = userService.findById(id);
if (foundUser == null) {
return ResponseEntity.notFound().build();
} else {
return ResponseEntity.ok(foundUser);
}
}
@PostMapping
@ResponseStatus(code = HttpStatus.CREATED)
public void create(@Valid @RequestBody User user) {
userService.create(user);
}
@PutMapping("/{id}")
public ResponseEntity<User> update(@RequestBody User card, @PathVariable UUID id) {
final User updatedUser = userService.update(id, card);
if (updatedUser == null) {
return ResponseEntity.notFound().build();
} else {
return ResponseEntity.ok(updatedUser);
}
}
}
I would like to expand my security to allow people to access the GET and PUT endpoints if the ID of the user with which they are logged in matches the ID of the request. Is it possible to add a rule for that in HttpSecurity or do I allow everyone to access those endpoints and then add Java in the REST methods to check the user that is logged in?