[JAVA] How to get keycloak credentials in interceptor class

When making a request to the API with the user's credentials from keycloak authentication From the intercepter before the method of the controller class is called Note that I had a hard time not knowing that there is a getUserPrincipal () method to get user information.

intercepterTestClass.java



  @Autowired
  private UserDto userDto;

  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
      throws Exception {
    //

    //If you don't have your credentials
    if (request.getUserPrincipal() == null) {
      System.out.println("Dummy setting because there is no authentication information");
      userDto.setUserid("U0001");
      userDto.setUserName("guest");
      return true;
    }

    //
    final KeycloakSecurityContext context = KeycloakPrincipal.class.cast(request.getUserPrincipal()).getKeycloakSecurityContext();
  
    userDto.setUserName(context.getToken().getPreferredUsername());
    userDto.setUserId(String.valueOf(context.getToken().getOtherClaims().get("user_id")));

    //
    return true;

See another article for the implementation of keycloak authentication. By obtaining the authentication information with the interceptor The versatility has improved because it is no longer necessary to describe it in each Controller class. Until now, I've only used Spring Security as the authentication function, Spring Security doesn't seem to be very suitable for API implementation.

Recommended Posts

How to get keycloak credentials in interceptor class
How to get Class from Element in Java
How to get parameters in Spark
How to get the class name / method name running in Java
How to get date data in Ruby
How to get the date in java
How to conditionally add html.erb class in Rails
How to get information about associated tables in many-to-many tables
How to use java class
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
How to get values in real time with TextWatcher (Android)
Mapping to a class with a value object in How to MyBatis
How to find May'n in XPath
How to use Java HttpClient (Get)
How to hide scrollbars in WebView
How to get started with slim
How to run JUnit in Eclipse
How to iterate infinitely in Ruby
[Rails] How to write in Japanese
How to run Ant in Gradle
How to master programming in 3 months
How to disassemble Java class files
How to use the wrapper class
How to learn JAVA in 7 days
How to install Bootstrap in Ruby
How to decompile java class files
How to use InjectorHolder in OpenAM
[Java] How to use LinkedHashMap class
How to use class methods [Java]
How to introduce jQuery in Rails 6
How to use classes in Java?
How to name variables in Java
How to get along with Rails
How to set Lombok in Eclipse
[Java] How to use Math class
How to concatenate strings in java
How to install Swiper in Rails
How to get the id of PRIMAY KEY auto_incremented in MyBatis
How to get boolean value with jQuery in rails simple form
How to get and add data from Firebase Firestore in Ruby
(Spring-data-jpa) How to get POJO (other than entity class) using JPA2.1
How to think about class design (division) in a business system (1)
How to get the length of an audio file in java
How to get JDK 11 on your mac in a comfortable way
[swift5] How to specify color in hexadecimal
How to implement search functionality in Rails
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
How to change app name in rails
[Java] How to use the File class
How to use custom helpers in rails
[Kotlin] 3 ways to get Class from KClass
How to reflect seeds.rb in production environment
How to use named volume in docker-compose.yml
How to filter JUnit Test in Gradle
How to insert a video in Rails
How to standardize header footer in Thymeleaf
[Java] How to use the HashMap class
How to include Spring Tool in Eclipse 4.6.3?
[Note] How to get started with Rspec