0

I am new to Keycloak. I started using it as a part of my spring-boot application.

I have extended KeycloakWebSecurityConfigurerAdapter and overridden methods like configure, configureGloabl etc., to have a specific authentication (see below code snippet.)

I am wondering if there is a possibility to get access of JWT token object for fetching further properties. Also it is not clear to me how to invalidate the token once the user is logged-out. At present once a single user is logged in, I am unable to sign him out and the JWT token seems to be remaining all the time.

protected void configure(HttpSecurity http) throws Exception {
    super.configure(http);
    http.authorizeRequests()
        .antMatchers("/customers*")
        .hasRole("user")
        .anyRequest()
        .permitAll();
}
Aritz
  • 30,971
  • 16
  • 136
  • 217
Zaks M
  • 87
  • 9

1 Answers1

2

you can get the access token in postman using this;

enter image description here

here ConfigKeycloak is realm name and config-app is client name.

Another way to get the access token is this.

 @RequestMapping(value = "/customers", method = RequestMethod.GET)
 @PreAuthorize("hasRole('ROLE_USER')")
 public String getCustomers(){
    KeycloakAuthenticationToken token = (KeycloakAuthenticationToken) request.getUserPrincipal();
    KeycloakPrincipal principal=(KeycloakPrincipal)token.getPrincipal();
    KeycloakSecurityContext session = principal.getKeycloakSecurityContext();
    AccessToken accessToken = session.getToken();
    String a = principal.getName();
    username = accessToken.getPreferredUsername();
    emailID = accessToken.getEmail();
    lastname = accessToken.getFamilyName();
    firstname = accessToken.getGivenName();
    realmName = accessToken.getIssuer();
    AccessToken.Access realmAccess = accessToken.getRealmAccess();
    }

you can logout from the session using this.

  @RequestMapping(value = "/logout", method = RequestMethod.GET)
  public String logout(HttpServletRequest request) throws ServletException {
    request.logout();
    return "/";
  }
Rahul Baghaniya
  • 291
  • 4
  • 5