JAVA-19354 Potential issue in A Quick Guide to Using Keycloak With Spring Boot article (#14537)

This commit is contained in:
anuragkumawat 2023-08-17 17:58:22 +05:30 committed by GitHub
parent e21b1e364e
commit 65159d313a
1 changed files with 17 additions and 4 deletions

View File

@ -2,6 +2,7 @@ package com.baeldung.keycloak;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@ -27,18 +28,30 @@ class SecurityConfig {
return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl()); return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
} }
@Order(1)
@Bean @Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { public SecurityFilterChain clientFilterChain(HttpSecurity http) throws Exception {
http.authorizeRequests() http.authorizeRequests()
.antMatchers("/customers*") .antMatchers("/")
.hasRole("USER") .permitAll()
.anyRequest() .anyRequest()
.permitAll(); .authenticated();
http.oauth2Login() http.oauth2Login()
.and() .and()
.logout() .logout()
.addLogoutHandler(keycloakLogoutHandler) .addLogoutHandler(keycloakLogoutHandler)
.logoutSuccessUrl("/"); .logoutSuccessUrl("/");
return http.build();
}
@Order(2)
@Bean
public SecurityFilterChain resourceServerFilterChain(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/customers*")
.hasRole("USER")
.anyRequest()
.authenticated();
http.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt); http.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt);
return http.build(); return http.build();
} }