File tree Expand file tree Collapse file tree 3 files changed +27
-1
lines changed
spring-security/getting-started/SecureApplication/src/main
java/com/reflectoring/security Expand file tree Collapse file tree 3 files changed +27
-1
lines changed Original file line number Diff line number Diff line change 55import org .springframework .security .config .Customizer ;
66import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
77import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
8+ import org .springframework .security .config .http .SessionCreationPolicy ;
89import org .springframework .security .web .SecurityFilterChain ;
910import org .springframework .security .web .util .matcher .AntPathRequestMatcher ;
1011
@@ -26,8 +27,10 @@ public class SecurityConfiguration {
2627
2728 @ Bean
2829 public SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
30+ // Requests
2931 http .authorizeRequests (request -> request .antMatchers (ENDPOINTS_WHITELIST ).permitAll ()
3032 .anyRequest ().authenticated ())
33+ // CSRF
3134 .csrf ().disable ()
3235 //.formLogin(Customizer.withDefaults())
3336 .formLogin (form -> form
@@ -42,7 +45,15 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
4245 .logoutUrl ("/logout" )
4346 .invalidateHttpSession (true )
4447 .deleteCookies ("JSESSIONID" )
45- .logoutSuccessUrl (LOGIN_URL + "?logout" ));
48+ .logoutSuccessUrl (LOGIN_URL + "?logout" ))
49+ //.sessionManagement(Customizer.withDefaults())
50+ .sessionManagement (session -> session
51+ .sessionCreationPolicy (SessionCreationPolicy .ALWAYS )
52+ .invalidSessionUrl ("/invalidSession" )
53+ .maximumSessions (1 )
54+ .maxSessionsPreventsLogin (true ));
55+
56+
4657 return http .build ();
4758 }
4859}
Original file line number Diff line number Diff line change @@ -12,4 +12,9 @@ public class HomeController {
1212 public String homePage (HttpServletResponse response ) {
1313 return "homePage" ;
1414 }
15+
16+ @ GetMapping ("/invalidSession" )
17+ public String invalidSession (HttpServletResponse response ) {
18+ return "invalidSession" ;
19+ }
1520}
Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html lang ="en " xmlns:th ="http://www.thymeleaf.org ">
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < title > Title</ title >
6+ </ head >
7+ < body >
8+ < h2 > Invalid Session</ h2 >
9+ </ body >
10+ </ html >
You can’t perform that action at this time.
0 commit comments