13
13
import static com .github .throyer .common .springboot .constants .SECURITY .TOKEN_SECRET ;
14
14
import static com .github .throyer .common .springboot .constants .SECURITY .USERNAME_PARAMETER ;
15
15
import static com .github .throyer .common .springboot .utils .Responses .forbidden ;
16
- import static java .util .Optional .ofNullable ;
17
16
import static org .springframework .http .HttpMethod .GET ;
18
17
import static org .springframework .http .HttpMethod .POST ;
19
18
import static org .springframework .security .config .http .SessionCreationPolicy .STATELESS ;
20
19
21
20
import java .util .List ;
22
- import java .util .Optional ;
23
21
import java .util .stream .Stream ;
24
22
25
- import com .github .throyer .common .springboot .domain .session .service .SessionService ;
26
- import com .github .throyer .common .springboot .middlewares .AuthorizationMiddleware ;
27
-
28
23
import org .springframework .beans .factory .annotation .Autowired ;
29
24
import org .springframework .beans .factory .annotation .Value ;
30
25
import org .springframework .context .annotation .Bean ;
39
34
import org .springframework .security .web .SecurityFilterChain ;
40
35
import org .springframework .security .web .authentication .UsernamePasswordAuthenticationFilter ;
41
36
import org .springframework .security .web .util .matcher .AntPathRequestMatcher ;
42
- import org .springframework .stereotype .Component ;
43
37
import org .springframework .web .cors .CorsConfiguration ;
44
38
45
- @ Component
39
+ import com .github .throyer .common .springboot .domain .session .service .SessionService ;
40
+ import com .github .throyer .common .springboot .middlewares .AuthorizationMiddleware ;
41
+ import com .github .throyer .common .springboot .utils .Strings ;
42
+
46
43
@ Configuration
47
44
@ EnableWebSecurity
48
45
@ EnableGlobalMethodSecurity (prePostEnabled = true )
@@ -51,132 +48,130 @@ public class SpringSecurityConfiguration {
51
48
private final SessionService sessionService ;
52
49
private final AuthorizationMiddleware filter ;
53
50
54
- public static String SWAGGER_USERNAME ;
55
- public static String SWAGGER_PASSWORD ;
51
+ public static String SWAGGER_USERNAME = null ;
52
+ public static String SWAGGER_PASSWORD = null ;
56
53
57
54
@ Autowired
58
55
public SpringSecurityConfiguration (
59
56
SessionService sessionService ,
60
- AuthorizationMiddleware filter
57
+ AuthorizationMiddleware filter ,
58
+ @ Value ("${swagger.username}" ) String username ,
59
+ @ Value ("${swagger.password}" ) String password
61
60
) {
62
- this .sessionService = sessionService ;
63
- this .filter = filter ;
61
+ this .sessionService = sessionService ;
62
+ this .filter = filter ;
63
+
64
+ SpringSecurityConfiguration .SWAGGER_USERNAME = username ;
65
+ SpringSecurityConfiguration .SWAGGER_PASSWORD = password ;
64
66
}
65
67
66
68
@ Autowired
67
- protected void globalConfiguration (
68
- AuthenticationManagerBuilder authentication ,
69
- @ Value ("${swagger.username}" ) String username ,
70
- @ Value ("${swagger.password}" ) String password
71
- ) throws Exception {
72
- SpringSecurityConfiguration .SWAGGER_USERNAME = username ;
73
- SpringSecurityConfiguration .SWAGGER_PASSWORD = password ;
74
-
69
+ protected void globalConfiguration (AuthenticationManagerBuilder authentication ) throws Exception {
75
70
if (Stream
76
- .of (ofNullable ( SWAGGER_PASSWORD ), ofNullable ( SWAGGER_USERNAME ) )
77
- .allMatch (Optional :: isPresent )) {
78
-
79
- authentication
80
- .inMemoryAuthentication ()
81
- .passwordEncoder (ENCODER )
82
- .withUser (username )
83
- .password (ENCODER .encode (password ))
84
- .authorities (List .of ());
71
+ .of (SWAGGER_PASSWORD , SWAGGER_USERNAME )
72
+ .allMatch (Strings :: notNullOrBlank )) {
73
+
74
+ authentication
75
+ .inMemoryAuthentication ()
76
+ .passwordEncoder (ENCODER )
77
+ .withUser (SWAGGER_USERNAME )
78
+ .password (ENCODER .encode (SWAGGER_PASSWORD ))
79
+ .authorities (List .of ());
85
80
}
86
81
87
82
88
83
authentication
89
- .userDetailsService (sessionService )
90
- .passwordEncoder (ENCODER );
84
+ .userDetailsService (sessionService )
85
+ .passwordEncoder (ENCODER );
91
86
}
92
87
93
88
@ Bean
94
89
public AuthenticationManager authenticationManager (
95
90
AuthenticationConfiguration configuration
96
91
) throws Exception {
97
- return configuration .getAuthenticationManager ();
92
+ return configuration .getAuthenticationManager ();
98
93
}
99
94
100
95
@ Bean
101
96
@ Order (1 )
102
97
public SecurityFilterChain api (HttpSecurity http ) throws Exception {
103
- PUBLICS .injectOn (http );
104
-
105
- http
106
- .antMatcher ("/api/**" )
107
- .authorizeRequests ()
108
- .anyRequest ()
109
- .authenticated ()
110
- .and ()
111
- .csrf ()
112
- .disable ()
113
- .exceptionHandling ()
114
- .authenticationEntryPoint ((request , response , exception ) -> forbidden (response ))
115
- .and ()
116
- .sessionManagement ()
117
- .sessionCreationPolicy (STATELESS )
118
- .and ()
119
- .addFilterBefore (filter , UsernamePasswordAuthenticationFilter .class )
120
- .cors ()
121
- .configurationSource (request -> new CorsConfiguration ().applyPermitDefaultValues ());
122
-
123
- return http .build ();
98
+ PUBLICS .injectOn (http );
99
+
100
+ http
101
+ .antMatcher ("/api/**" )
102
+ .authorizeRequests ()
103
+ .anyRequest ()
104
+ .authenticated ()
105
+ .and ()
106
+ .csrf ()
107
+ .disable ()
108
+ .exceptionHandling ()
109
+ .authenticationEntryPoint ((request , response , exception ) -> forbidden (response ))
110
+ .and ()
111
+ .sessionManagement ()
112
+ .sessionCreationPolicy (STATELESS )
113
+ .and ()
114
+ .addFilterBefore (filter , UsernamePasswordAuthenticationFilter .class )
115
+ .cors ()
116
+ .configurationSource (request -> new CorsConfiguration ().applyPermitDefaultValues ());
117
+
118
+ return http .build ();
124
119
}
125
120
126
121
@ Bean
127
122
@ Order (2 )
128
123
public SecurityFilterChain app (HttpSecurity http ) throws Exception {
129
- http
130
- .antMatcher ("/app/**" )
131
- .authorizeRequests ()
132
- .antMatchers (GET , LOGIN_URL , "/app" , "/app/register" , "/app/recovery/**" )
133
- .permitAll ()
134
- .antMatchers (POST , "/app/register" , "/app/recovery/**" )
135
- .permitAll ()
136
- .anyRequest ()
137
- . hasAuthority ( "USER" )
138
- .and ()
139
- .csrf ()
140
- .disable ()
141
- .formLogin ()
142
- .loginPage (LOGIN_URL )
143
- .failureUrl (LOGIN_ERROR_URL )
144
- .defaultSuccessUrl (HOME_URL )
145
- .usernameParameter (USERNAME_PARAMETER )
146
- .passwordParameter (PASSWORD_PARAMETER )
147
- .and ()
148
- .rememberMe ()
149
- .key (TOKEN_SECRET )
150
- .tokenValiditySeconds (DAY_MILLISECONDS )
151
- .and ()
152
- .logout ()
153
- .deleteCookies (SESSION_COOKIE_NAME )
154
- .logoutRequestMatcher (new AntPathRequestMatcher (LOGOUT_URL ))
155
- .logoutSuccessUrl (LOGIN_URL )
156
- .and ()
157
- .exceptionHandling ()
158
- .accessDeniedPage (ACESSO_NEGADO_URL );
159
-
160
- return http .build ();
124
+ http
125
+ .antMatcher ("/app/**" )
126
+ .authorizeRequests ()
127
+ .antMatchers (GET , LOGIN_URL , "/app" , "/app/register" , "/app/recovery/**" )
128
+ .permitAll ()
129
+ .antMatchers (POST , "/app/register" , "/app/recovery/**" )
130
+ .permitAll ()
131
+ .anyRequest ()
132
+ . hasAuthority ( "USER" )
133
+ .and ()
134
+ .csrf ()
135
+ .disable ()
136
+ .formLogin ()
137
+ .loginPage (LOGIN_URL )
138
+ .failureUrl (LOGIN_ERROR_URL )
139
+ .defaultSuccessUrl (HOME_URL )
140
+ .usernameParameter (USERNAME_PARAMETER )
141
+ .passwordParameter (PASSWORD_PARAMETER )
142
+ .and ()
143
+ .rememberMe ()
144
+ .key (TOKEN_SECRET )
145
+ .tokenValiditySeconds (DAY_MILLISECONDS )
146
+ .and ()
147
+ .logout ()
148
+ .deleteCookies (SESSION_COOKIE_NAME )
149
+ .logoutRequestMatcher (new AntPathRequestMatcher (LOGOUT_URL ))
150
+ .logoutSuccessUrl (LOGIN_URL )
151
+ .and ()
152
+ .exceptionHandling ()
153
+ .accessDeniedPage (ACESSO_NEGADO_URL );
154
+
155
+ return http .build ();
161
156
}
162
157
163
158
@ Bean
164
159
@ Order (4 )
165
160
public SecurityFilterChain swagger (HttpSecurity http ) throws Exception {
166
- if (Stream
167
- .of (ofNullable ( SWAGGER_PASSWORD ), ofNullable ( SWAGGER_USERNAME ) )
168
- .allMatch (Optional :: isPresent )) {
161
+ if (Stream
162
+ .of (SWAGGER_PASSWORD , SWAGGER_USERNAME )
163
+ .allMatch (Strings :: notNullOrBlank )) {
169
164
170
165
http
171
- .antMatcher ("/swagger-ui/**" )
172
- .authorizeRequests ()
173
- .anyRequest ()
174
- .authenticated ()
175
- .and ()
176
- .sessionManagement ()
177
- .sessionCreationPolicy (STATELESS )
178
- .and ()
179
- .httpBasic ();
166
+ .antMatcher ("/swagger-ui/**" )
167
+ .authorizeRequests ()
168
+ .anyRequest ()
169
+ .authenticated ()
170
+ .and ()
171
+ .sessionManagement ()
172
+ .sessionCreationPolicy (STATELESS )
173
+ .and ()
174
+ .httpBasic ();
180
175
}
181
176
182
177
return http .build ();
0 commit comments