Good day all.
I am struggling finding a way to implement any logic after user gets authenticated.
The idea is, after login, map some data to user and send it to frontend.
The problem is I cannot find a way to catch after authorization/login and add my own logic.
Any suggestion would be very welcome.
Ps. I am new to keycloak and spring security.
Cheers.
@KeycloakConfiguration
public class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
/**
* Register Keycloak with the Spring Security authentication manager.
*/
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) {
SimpleAuthorityMapper grantedAuthorityMapper = new SimpleAuthorityMapper();
grantedAuthorityMapper.setPrefix("ROLE_");
KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(grantedAuthorityMapper);
auth.authenticationProvider(keycloakAuthenticationProvider);
}
/**
* Registers a user session after successful authentication.
*/
@Bean
@Override
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
}
/**
* Adjusting the bean definition to be loaded conditionally only if
* no other bean of that type has been defined.
*/
@Bean
@Override
@ConditionalOnMissingBean(HttpSessionManager.class)
protected HttpSessionManager httpSessionManager() {
return new HttpSessionManager();
}
/**
* Define Role-Based Access Security Policies
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http
.authorizeRequests()
.antMatchers("/patient").hasAnyRole("ADMIN", "USER")
.antMatchers("/manager").hasAnyRole("ADMIN")
.anyRequest().permitAll();
}