Skip to content

Commit 87cce8d

Browse files
authored
Merge pull request eugenp#12022 from anuragkumawat/master
JAVA-8360 Split or move spring-5-security module
2 parents 7608c35 + b99071a commit 87cce8d

33 files changed

Lines changed: 883 additions & 842 deletions

spring-security-modules/spring-5-security/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ This module contains articles about Spring Security 5
44

55
## Relevant articles:
66

7-
- [Extra Login Fields with Spring Security](https://www.baeldung.com/spring-security-extra-login-fields)
87
- [A Custom Spring SecurityConfigurer](https://www.baeldung.com/spring-security-custom-configurer)
98
- [New Password Storage In Spring Security 5](https://www.baeldung.com/spring-security-5-password-storage)
109
- [Default Password Encoder in Spring Security 5](https://www.baeldung.com/spring-security-5-default-password-encoder)

spring-security-modules/spring-security-web-login/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The "Learn Spring Security" Classes: http://github.learnspringsecurity.com
1212
- [Spring Security – Customize the 403 Forbidden/Access Denied Page](https://www.baeldung.com/spring-security-custom-access-denied-page)
1313
- [Spring Security – Redirect to the Previous URL After Login](https://www.baeldung.com/spring-security-redirect-login)
1414
- [Spring Security Custom AuthenticationFailureHandler](https://www.baeldung.com/spring-security-custom-authentication-failure-handler)
15+
- [Extra Login Fields with Spring Security](https://www.baeldung.com/spring-security-extra-login-fields)
1516

1617
### Build the Project
1718
```

spring-security-modules/spring-security-web-login/pom.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,26 @@
1616
</parent>
1717

1818
<dependencies>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-web</artifactId>
22+
<version>${spring-boot.version}</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-security</artifactId>
27+
<version>${spring-boot.version}</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
32+
<version>${spring-boot.version}</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.thymeleaf.extras</groupId>
36+
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
37+
<version>${thymeleaf-extras-springsecurity5.version}</version>
38+
</dependency>
1939
<!-- Spring Security -->
2040
<dependency>
2141
<groupId>org.springframework.security</groupId>
@@ -98,6 +118,12 @@
98118
<scope>runtime</scope>
99119
</dependency>
100120
<!-- test scoped -->
121+
<dependency>
122+
<groupId>org.springframework.boot</groupId>
123+
<artifactId>spring-boot-starter-test</artifactId>
124+
<version>${spring-boot.version}</version>
125+
<scope>test</scope>
126+
</dependency>
101127
<dependency>
102128
<groupId>org.springframework</groupId>
103129
<artifactId>spring-test</artifactId>
@@ -110,6 +136,11 @@
110136
<version>${spring-security.version}</version>
111137
<scope>test</scope>
112138
</dependency>
139+
<dependency>
140+
<groupId>org.apache.commons</groupId>
141+
<artifactId>commons-lang3</artifactId>
142+
<version>${commons-lang3.version}</version>
143+
</dependency>
113144
</dependencies>
114145

115146
<build>
@@ -153,6 +184,9 @@
153184
<properties>
154185
<!-- Maven plugins -->
155186
<cargo-maven3-plugin.version>1.9.9</cargo-maven3-plugin.version>
187+
<spring-boot.version>2.6.4</spring-boot.version>
188+
<thymeleaf-extras-springsecurity5.version>3.0.4.RELEASE</thymeleaf-extras-springsecurity5.version>
189+
<commons-lang3.version>3.11</commons-lang3.version>
156190
</properties>
157191

158192
</project>

spring-security-modules/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/CustomAuthenticationFilter.java renamed to spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/loginextrafieldscustom/CustomAuthenticationFilter.java

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
1-
package com.baeldung.loginextrafieldscustom;
2-
3-
import javax.servlet.http.HttpServletRequest;
4-
import javax.servlet.http.HttpServletResponse;
5-
6-
import org.springframework.security.authentication.AuthenticationServiceException;
7-
import org.springframework.security.core.Authentication;
8-
import org.springframework.security.core.AuthenticationException;
9-
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
10-
11-
public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
12-
13-
public static final String SPRING_SECURITY_FORM_DOMAIN_KEY = "domain";
14-
15-
@Override
16-
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
17-
throws AuthenticationException {
18-
19-
if (!request.getMethod().equals("POST")) {
20-
throw new AuthenticationServiceException("Authentication method not supported: "
21-
+ request.getMethod());
22-
}
23-
24-
CustomAuthenticationToken authRequest = getAuthRequest(request);
25-
setDetails(request, authRequest);
26-
return this.getAuthenticationManager().authenticate(authRequest);
27-
}
28-
29-
private CustomAuthenticationToken getAuthRequest(HttpServletRequest request) {
30-
String username = obtainUsername(request);
31-
String password = obtainPassword(request);
32-
String domain = obtainDomain(request);
33-
34-
if (username == null) {
35-
username = "";
36-
}
37-
if (password == null) {
38-
password = "";
39-
}
40-
if (domain == null) {
41-
domain = "";
42-
}
43-
44-
return new CustomAuthenticationToken(username, password, domain);
45-
}
46-
47-
private String obtainDomain(HttpServletRequest request) {
48-
return request.getParameter(SPRING_SECURITY_FORM_DOMAIN_KEY);
49-
}
50-
}
1+
package com.baeldung.loginextrafieldscustom;
2+
3+
import javax.servlet.http.HttpServletRequest;
4+
import javax.servlet.http.HttpServletResponse;
5+
6+
import org.springframework.security.authentication.AuthenticationServiceException;
7+
import org.springframework.security.core.Authentication;
8+
import org.springframework.security.core.AuthenticationException;
9+
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
10+
11+
public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
12+
13+
public static final String SPRING_SECURITY_FORM_DOMAIN_KEY = "domain";
14+
15+
@Override
16+
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
17+
throws AuthenticationException {
18+
19+
if (!request.getMethod().equals("POST")) {
20+
throw new AuthenticationServiceException("Authentication method not supported: "
21+
+ request.getMethod());
22+
}
23+
24+
CustomAuthenticationToken authRequest = getAuthRequest(request);
25+
setDetails(request, authRequest);
26+
return this.getAuthenticationManager().authenticate(authRequest);
27+
}
28+
29+
private CustomAuthenticationToken getAuthRequest(HttpServletRequest request) {
30+
String username = obtainUsername(request);
31+
String password = obtainPassword(request);
32+
String domain = obtainDomain(request);
33+
34+
if (username == null) {
35+
username = "";
36+
}
37+
if (password == null) {
38+
password = "";
39+
}
40+
if (domain == null) {
41+
domain = "";
42+
}
43+
44+
return new CustomAuthenticationToken(username, password, domain);
45+
}
46+
47+
private String obtainDomain(HttpServletRequest request) {
48+
return request.getParameter(SPRING_SECURITY_FORM_DOMAIN_KEY);
49+
}
50+
}

spring-security-modules/spring-5-security/src/main/java/com/baeldung/loginextrafieldscustom/CustomAuthenticationToken.java renamed to spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/loginextrafieldscustom/CustomAuthenticationToken.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
package com.baeldung.loginextrafieldscustom;
2-
3-
import java.util.Collection;
4-
5-
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
6-
import org.springframework.security.core.GrantedAuthority;
7-
8-
public class CustomAuthenticationToken extends UsernamePasswordAuthenticationToken {
9-
10-
private String domain;
11-
12-
public CustomAuthenticationToken(Object principal, Object credentials, String domain) {
13-
super(principal, credentials);
14-
this.domain = domain;
15-
super.setAuthenticated(false);
16-
}
17-
18-
public CustomAuthenticationToken(Object principal, Object credentials, String domain,
19-
Collection<? extends GrantedAuthority> authorities) {
20-
super(principal, credentials, authorities);
21-
this.domain = domain;
22-
super.setAuthenticated(true); // must use super, as we override
23-
}
24-
25-
public String getDomain() {
26-
return this.domain;
27-
}
28-
}
1+
package com.baeldung.loginextrafieldscustom;
2+
3+
import java.util.Collection;
4+
5+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
6+
import org.springframework.security.core.GrantedAuthority;
7+
8+
public class CustomAuthenticationToken extends UsernamePasswordAuthenticationToken {
9+
10+
private String domain;
11+
12+
public CustomAuthenticationToken(Object principal, Object credentials, String domain) {
13+
super(principal, credentials);
14+
this.domain = domain;
15+
super.setAuthenticated(false);
16+
}
17+
18+
public CustomAuthenticationToken(Object principal, Object credentials, String domain,
19+
Collection<? extends GrantedAuthority> authorities) {
20+
super(principal, credentials, authorities);
21+
this.domain = domain;
22+
super.setAuthenticated(true); // must use super, as we override
23+
}
24+
25+
public String getDomain() {
26+
return this.domain;
27+
}
28+
}

0 commit comments

Comments
 (0)