How to configure security filter chain
@KeycloakConfiguration
@EnableWebSecurity
@Order(2)
@EnableGlobalMethodSecurity(jsr250Enabled = true, prePostEnabled = true)
public class IamSecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http.authorizeRequests()
// Allow open access to specific endpoints
.antMatchers("/login").permitAll()
.antMatchers("/refreshToken").permitAll()
.antMatchers("/user").permitAll()
.antMatchers("/user/*").permitAll()
.antMatchers("/actuator/health").permitAll()
.antMatchers("/swagger-ui/**", "/v3/api-docs/**", "/user/storage/get-meta-file").permitAll()
// All other requests require authentication
.anyRequest().authenticated();
// Disable Cross-Site Request Forgery (CSRF) and enable Cross-Origin Resource Sharing (CORS)
http.cors().and().csrf().disable();
}
// Defines the session authentication strategy to use NullAuthenticatedSessionStrategy
@Bean
@Override
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
return new NullAuthenticatedSessionStrategy();
}
// Defines BCrypt as the password encoding mechanism
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}Last updated