Skip to content

Commit

Permalink
Merge pull request #55 from pawl1n/fix_role_hierarchy
Browse files Browse the repository at this point in the history
fix: allow access to endpoints using role hierarchy
  • Loading branch information
pawl1n authored Apr 12, 2023
2 parents 9eff15f + c0b0c06 commit d346769
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.authorization.AuthorityAuthorizationManager;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
Expand All @@ -33,6 +34,7 @@
import org.springframework.security.oauth2.jwt.NimbusJwtEncoder;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationProvider;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.access.intercept.RequestAuthorizationContext;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
Expand Down Expand Up @@ -65,6 +67,10 @@ public PasswordEncoder passwordEncoder() {

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
AuthorityAuthorizationManager<RequestAuthorizationContext> hasRoleUser =
AuthorityAuthorizationManager.hasRole(Role.USER.name());
hasRoleUser.setRoleHierarchy(roleHierarchy());

return httpSecurity
.csrf(AbstractHttpConfigurer::disable)
.cors(Customizer.withDefaults())
Expand All @@ -75,7 +81,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws
.requestMatchers(GET, PUBLIC_GET_ENDPOINTS)
.permitAll()
.requestMatchers(USER_ENDPOINTS)
.hasRole(Role.USER.name())
.access(hasRoleUser)
.anyRequest()
.hasRole(Role.ADMIN.name()))
.sessionManagement(
Expand All @@ -88,9 +94,10 @@ public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws
@Bean
RoleHierarchy roleHierarchy() {
Map<String, List<String>> roleHierarchyMap = new HashMap<>();
roleHierarchyMap.put(Role.ADMIN.name(), List.of(Role.USER.name()));
roleHierarchyMap.put(Role.ADMIN.withPrefix(), List.of(Role.USER.withPrefix()));
RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
roleHierarchy.setHierarchy(RoleHierarchyUtils.roleHierarchyFromMap(roleHierarchyMap));

return roleHierarchy;
}

Expand All @@ -107,7 +114,6 @@ CorsConfigurationSource corsConfigurationSource() {
return source;
}


@Bean
@Primary
JwtEncoder jwtAccessTokenEncoder() {
Expand Down Expand Up @@ -135,7 +141,8 @@ JwtDecoder jwtRefreshTokenDecoder() {
@Bean
@Qualifier("refreshToken")
JwtAuthenticationProvider jwtAuthenticationProvider() {
JwtAuthenticationProvider jwtAuthenticationProvider = new JwtAuthenticationProvider(jwtRefreshTokenDecoder());
JwtAuthenticationProvider jwtAuthenticationProvider =
new JwtAuthenticationProvider(jwtRefreshTokenDecoder());
jwtAuthenticationProvider.setJwtAuthenticationConverter(jwtToUserConverter);
return jwtAuthenticationProvider;
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/ua/kishkastrybaie/user/Role.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@

public enum Role {
USER,
ADMIN
ADMIN;

public String withPrefix() {
return "ROLE_" + name().toUpperCase();
}
}
2 changes: 1 addition & 1 deletion src/main/java/ua/kishkastrybaie/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void setEmail(String email) {

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return List.of(new SimpleGrantedAuthority("ROLE_" + role.name()));
return List.of(new SimpleGrantedAuthority(role.withPrefix()));
}

@Override
Expand Down

0 comments on commit d346769

Please sign in to comment.