For a software in active development we are using Spring Boot (with Spring Security) and the Keycloak Adapter.
The goal is to:
@Public
(see the code snippet) (this works)Everything is working fine as it stands, but I have some problems understanding a few details:
KeycloakWebSecurityConfigurerAdapter
enables CSRF
protection. I think this is only done so it can register its own Matcher to allow requests from KeycloakJSESSIONID
cookie is returnedAccording to my understanding:
KeycloakWebSecurityConfigurerAdapter
enabling it). Is this only for the BASIC Auth
part?CSRF
protection is indeed needed - but I don't want the sessions in the first place and then the API would not need CSRF
protection, right?http.sessionManagement().disable()
after the super.configure(http)
call the JSESSIONID
cookie is set (so where is this coming from?)As stated in the code snippet, SessionAuthenticationStrategy
is not set to the null once since we use the Authorization
part of Keycloak and the application is a Service Account Manager
(thus managing those resource records).
Would be great if someone can clear things up. Thanks in advance!
@KeycloakConfiguration
public class WebSecurityConfiguration extends KeycloakWebSecurityConfigurerAdapter {
@Inject private RequestMappingHandlerMapping requestMappingHandlerMapping;
@Override
protected void configure(final HttpSecurity http) throws Exception {
super.configure(http);
http
.authorizeRequests()
.requestMatchers(new PublicHandlerMethodMatcher(requestMappingHandlerMapping))
.permitAll()
.anyRequest()
.authenticated();
}
// ~~~~~~~~~~ Keycloak ~~~~~~~~~~
@Override
@ConditionalOnMissingBean(HttpSessionManager.class)
@Bean protected HttpSessionManager httpSessionManager() {
return new HttpSessionManager();
}
/**
* {@link NullAuthenticatedSessionStrategy} is not used since we initiate logins
* from our application and this would not be possible with {@code bearer-only}
* clients (for which the null strategy is recommended).
*/
@Override
@Bean protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
}
/**
* HTTP session {@link ApplicationEvent} publisher needed for the
* {@link SessionRegistryImpl} of {@link #sessionAuthenticationStrategy()}
* to work properly.
*/
@Bean public HttpSessionEventPublisher httpSessionEventPublisher() {
return new HttpSessionEventPublisher();
}
@Override
@Bean public KeycloakAuthenticationProvider keycloakAuthenticationProvider() {
return super.keycloakAuthenticationProvider();
}
}
You may fall into excessive JWT token usage. Look at this article for example https://blog.logrocket.com/jwt-authentication-best-practices/. Especially look at the references at the end of the article about JWT as a session token.
For your web-application UI you are using sessions in most of the cases. It doesn't matter what type of token is used for authentication. Keycloak does everything correctly - it gives back httpOnly secure cookie for session management and tracks user status at backend. For better understanding of how it works you may look at the example code here: examples
For better separation of stateless backend (micro-)services and user UI session keycloak documentation suggest to use 2 different authentication stratagies: RegisterSessionAuthenticationStrategy
for sessions and NullAuthenticatedSessionStrategy
for bearer-only services
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With