Spring Security: recommended way to handle per-user permissions with JWT without hitting the DB twice per request?
I'm using Spring Boot 3 / Spring Security 6 with JPA. My JWT contains only the user's email. On every request, my filter loads the User entity from the DB and puts it in the SecurityContext: String email = jwtService.extractEmail(token); User user = userRepository.findByEmail(email).orElseThrow(); var auth = new UsernamePasswordAuthenticationToken( user, null, user.getAuthorities()); SecurityContextHolder.getContext().setAuthentication(auth); The entity is detached by the time it reaches my services, so when I need to modify the user I have to fetch it again, which costs a second query per req
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.