2
2
3
3
import static com .github .throyer .common .springboot .constants .SECURITY .ACESSO_NEGADO_URL ;
4
4
import static com .github .throyer .common .springboot .constants .SECURITY .DAY_MILLISECONDS ;
5
+ import static com .github .throyer .common .springboot .constants .SECURITY .ENCODER ;
5
6
import static com .github .throyer .common .springboot .constants .SECURITY .HOME_URL ;
6
7
import static com .github .throyer .common .springboot .constants .SECURITY .LOGIN_ERROR_URL ;
7
8
import static com .github .throyer .common .springboot .constants .SECURITY .LOGIN_URL ;
8
9
import static com .github .throyer .common .springboot .constants .SECURITY .LOGOUT_URL ;
9
- import static com .github .throyer .common .springboot .constants .SECURITY .PASSWORD_ENCODER ;
10
10
import static com .github .throyer .common .springboot .constants .SECURITY .PASSWORD_PARAMETER ;
11
- import static com .github .throyer .common .springboot .constants .SECURITY .PRIVATE_SWAGGER ;
12
- import static com .github .throyer .common .springboot .constants .SECURITY .PUBLIC_API_ROUTES ;
11
+ import static com .github .throyer .common .springboot .constants .SECURITY .PUBLICS ;
13
12
import static com .github .throyer .common .springboot .constants .SECURITY .SESSION_COOKIE_NAME ;
14
13
import static com .github .throyer .common .springboot .constants .SECURITY .TOKEN_SECRET ;
15
14
import static com .github .throyer .common .springboot .constants .SECURITY .USERNAME_PARAMETER ;
16
15
import static com .github .throyer .common .springboot .utils .Responses .forbidden ;
16
+ import static java .util .Optional .ofNullable ;
17
17
import static org .springframework .http .HttpMethod .GET ;
18
18
import static org .springframework .http .HttpMethod .POST ;
19
19
import static org .springframework .security .config .http .SessionCreationPolicy .STATELESS ;
20
20
21
+ import java .util .Optional ;
22
+ import java .util .stream .Stream ;
23
+
21
24
import com .github .throyer .common .springboot .domain .session .service .SessionService ;
22
25
import com .github .throyer .common .springboot .middlewares .AuthorizationMiddleware ;
23
26
24
27
import org .springframework .beans .factory .annotation .Autowired ;
28
+ import org .springframework .beans .factory .annotation .Value ;
25
29
import org .springframework .context .annotation .Bean ;
26
30
import org .springframework .context .annotation .Configuration ;
27
31
import org .springframework .core .annotation .Order ;
@@ -46,6 +50,9 @@ public class SpringSecurityConfiguration {
46
50
private final SessionService sessionService ;
47
51
private final AuthorizationMiddleware filter ;
48
52
53
+ public static String SWAGGER_USERNAME ;
54
+ public static String SWAGGER_PASSWORD ;
55
+
49
56
@ Autowired
50
57
public SpringSecurityConfiguration (
51
58
SessionService sessionService ,
@@ -57,11 +64,29 @@ public SpringSecurityConfiguration(
57
64
58
65
@ Autowired
59
66
protected void globalConfiguration (
60
- AuthenticationManagerBuilder authentication
67
+ AuthenticationManagerBuilder authentication ,
68
+ @ Value ("${swagger.username}" ) String username ,
69
+ @ Value ("${swagger.password}" ) String password
61
70
) throws Exception {
71
+ SpringSecurityConfiguration .SWAGGER_USERNAME = username ;
72
+ SpringSecurityConfiguration .SWAGGER_PASSWORD = password ;
73
+
74
+ if (Stream
75
+ .of (ofNullable (SWAGGER_PASSWORD ), ofNullable (SWAGGER_USERNAME ))
76
+ .allMatch (Optional ::isPresent )) {
77
+
78
+ authentication
79
+ .inMemoryAuthentication ()
80
+ .passwordEncoder (ENCODER )
81
+ .withUser (username )
82
+ .password (ENCODER .encode (password ))
83
+ .authorities ("SWAGGER" );
84
+ }
85
+
86
+
62
87
authentication
63
88
.userDetailsService (sessionService )
64
- .passwordEncoder (PASSWORD_ENCODER );
89
+ .passwordEncoder (ENCODER );
65
90
}
66
91
67
92
@ Bean
@@ -74,7 +99,7 @@ public AuthenticationManager authenticationManager(
74
99
@ Bean
75
100
@ Order (1 )
76
101
public SecurityFilterChain api (HttpSecurity http ) throws Exception {
77
- PUBLIC_API_ROUTES .injectOn (http );
102
+ PUBLICS .injectOn (http );
78
103
79
104
http
80
105
.antMatcher ("/api/**" )
@@ -137,19 +162,20 @@ public SecurityFilterChain app(HttpSecurity http) throws Exception {
137
162
@ Bean
138
163
@ Order (4 )
139
164
public SecurityFilterChain swagger (HttpSecurity http ) throws Exception {
165
+ if (Stream
166
+ .of (ofNullable (SWAGGER_PASSWORD ), ofNullable (SWAGGER_USERNAME ))
167
+ .allMatch (Optional ::isPresent )) {
140
168
141
- if (PRIVATE_SWAGGER ) {
142
169
http
143
- .authorizeRequests ()
144
- .antMatchers ("/swagger-ui/**" , "/swagger-ui.html" , "/**.html" , "/documentation/**" )
170
+ .antMatcher ("/swagger-ui/**" )
171
+ .authorizeRequests ()
172
+ .anyRequest ()
145
173
.authenticated ()
174
+ .and ()
175
+ .sessionManagement ()
176
+ .sessionCreationPolicy (STATELESS )
146
177
.and ()
147
178
.httpBasic ();
148
- } else {
149
- http
150
- .authorizeRequests ()
151
- .antMatchers ("/swagger-ui/**" , "/swagger-ui.html" , "/**.html" , "/documentation/**" )
152
- .permitAll ();
153
179
}
154
180
155
181
return http .build ();
0 commit comments