ali-bouali / spring-boot-3-jwt-security

Sample project on how to implement JWT security based using Spring boot 3 and Spring security 6
https://aliboucoding.com/p/securing-your-spring-boot-3-0-applications-with-json-web-tokens-jwt
Apache License 2.0
1.86k stars 870 forks source link

AuthenticationService.java line 43 - Double call to the database #3

Closed ghost closed 1 year ago

ghost commented 1 year ago

You can retrieve the user details from the authentication object instead of making another call to the database:

Authentication authentication = authenticationManager.authenticate(
                new UsernamePasswordAuthenticationToken(
                        request.getUsernameOrEmail(),
                        request.getPassword()
                )
        );
var user = (User) authentication.getPrincipal();
var jwtToken = jwtService.generateJwt(user);
RapidFire05 commented 1 year ago

Thank you! I also jsut discovered this.

ali-bouali commented 1 year ago

yes that's true. As I mentioned, it is just a sample project to show how to implement JWT security with Spring boot