12
12
import org .springframework .security .web .SecurityFilterChain ;
13
13
import org .springframework .security .web .authentication .UsernamePasswordAuthenticationFilter ;
14
14
import org .springframework .web .servlet .config .annotation .CorsRegistry ;
15
+ import org .springframework .web .servlet .config .annotation .EnableWebMvc ;
16
+ import org .springframework .web .servlet .config .annotation .ViewControllerRegistry ;
15
17
import org .springframework .web .servlet .config .annotation .WebMvcConfigurer ;
18
+
16
19
// SecurityConfig.java
17
20
@ Configuration
18
21
@ EnableWebSecurity
22
+ @ EnableWebMvc
19
23
public class SecurityConfig implements WebMvcConfigurer {
20
24
21
25
private final AuthenticationProvider authenticationProvider ;
22
26
private final JwtAuthenticationFilter jwtAuthenticationFilter ;
23
27
24
- private static final String ADMIN = "ADMIN" ;
25
- private static final String API_GYM = "/api/gym/**" ;
26
- private static final String API_GYMS = "/api/gyms/**" ;
27
- private static final String API_USER = "/api/user/**" ;
28
+ @ Value ("${app.roles.admin}" )
29
+ private String ADMIN ;
30
+
31
+ @ Value ("${app.roles.user}" )
32
+ private String USER ;
28
33
29
34
@ Value ("${app.cors.allowed-origins}" )
30
35
private String allowedOrigin ;
31
36
37
+ @ Value ("${app.cors.allowed-headers}" )
38
+ private String allowedHeaders ;
39
+
40
+ @ Value ("${app.cors.allowed-methods}" )
41
+ private String allowedMethods ;
42
+
43
+ private static final String API_GYM = "/api/gym/**" ;
44
+ private static final String API_GYMS = "/api/gyms/**" ;
45
+ private static final String API_USER = "/api/user/**" ;
46
+ private static final String API_AUTH = "/api/auth/**" ;
47
+ private static final String SWAGGER_UI_HTML = "/swagger-ui.html" ;
48
+ private static final String [] SWAGGER_UI = {"/" , "/swagger-ui/**" , "/v3/api-docs/**" , SWAGGER_UI_HTML , "/webjars/**" , "/swagger-resources/**" };
49
+
32
50
public SecurityConfig (AuthenticationProvider authenticationProvider , JwtAuthenticationFilter jwtAuthenticationFilter ) {
33
51
this .authenticationProvider = authenticationProvider ;
34
52
this .jwtAuthenticationFilter = jwtAuthenticationFilter ;
35
53
}
36
54
37
-
38
55
@ Bean
39
56
public SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
40
57
http .csrf (AbstractHttpConfigurer ::disable )
41
58
.authorizeHttpRequests (auth -> auth
42
- .requestMatchers ("/api/auth/**" ,"/" , "/swagger-ui/**" , "/v3/api-docs/**" , "/swagger-ui.html" , "/webjars/**" , "/swagger-resources/**" ).permitAll ()
59
+ .requestMatchers (SWAGGER_UI ).permitAll ()
60
+ .requestMatchers (HttpMethod .OPTIONS , "/api/**" ).permitAll ()
61
+ .requestMatchers (HttpMethod .GET , API_GYM , API_GYMS , API_USER ).permitAll ()
62
+ .requestMatchers (HttpMethod .POST , API_AUTH ).permitAll ()
63
+ .requestMatchers (HttpMethod .POST , API_GYM ).hasAnyRole (ADMIN , USER )
64
+ .requestMatchers (HttpMethod .PUT , API_USER ).hasAnyRole (ADMIN , USER )
65
+ .requestMatchers (HttpMethod .POST , API_GYMS ).hasRole (ADMIN )
66
+ .requestMatchers (HttpMethod .PUT , API_GYMS , API_GYM ).hasRole (ADMIN )
43
67
.requestMatchers (HttpMethod .DELETE , API_GYMS , API_GYM , API_USER ).hasRole (ADMIN )
44
- .requestMatchers (HttpMethod .POST , API_GYMS , API_GYM ).hasRole (ADMIN )
45
- .requestMatchers (HttpMethod .PUT , API_GYMS , API_GYM , API_USER ).hasRole (ADMIN )
46
- .requestMatchers (HttpMethod .GET , API_GYMS , API_GYM , API_USER ).permitAll ()
47
- .anyRequest ()
48
- .authenticated ())
68
+ .anyRequest ().fullyAuthenticated ())
49
69
.sessionManagement (session -> session .sessionCreationPolicy (SessionCreationPolicy .STATELESS ))
50
70
.authenticationProvider (authenticationProvider )
51
71
.addFilterBefore (jwtAuthenticationFilter , UsernamePasswordAuthenticationFilter .class );
@@ -56,9 +76,18 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
56
76
public void addCorsMappings (CorsRegistry registry ) {
57
77
registry .addMapping ("/api/**" )
58
78
.allowedOriginPatterns (allowedOrigin )
59
- .allowedMethods ("GET" , "POST" , "PUT" , "DELETE" , "OPTIONS" )
60
- .allowedHeaders ("*" )
79
+ .allowedMethods (allowedMethods )
80
+ .allowedHeaders (allowedHeaders )
61
81
.allowCredentials (true );
62
82
}
83
+
84
+ @ Override
85
+ public void addViewControllers (ViewControllerRegistry registry ) {
86
+ registry .addRedirectViewController ("/" , SWAGGER_UI_HTML );
87
+ registry .addRedirectViewController ("/swagger" , SWAGGER_UI_HTML );
88
+ registry .addRedirectViewController ("/swagger-ui" , SWAGGER_UI_HTML );
89
+ registry .addRedirectViewController ("/swagger-ui/" , SWAGGER_UI_HTML );
90
+ }
91
+
63
92
}
64
93
0 commit comments