Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow access to endpoints using role hierarchy #55

Merged
merged 1 commit into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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