Skip to content

Commit f2e38be

Browse files
committed
Updates to usecases
1 parent ec0cca1 commit f2e38be

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,48 @@
22

33
import org.springframework.context.annotation.Bean;
44
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.security.config.Customizer;
56
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
67
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
78
import org.springframework.security.web.SecurityFilterChain;
9+
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
810

911
@Configuration
1012
@EnableWebSecurity
1113
public class SecurityConfiguration {
14+
15+
public static final String[] ENDPOINTS_WHITELIST = {
16+
"/css/**",
17+
"/",
18+
"/login",
19+
"/home"
20+
};
21+
public static final String LOGIN_URL = "/login";
22+
public static final String LOGIN_FAIL_URL = LOGIN_URL + "?error";
23+
public static final String DEFAULT_SUCCESS_URL = "/home";
24+
public static final String USERNAME = "username";
25+
public static final String PASSWORD = "password";
26+
1227
@Bean
1328
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
14-
http.formLogin()
15-
.defaultSuccessUrl("/home", true);
29+
http.authorizeRequests(request -> request.antMatchers(ENDPOINTS_WHITELIST).permitAll()
30+
.anyRequest().authenticated())
31+
.csrf().disable()
32+
//.formLogin(Customizer.withDefaults())
33+
.formLogin(form -> form
34+
.loginPage(LOGIN_URL)
35+
.loginProcessingUrl(LOGIN_URL)
36+
.failureUrl(LOGIN_FAIL_URL)
37+
.usernameParameter(USERNAME)
38+
.passwordParameter(PASSWORD)
39+
.defaultSuccessUrl(DEFAULT_SUCCESS_URL))
40+
//.logout(Customizer.withDefaults())
41+
.logout(logout -> logout
42+
.logoutUrl("/logout")
43+
.invalidateHttpSession(true)
44+
.deleteCookies("JSESSIONID")
45+
.logoutSuccessUrl(LOGIN_URL + "?logout"));
1646
return http.build();
1747
}
1848
}
49+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.reflectoring.security.web;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
6+
@Controller
7+
public class LoginController {
8+
9+
@GetMapping("/login")
10+
String login() {
11+
return "login";
12+
}
13+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
3+
<head>
4+
<title>Please Log In</title>
5+
</head>
6+
<body>
7+
<h1>Please Log In</h1>
8+
<div th:if="${param.error}">
9+
Invalid username and password.</div>
10+
<div th:if="${param.logout}">
11+
You have been logged out.</div>
12+
<form th:action="@{/login}" method="post">
13+
<div>
14+
<input type="text" name="username" placeholder="Username"/>
15+
</div>
16+
<div>
17+
<input type="password" name="password" placeholder="Password"/>
18+
</div>
19+
<input type="submit" value="Log in" />
20+
</form>
21+
</body>
22+
</html>
23+

0 commit comments

Comments
 (0)