user login on /login, /login redireting user to oauth2 keycloak next(after successful login) keycloak redirecting user to /home but user is not authenticated. roles are not working too. /roleDebug return authority: [ROLE_ANONYMOUS], /loginDebug return User is not authenticated, /tokenDebug return NullPointerException
@RestController
public class DebugController {
@GetMapping("/tokenDebug")
public void tokenDebug(OAuth2AuthenticationToken authentication) {
if (authentication == null) {
System.out.println("No authentication token found");
}
OAuth2User user = authentication.getPrincipal();
System.out.println("Access Token: " + user.getAttributes().get("access_token"));
}
@GetMapping("/roleDebug")
public void rD(){
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
System.out.println(auth);
Collection<? extends GrantedAuthority> roles = auth.getAuthorities();
System.out.println("authority: " + roles);
}
@GetMapping("/loginDebug")
public void getCurrentUser(@AuthenticationPrincipal OAuth2AuthenticationToken authentication) {
if (authentication == null) {
System.out.println("User is not authenticated");
}
else{
System.out.println(authentication.getPrincipal());
}
}
}
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(auth ->
auth
.requestMatchers("/","/loginDebug" ,"/login","roleDebug","/tokenDebug").permitAll()
.requestMatchers("/adminPanel").hasAuthority("ADMIN")
.requestMatchers("/home").authenticated()
.anyRequest().authenticated()
)
.oauth2Login(oauth2Login ->
oauth2Login
.loginPage("/login")
.defaultSuccessUrl("/home",true)
);
return http.build();
}
}
spring.security.oauth2.client.registration.keycloak.redirect-uri=http://localhost:8180/home
spring.security.oauth2.client.registration.keycloak.issuer-uri=http://localhost:8080/realms/master
spring.security.oauth2.client.provider.keycloak.authorization-uri=http://localhost:8080/realms/master/protocol/openid-connect/auth
spring.security.oauth2.client.provider.keycloak.token-uri=http://localhost:8080/realms/master/protocol/openid-connect/token
spring.security.oauth2.client.provider.keycloak.user-info-uri=http://localhost:8080/realms/master/protocol/openid-connect/userinfo
spring.security.oauth2.client.provider.keycloak.jwk-set-uri=http://localhost:8080/realms/master/protocol/openid-connect/certs
server.port=8180
spring.security.oauth2.client.registration.keycloak.client-id=springClient
spring.security.oauth2.client.registration.keycloak.client-secret=gJNj148Eukyqo5YEALZ805UXbz2fcsDp
spring.security.oauth2.client.registration.keycloak.scope=openid,profile,email
spring.security.oauth2.client.registration.keycloak.authorization-grant-type=authorization_code
spring.security.oauth2.client.provider.keycloak.user-name-attribute=preferred_username
spring.security.oauth2.client.provider.keycloak.user-info-authentication-method=form
"Authorization": {
"springClient": {
"roles": [
"ROLE_USER",
"ROLE_ADMIN"
]
},
token is working propertly. i have read spring boot search for roles in token/Authorization so changed resource_acces for Authorization(before it didn’t work too)
i have tried change resource_access name for Authorization, change application.properties, implment jwt decoder,change securityFilterChain. i am expecting that after login(keycloak oauth2) user will rdirected to /home and will has Authorization