DEV Community

Cover image for Implementing Spring Security 6 with Spring Boot 3: A Guide to OAuth and JWT with Nimbus for Authentication

Implementing Spring Security 6 with Spring Boot 3: A Guide to OAuth and JWT with Nimbus for Authentication

isaac uchechukwu on October 12, 2023

Introduction Since the introduction of Spring Security 6, I have encountered many developers who experienced issues getting their heads ...
Collapse
 
othmane_darhoni profile image
Othmane Darhoni

in the main method you need to change from this

public CommandLineRunner initializeUser(UserRepository userRepository, BCryptPasswordEncoder passwordEncoder)

to this

public CommandLineRunner initializeUser(UserRepository userRepository, PasswordEncoder passwordEncoder)

if you pass a BCryptPasswordEncoder in the parameter you will get an error

No beans of 'BCryptPasswordEncoder' type found.

Collapse
 
darkmoon profile image
gontran

Hello, thank you for this tutorial. When I try to retrieve the name of the authenticated user ( SecurityContextHolder.getContext().getAuthentication().getName() ) I get Null what should I do to solve this problem?

Collapse
 
osamamoinchawla profile image
osamamoinchawla

hello gontran, have you be able to generate public and private RSA keys successfully?

Collapse
 
osamamoinchawla profile image
osamamoinchawla

you getting NULL, there could be many possibilities:

  • there is no NAME field in user model
  • maybe the field is empty no value
  • check out for the spelling
Collapse
 
osamamoinchawla profile image
osamamoinchawla

ERROR: "User account is locked"

Collapse
 
andreschile87 profile image
andresChile

hi, please check your code in AuthUser, probably you wrote something wrong. i had the same problem and i checked my error in that class. i hope you can resolve your error. greetings

Collapse
 
parten profile image
Pard Tengah

yep @andreschile87 is correct. Change return types to true of all the methods that we are overiding which came from UserDetails interface .

Collapse
 
nartai_aitmukashev_6f826e profile image
Nartai Aitmukashev

configure cors if requests are not working on postman

Collapse
 
osamamoinchawla profile image
osamamoinchawla

Fantastic guide!
I am kinda stuck at RSA public and private key generation!!!
The commands you have written up are not working for me! is there any other way?

Collapse
 
vittorfraga profile image
Carlos Fraga

*in his github code there's a little difference:
*

private-key: classpath:certs/private-keyy.pem
  public-key: classpath:certs/public-key.pem
Enter fullscreen mode Exit fullscreen mode

*maybe thats why the command is not working, you can try replace the command
*

openssl pkcs8 -topk8 -inform PEM -outform PEM -in private-key.pem -out private-key.pem -nocrypt
Enter fullscreen mode Exit fullscreen mode

for

openssl pkcs8 -topk8 -inform PEM -outform PEM -in private-key.pem -out private-keyy.pem -nocrypt
Enter fullscreen mode Exit fullscreen mode