I try to reproduce a https://www.baeldung.com/spring-boot-keycloak tutorial with some simplifications. Also my Keycloak server is on another machine. Config is also slightly changed due to deprecation of the antMatchers:
@Configuration
@EnableWebSecurity
class SecurityConfig {
private final KeycloakLogoutHandler keycloakLogoutHandler;
SecurityConfig(KeycloakLogoutHandler keycloakLogoutHandler) {
this.keycloakLogoutHandler = keycloakLogoutHandler;
}
@Bean
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests((requests) -> requests
.requestMatchers("/customers*")
.hasRole("user")
.anyRequest()
.permitAll()
);
http.oauth2Login()
.and()
.logout()
.addLogoutHandler(keycloakLogoutHandler)
.logoutSuccessUrl("/");
http.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt);
return http.build();
}
}
Properties:
spring.security.oauth2.client.registration.keycloak.client-id=***
spring.security.oauth2.client.registration.keycloak.authorization-grant-type=authorization_code
spring.security.oauth2.client.registration.keycloak.scope=openid
spring.security.oauth2.client.provider.keycloak.issuer-uri=http://192.168.254.1:8184/realms/***
spring.security.oauth2.client.provider.keycloak.user-name-attribute=preferred_username
spring.security.oauth2.resourceserver.jwt.issuer-uri=http://localhost:8080/realms/***
Unprotected resource works, login works, but after login redirect I get 403. I tried to disable CORS and/or CSRF, the same. Log:
2023-03-10T17:33:57.727+03:00 DEBUG 54908 --- [o-8080-Acceptor] o.apache.tomcat.util.threads.LimitLatch : Counting up[http-nio-8080-Acceptor] latch=1
2023-03-10T17:33:57.728+03:00 DEBUG 54908 --- [nio-8080-exec-6] o.a.coyote.http11.Http11InputBuffer : Before fill(): parsingHeader: [true], parsingRequestLine: [true], parsingRequestLinePhase: [0], parsingRequestLineStart: [0], byteBuffer.position(): [0], byteBuffer.limit(): [0], end: [515]
2023-03-10T17:33:57.728+03:00 DEBUG 54908 --- [nio-8080-exec-6] o.a.tomcat.util.net.SocketWrapperBase : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@7ab77668:org.apache.tomcat.util.net.NioChannel@136f346d:java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:47080]], Read from buffer: [0]
2023-03-10T17:33:57.729+03:00 DEBUG 54908 --- [nio-8080-exec-6] org.apache.tomcat.util.net.NioEndpoint : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@7ab77668:org.apache.tomcat.util.net.NioChannel@136f346d:java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:47080]], Read direct from socket: [567]
2023-03-10T17:33:57.729+03:00 DEBUG 54908 --- [nio-8080-exec-6] o.a.coyote.http11.Http11InputBuffer : Received [GET /customers?continue HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/110.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Cookie: Idea-296f3fac=79e0126b-1518-4c2a-b4a3-1f158c95489b; JSESSIONID=A71AB029616D5092A00BE2B11E978123
Upgrade-Insecure-Requests: 1
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Sec-Fetch-User: ?1
]
2023-03-10T17:33:57.729+03:00 DEBUG 54908 --- [nio-8080-exec-6] o.a.t.util.http.Rfc6265CookieProcessor : Cookies: Parsing b[]: Idea-296f3fac=79e0126b-1518-4c2a-b4a3-1f158c95489b; JSESSIONID=A71AB029616D5092A00BE2B11E978123
2023-03-10T17:33:57.729+03:00 DEBUG 54908 --- [nio-8080-exec-6] o.a.catalina.connector.CoyoteAdapter : Requested cookie session id is A71AB029616D5092A00BE2B11E978123
2023-03-10T17:33:57.730+03:00 DEBUG 54908 --- [nio-8080-exec-6] o.a.c.authenticator.AuthenticatorBase : Security checking request GET /customers
2023-03-10T17:33:57.730+03:00 DEBUG 54908 --- [nio-8080-exec-6] org.apache.catalina.realm.RealmBase : No applicable constraints defined
2023-03-10T17:33:57.730+03:00 DEBUG 54908 --- [nio-8080-exec-6] o.a.c.authenticator.AuthenticatorBase : Not subject to any constraint
2023-03-10T17:33:57.730+03:00 DEBUG 54908 --- [nio-8080-exec-6] o.s.security.web.FilterChainProxy : Securing GET /customers?continue
2023-03-10T17:33:57.731+03:00 DEBUG 54908 --- [nio-8080-exec-6] org.apache.tomcat.util.http.Parameters : Set encoding to UTF-8
2023-03-10T17:33:57.731+03:00 DEBUG 54908 --- [nio-8080-exec-6] org.apache.tomcat.util.http.Parameters : Decoding query null UTF-8
2023-03-10T17:33:57.731+03:00 DEBUG 54908 --- [nio-8080-exec-6] org.apache.tomcat.util.http.Parameters : Start processing with input [continue]
2023-03-10T17:33:57.732+03:00 DEBUG 54908 --- [nio-8080-exec-6] org.apache.tomcat.util.http.Parameters : Parameter starting at position [0] and ending at position [8] with a value of [continue] was not followed by an '=' character
2023-03-10T17:33:57.732+03:00 DEBUG 54908 --- [nio-8080-exec-6] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.dvperv.kkauth.KkauthController#customers(Principal, Model)
2023-03-10T17:33:57.733+03:00 DEBUG 54908 --- [nio-8080-exec-6] w.c.HttpSessionSecurityContextRepository : Retrieved SecurityContextImpl [Authentication=OAuth2AuthenticationToken [Principal=Name: [user1], Granted Authorities: [[OIDC_USER, SCOPE_email, SCOPE_openid, SCOPE_profile]], User Attributes: [{at_hash=cOAUGbpbGKcvhnfekDHyEA, sub=09581f25-c22d-4192-89e0-f90af6c8757c, email_verified=false, iss=http://192.168.254.1:8184/realms/***, typ=ID, preferred_username=user1, given_name=, nonce=M-_DevVz-pT0R6tT9SDg6Wd3S9DBb72mxDH-gBBk0Cc, sid=b75bcd4a-c656-4572-b095-450a038e137d, aud=[***], acr=0, azp=***, auth_time=2023-03-10T14:08:13Z, exp=2023-03-10T14:36:51Z, session_state=b75bcd4a-c656-4572-b095-450a038e137d, family_name=, iat=2023-03-10T14:31:51Z, jti=4607af9f-8e7a-4dfe-8c70-5b8b3e5982e3}], Credentials=[PROTECTED], Authenticated=true, Details=WebAuthenticationDetails [RemoteIpAddress=127.0.0.1, SessionId=432BECAABE9CFCC7EC117409C3E982E9], Granted Authorities=[OIDC_USER, SCOPE_email, SCOPE_openid, SCOPE_profile]]]
2023-03-10T17:33:57.735+03:00 DEBUG 54908 --- [nio-8080-exec-6] o.a.coyote.http11.Http11InputBuffer : Before fill(): parsingHeader: [true], parsingRequestLine: [true], parsingRequestLinePhase: [0], parsingRequestLineStart: [0], byteBuffer.position(): [0], byteBuffer.limit(): [0], end: [567]
2023-03-10T17:33:57.735+03:00 DEBUG 54908 --- [nio-8080-exec-6] o.a.tomcat.util.net.SocketWrapperBase : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@7ab77668:org.apache.tomcat.util.net.NioChannel@136f346d:java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:47080]], Read from buffer: [0]
2023-03-10T17:33:57.735+03:00 DEBUG 54908 --- [nio-8080-exec-6] org.apache.tomcat.util.net.NioEndpoint : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@7ab77668:org.apache.tomcat.util.net.NioChannel@136f346d:java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:47080]], Read direct from socket: [0]
2023-03-10T17:33:57.735+03:00 DEBUG 54908 --- [nio-8080-exec-6] o.a.coyote.http11.Http11InputBuffer : Received []
2023-03-10T17:33:57.736+03:00 DEBUG 54908 --- [nio-8080-exec-6] o.apache.coyote.http11.Http11Processor : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@7ab77668:org.apache.tomcat.util.net.NioChannel@136f346d:java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:47080]], Status in: [OPEN_READ], State out: [OPEN]
2023-03-10T17:33:57.736+03:00 DEBUG 54908 --- [nio-8080-exec-6] org.apache.tomcat.util.net.NioEndpoint : Registered read interest for [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@7ab77668:org.apache.tomcat.util.net.NioChannel@136f346d:java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:47080]]
2023-03-10T17:33:59.794+03:00 DEBUG 54908 --- [alina-utility-2] o.apache.catalina.session.ManagerBase : Start expire sessions StandardManager at 1678458839794 sessioncount 1
2023-03-10T17:33:59.794+03:00 DEBUG 54908 --- [alina-utility-2] o.apache.catalina.session.ManagerBase : End expire sessions StandardManager processingTime 0 expired sessions: 0