1313import static com .github .throyer .common .springboot .constants .SECURITY .TOKEN_SECRET ;
1414import static com .github .throyer .common .springboot .constants .SECURITY .USERNAME_PARAMETER ;
1515import static com .github .throyer .common .springboot .utils .Responses .forbidden ;
16- import static java .util .Optional .ofNullable ;
1716import static org .springframework .http .HttpMethod .GET ;
1817import static org .springframework .http .HttpMethod .POST ;
1918import static org .springframework .security .config .http .SessionCreationPolicy .STATELESS ;
2019
2120import java .util .List ;
22- import java .util .Optional ;
2321import java .util .stream .Stream ;
2422
25- import com .github .throyer .common .springboot .domain .session .service .SessionService ;
26- import com .github .throyer .common .springboot .middlewares .AuthorizationMiddleware ;
27-
2823import org .springframework .beans .factory .annotation .Autowired ;
2924import org .springframework .beans .factory .annotation .Value ;
3025import org .springframework .context .annotation .Bean ;
3934import org .springframework .security .web .SecurityFilterChain ;
4035import org .springframework .security .web .authentication .UsernamePasswordAuthenticationFilter ;
4136import org .springframework .security .web .util .matcher .AntPathRequestMatcher ;
42- import org .springframework .stereotype .Component ;
4337import org .springframework .web .cors .CorsConfiguration ;
4438
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+
4643@ Configuration
4744@ EnableWebSecurity
4845@ EnableGlobalMethodSecurity (prePostEnabled = true )
@@ -51,132 +48,130 @@ public class SpringSecurityConfiguration {
5148 private final SessionService sessionService ;
5249 private final AuthorizationMiddleware filter ;
5350
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 ;
5653
5754 @ Autowired
5855 public SpringSecurityConfiguration (
5956 SessionService sessionService ,
60- AuthorizationMiddleware filter
57+ AuthorizationMiddleware filter ,
58+ @ Value ("${swagger.username}" ) String username ,
59+ @ Value ("${swagger.password}" ) String password
6160 ) {
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 ;
6466 }
6567
6668 @ 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 {
7570 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 ());
8580 }
8681
8782
8883 authentication
89- .userDetailsService (sessionService )
90- .passwordEncoder (ENCODER );
84+ .userDetailsService (sessionService )
85+ .passwordEncoder (ENCODER );
9186 }
9287
9388 @ Bean
9489 public AuthenticationManager authenticationManager (
9590 AuthenticationConfiguration configuration
9691 ) throws Exception {
97- return configuration .getAuthenticationManager ();
92+ return configuration .getAuthenticationManager ();
9893 }
9994
10095 @ Bean
10196 @ Order (1 )
10297 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 ();
124119 }
125120
126121 @ Bean
127122 @ Order (2 )
128123 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 ();
161156 }
162157
163158 @ Bean
164159 @ Order (4 )
165160 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 )) {
169164
170165 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 ();
180175 }
181176
182177 return http .build ();
0 commit comments