I am upgrading from spring boot 2.7.x to 3.0.0. After doing changes as recommended in the official docs I found that my role hierarchies are not being honored.
I added expressionHandler()
to my code as suggested in AccessDecisionVoter Deprecated with Spring Security 6.x but it doesn't work.
Any ideas what am I missing?
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {
@Bean
public SecurityFilterChain configure(
HttpSecurity http,
RequestHeaderAuthenticationFilter headerAuthenticationFilter) throws Exception {
HttpStatusEntryPoint authenticationEntryPoint =
new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED);
http
.addFilterAfter(headerAuthenticationFilter, RequestHeaderAuthenticationFilter.class)
.authorizeHttpRequests(auth -> auth
.requestMatchers("/actuator/**", "/", "/webjars/**").permitAll()
.requestMatchers(HttpMethod.POST).hasRole("SUPERUSER")
.requestMatchers(HttpMethod.GET).hasRole("USER"))
.sessionManagement(session -> session
.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.exceptionHandling(ex -> ex
.authenticationEntryPoint(authenticationEntryPoint)
.accessDeniedHandler(accessDeniedHandler()))
.csrf(customizer -> customizer.disable());
return http.build();
}
@Bean
public RequestHeaderAuthenticationFilter headerAuthenticationFilter(
...
}
@Bean
public RoleHierarchy roleHierarchy() {
RoleHierarchyImpl r = new RoleHierarchyImpl();
r.setHierarchy("ROLE_SUPERUSER > ROLE_USER");
return r;
}
@Bean
public DefaultWebSecurityExpressionHandler expressionHandler() {
DefaultWebSecurityExpressionHandler expressionHandler = new DefaultWebSecurityExpressionHandler();
expressionHandler.setRoleHierarchy(roleHierarchy());
return expressionHandler;
}