Skip to content

Commit df54ae4

Browse files
committed
Updates
1 parent f2e38be commit df54ae4

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

spring-security/getting-started/SecureApplication/src/main/java/com/reflectoring/security/config/SecurityConfiguration.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.springframework.security.config.Customizer;
66
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
77
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
8+
import org.springframework.security.config.http.SessionCreationPolicy;
89
import org.springframework.security.web.SecurityFilterChain;
910
import 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
}

spring-security/getting-started/SecureApplication/src/main/java/com/reflectoring/security/web/HomeController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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>

0 commit comments

Comments
 (0)