Skip to content

Commit 6002f6e

Browse files
committed
refactor: code
1 parent 3013dc1 commit 6002f6e

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

src/main/java/com/thisaster/testtask/auth/config/SecurityConfiguration.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.thisaster.testtask.auth.service.SecurityUserDetailsService;
44
import lombok.RequiredArgsConstructor;
5+
import lombok.extern.slf4j.Slf4j;
56
import org.springframework.context.annotation.Bean;
67
import org.springframework.context.annotation.Configuration;
78
import org.springframework.security.authentication.AuthenticationManager;
@@ -14,16 +15,20 @@
1415
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
1516
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
1617
import org.springframework.security.config.http.SessionCreationPolicy;
18+
import org.springframework.security.core.GrantedAuthority;
1719
import org.springframework.security.crypto.password.PasswordEncoder;
1820
import org.springframework.security.oauth2.jwt.JwtDecoder;
1921
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter;
2022
import org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter;
2123
import org.springframework.security.web.SecurityFilterChain;
2224

25+
import java.util.Collection;
26+
2327
@Configuration
2428
@EnableWebSecurity
2529
@EnableMethodSecurity
2630
@RequiredArgsConstructor
31+
@Slf4j
2732
public class SecurityConfiguration {
2833

2934
private final SecurityUserDetailsService userDetailsService;
@@ -36,7 +41,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
3641
http
3742
.csrf(AbstractHttpConfigurer::disable)
3843
.authorizeHttpRequests(request -> request
39-
.requestMatchers("/api/auth/register").access(AuthorityAuthorizationManager.hasRole("SUPERVISOR"))
44+
.requestMatchers("/api/auth/register").access(AuthorityAuthorizationManager.hasAuthority("SUPERVISOR"))
4045
.requestMatchers("/api", "/swagger-ui/**", "/v1/api-docs/**").permitAll()
4146
.requestMatchers("/api/auth/login").permitAll()
4247
.anyRequest().authenticated())
@@ -59,6 +64,11 @@ public JwtAuthenticationConverter jwtAuthenticationConverter() {
5964

6065
JwtAuthenticationConverter jwtConverter = new JwtAuthenticationConverter();
6166
jwtConverter.setJwtGrantedAuthoritiesConverter(converter);
67+
jwtConverter.setJwtGrantedAuthoritiesConverter(jwt -> {
68+
Collection<GrantedAuthority> authorities = converter.convert(jwt);
69+
log.info("Extracted roles from token: {}", authorities);
70+
return authorities;
71+
});
6272
return jwtConverter;
6373
}
6474

src/main/java/com/thisaster/testtask/auth/config/UserPrincipal.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ public class UserPrincipal implements UserDetails {
2424

2525
@Override
2626
public Collection<? extends GrantedAuthority> getAuthorities() {
27-
return List.of(new SimpleGrantedAuthority("ROLE_" + user.getRole().getName().toUpperCase()));
27+
return List.of(new SimpleGrantedAuthority(user.getRole().getName().toUpperCase()));
2828
}
29-
3029
@Override
3130
public String getPassword() {
3231
return user.getPassword();

src/main/java/com/thisaster/testtask/auth/utils/JWTUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
@Component
1616
@RequiredArgsConstructor
1717
public class JWTUtils {
18-
private final JwtEncoder jwtEncoder;
1918

19+
private final JwtEncoder jwtEncoder;
2020

2121
public String generateToken(UserDetails userDetails) {
2222
Instant now = Instant.now();

0 commit comments

Comments
 (0)