I know this question has been answered a number of times ... but all the answers I found refer to earlier versions of Keycloak, and I cannot find the equivalents in the current versions (22.0.1).
I am trying to develop an application that uses OAuth2 login, where the login is referred to keycloak. That's pretty simple to do with the use of org.springframework.boot:spring-boot-starter-oauth2-client, and a security config like this:
public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) {
httpSecurity.authorizeHttpRequests()
.anyRequest().authenticated()
.and()
.oauth2Login();
return httpSecurity.build();
}
So far, so good. When I navigate to any page, I first have to log in through keycloak, and get forwarded to the page successfully. A call to SecurityContextHolder.getContext().getAuthentication() gets me the content of the token ... but what is missing are the roles that I assigned to the user in Keycloak. So I cannot use role based API access control.
When I follow the many examples that obtain the token through Postman (through /realms/{realm}/protocol/openid-connect/token, the roles are present. I could technically just write my own login screen, and use that approach, but I'd rather let keycloak handle the login (to cover for MFA and stuff).
Is it even possible to get the roles defined in Keycloak through a Spring boot OAuth2 login?
Thx,
Stefan