11package com .github .throyer .common .springboot .configurations ;
22
3+ import static com .github .throyer .common .springboot .constants .SECURITY .ACESSO_NEGADO_URL ;
4+ import static com .github .throyer .common .springboot .constants .SECURITY .DAY_MILLISECONDS ;
5+ import static com .github .throyer .common .springboot .constants .SECURITY .HOME_URL ;
6+ import static com .github .throyer .common .springboot .constants .SECURITY .LOGIN_ERROR_URL ;
7+ import static com .github .throyer .common .springboot .constants .SECURITY .LOGIN_URL ;
8+ import static com .github .throyer .common .springboot .constants .SECURITY .LOGOUT_URL ;
9+ import static com .github .throyer .common .springboot .constants .SECURITY .PASSWORD_PARAMETER ;
10+ import static com .github .throyer .common .springboot .constants .SECURITY .PUBLIC_API_ROUTES ;
11+ import static com .github .throyer .common .springboot .constants .SECURITY .SESSION_COOKIE_NAME ;
12+ import static com .github .throyer .common .springboot .constants .SECURITY .STATIC_FILES ;
13+ import static com .github .throyer .common .springboot .constants .SECURITY .TOKEN_SECRET ;
14+ import static com .github .throyer .common .springboot .constants .SECURITY .USERNAME_PARAMETER ;
15+ import static com .github .throyer .common .springboot .utils .Responses .forbidden ;
16+ import static org .springframework .http .HttpMethod .GET ;
17+ import static org .springframework .http .HttpMethod .POST ;
18+ import static org .springframework .security .config .Customizer .withDefaults ;
19+ import static org .springframework .security .config .http .SessionCreationPolicy .STATELESS ;
20+
321import com .github .throyer .common .springboot .domain .session .service .SessionService ;
422import com .github .throyer .common .springboot .middlewares .AuthorizationMiddleware ;
23+
524import org .springframework .beans .factory .annotation .Autowired ;
625import org .springframework .context .annotation .Bean ;
726import org .springframework .context .annotation .Configuration ;
827import org .springframework .core .annotation .Order ;
9- import org .springframework .security .authentication .AuthenticationManager ;
10- import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
1128import org .springframework .security .config .annotation .method .configuration .EnableGlobalMethodSecurity ;
1229import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
13- import org .springframework .security .config .annotation .web .builders .WebSecurity ;
1430import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
15- import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
31+ import org .springframework .security .config .annotation .web .configuration .WebSecurityCustomizer ;
32+ import org .springframework .security .web .SecurityFilterChain ;
1633import org .springframework .security .web .authentication .UsernamePasswordAuthenticationFilter ;
1734import org .springframework .security .web .util .matcher .AntPathRequestMatcher ;
1835import org .springframework .stereotype .Component ;
1936import org .springframework .web .cors .CorsConfiguration ;
2037
21- import static com .github .throyer .common .springboot .constants .SECURITY .*;
22- import static com .github .throyer .common .springboot .utils .Responses .forbidden ;
23- import static org .springframework .http .HttpMethod .GET ;
24- import static org .springframework .http .HttpMethod .POST ;
25- import static org .springframework .security .config .http .SessionCreationPolicy .STATELESS ;
26-
2738@ Component
39+ @ Configuration
2840@ EnableWebSecurity
2941@ EnableGlobalMethodSecurity (prePostEnabled = true )
3042public class SpringSecurityConfiguration {
@@ -41,96 +53,75 @@ public SpringSecurityConfiguration(
4153 this .filter = filter ;
4254 }
4355
44- @ Order (1 )
45- @ Configuration
46- public class Api extends WebSecurityConfigurerAdapter {
47-
48- @ Override
49- protected void configure (AuthenticationManagerBuilder auth ) throws Exception {
50- auth .userDetailsService (sessionService )
51- .passwordEncoder (PASSWORD_ENCODER );
52- }
53-
54- @ Override
55- protected void configure (HttpSecurity http ) throws Exception {
56- PUBLIC_API_ROUTES .injectOn (http );
56+ @ Bean
57+ public WebSecurityCustomizer webSecurityCustomizer () {
58+ return (web ) -> web .ignoring ().antMatchers (STATIC_FILES );
59+ }
5760
58- http
59- .antMatcher ("/api/**" )
60- .authorizeRequests ()
61- .anyRequest ()
62- .authenticated ()
63- .and ()
64- .csrf ()
65- .disable ()
66- .exceptionHandling ()
67- .authenticationEntryPoint ((request , response , exception ) -> forbidden (response ))
68- .and ()
69- .sessionManagement ()
70- .sessionCreationPolicy (STATELESS )
71- .and ()
72- .addFilterBefore (filter , UsernamePasswordAuthenticationFilter .class )
73- .cors ()
74- .configurationSource (request -> new CorsConfiguration ().applyPermitDefaultValues ());
75- }
61+ @ Bean
62+ @ Order (1 )
63+ public SecurityFilterChain api (HttpSecurity http ) throws Exception {
64+ PUBLIC_API_ROUTES .injectOn (http );
7665
77- @ Override
78- public void configure (WebSecurity web ) {
79- web
80- .ignoring ()
81- .antMatchers (STATIC_FILES );
82- }
66+ http
67+ .httpBasic (withDefaults ())
68+ .antMatcher ("/api/**" )
69+ .authorizeRequests ()
70+ .anyRequest ()
71+ .authenticated ()
72+ .and ()
73+ .csrf ()
74+ .disable ()
75+ .exceptionHandling ()
76+ .authenticationEntryPoint ((request , response , exception ) -> forbidden (response ))
77+ .and ()
78+ .userDetailsService (sessionService )
79+ .sessionManagement ()
80+ .sessionCreationPolicy (STATELESS )
81+ .and ()
82+ .addFilterBefore (filter , UsernamePasswordAuthenticationFilter .class )
83+ .cors ()
84+ .configurationSource (request -> new CorsConfiguration ().applyPermitDefaultValues ());
8385
84- @ Bean
85- @ Override
86- protected AuthenticationManager authenticationManager () throws Exception {
87- return super .authenticationManager ();
88- }
86+ return http .build ();
8987 }
9088
89+ @ Bean
9190 @ Order (2 )
92- @ Configuration
93- public class App extends WebSecurityConfigurerAdapter {
94- @ Override
95- protected void configure (AuthenticationManagerBuilder auth ) throws Exception {
96- auth .
97- userDetailsService (sessionService )
98- .passwordEncoder (PASSWORD_ENCODER );
99- }
100-
101- @ Override
102- protected void configure (HttpSecurity http ) throws Exception {
91+ public SecurityFilterChain app (HttpSecurity http ) throws Exception {
92+ http
93+ .antMatcher ("/app/**" )
94+ .authorizeRequests ()
95+ .antMatchers (GET , LOGIN_URL , "/app" , "/app/register" , "/app/recovery/**" )
96+ .permitAll ()
97+ .antMatchers (POST , "/app/register" , "/app/recovery/**" )
98+ .permitAll ()
99+ .anyRequest ()
100+ .authenticated ()
101+ .and ()
102+ .csrf ()
103+ .disable ()
104+ .userDetailsService (sessionService )
105+ .formLogin ()
106+ .loginPage (LOGIN_URL )
107+ .failureUrl (LOGIN_ERROR_URL )
108+ .defaultSuccessUrl (HOME_URL )
109+ .usernameParameter (USERNAME_PARAMETER )
110+ .passwordParameter (PASSWORD_PARAMETER )
111+ .and ()
112+ .rememberMe ()
113+ .userDetailsService (sessionService )
114+ .key (TOKEN_SECRET )
115+ .tokenValiditySeconds (DAY_MILLISECONDS )
116+ .and ()
117+ .logout ()
118+ .deleteCookies (SESSION_COOKIE_NAME )
119+ .logoutRequestMatcher (new AntPathRequestMatcher (LOGOUT_URL ))
120+ .logoutSuccessUrl (LOGIN_URL )
121+ .and ()
122+ .exceptionHandling ()
123+ .accessDeniedPage (ACESSO_NEGADO_URL );
103124
104- http
105- .antMatcher ("/app/**" )
106- .authorizeRequests ()
107- .antMatchers (GET , LOGIN_URL , "/app" , "/app/register" , "/app/recovery/**" )
108- .permitAll ()
109- .antMatchers (POST , "/app/register" , "/app/recovery/**" )
110- .permitAll ()
111- .anyRequest ()
112- .authenticated ()
113- .and ()
114- .csrf ()
115- .disable ()
116- .formLogin ()
117- .loginPage (LOGIN_URL )
118- .failureUrl (LOGIN_ERROR_URL )
119- .defaultSuccessUrl (HOME_URL )
120- .usernameParameter (USERNAME_PARAMETER )
121- .passwordParameter (PASSWORD_PARAMETER )
122- .and ()
123- .rememberMe ()
124- .key (TOKEN_SECRET )
125- .tokenValiditySeconds (DAY_MILLISECONDS )
126- .and ()
127- .logout ()
128- .deleteCookies (SESSION_COOKIE_NAME )
129- .logoutRequestMatcher (new AntPathRequestMatcher (LOGOUT_URL ))
130- .logoutSuccessUrl (LOGIN_URL )
131- .and ()
132- .exceptionHandling ()
133- .accessDeniedPage (ACESSO_NEGADO_URL );
134- }
125+ return http .build ();
135126 }
136127}
0 commit comments