2
2
3
3
import com .thisaster .testtask .auth .service .SecurityUserDetailsService ;
4
4
import lombok .RequiredArgsConstructor ;
5
+ import lombok .extern .slf4j .Slf4j ;
5
6
import org .springframework .context .annotation .Bean ;
6
7
import org .springframework .context .annotation .Configuration ;
7
8
import org .springframework .security .authentication .AuthenticationManager ;
14
15
import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
15
16
import org .springframework .security .config .annotation .web .configurers .AbstractHttpConfigurer ;
16
17
import org .springframework .security .config .http .SessionCreationPolicy ;
18
+ import org .springframework .security .core .GrantedAuthority ;
17
19
import org .springframework .security .crypto .password .PasswordEncoder ;
18
20
import org .springframework .security .oauth2 .jwt .JwtDecoder ;
19
21
import org .springframework .security .oauth2 .server .resource .authentication .JwtAuthenticationConverter ;
20
22
import org .springframework .security .oauth2 .server .resource .authentication .JwtGrantedAuthoritiesConverter ;
21
23
import org .springframework .security .web .SecurityFilterChain ;
22
24
25
+ import java .util .Collection ;
26
+
23
27
@ Configuration
24
28
@ EnableWebSecurity
25
29
@ EnableMethodSecurity
26
30
@ RequiredArgsConstructor
31
+ @ Slf4j
27
32
public class SecurityConfiguration {
28
33
29
34
private final SecurityUserDetailsService userDetailsService ;
@@ -36,7 +41,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
36
41
http
37
42
.csrf (AbstractHttpConfigurer ::disable )
38
43
.authorizeHttpRequests (request -> request
39
- .requestMatchers ("/api/auth/register" ).access (AuthorityAuthorizationManager .hasRole ("SUPERVISOR" ))
44
+ .requestMatchers ("/api/auth/register" ).access (AuthorityAuthorizationManager .hasAuthority ("SUPERVISOR" ))
40
45
.requestMatchers ("/api" , "/swagger-ui/**" , "/v1/api-docs/**" ).permitAll ()
41
46
.requestMatchers ("/api/auth/login" ).permitAll ()
42
47
.anyRequest ().authenticated ())
@@ -59,6 +64,11 @@ public JwtAuthenticationConverter jwtAuthenticationConverter() {
59
64
60
65
JwtAuthenticationConverter jwtConverter = new JwtAuthenticationConverter ();
61
66
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
+ });
62
72
return jwtConverter ;
63
73
}
64
74
0 commit comments