getDomain() {
+ Authentication auth = SecurityContextHolder.getContext()
+ .getAuthentication();
+ String domain = null;
+ if (auth != null && !auth.getClass().equals(AnonymousAuthenticationToken.class)) {
+ User user = (User) auth.getPrincipal();
+ domain = user.getDomain();
+ }
+ return Optional.ofNullable(domain);
+ }
+}
diff --git a/spring-5-security/src/main/resources/application-extrafields.properties b/spring-5-security/src/main/resources/application-extrafields.properties
new file mode 100644
index 0000000000..ab4134ce3e
--- /dev/null
+++ b/spring-5-security/src/main/resources/application-extrafields.properties
@@ -0,0 +1 @@
+spring.thymeleaf.prefix = classpath:/templatesextrafields/
\ No newline at end of file
diff --git a/spring-5-security/src/main/resources/static/css/main.css b/spring-5-security/src/main/resources/static/css/main.css
new file mode 100644
index 0000000000..9299ee6158
--- /dev/null
+++ b/spring-5-security/src/main/resources/static/css/main.css
@@ -0,0 +1,18 @@
+body {
+ font-family: sans;
+ font-size: 1em;
+}
+
+p.error {
+ font-weight: bold;
+ color: red;
+}
+
+div.logout {
+ float: right;
+}
+
+.formfield {
+ margin: 0.5em;
+ padding: 0.3em;
+}
\ No newline at end of file
diff --git a/spring-5-security/src/main/resources/templatesextrafields/index.html b/spring-5-security/src/main/resources/templatesextrafields/index.html
new file mode 100644
index 0000000000..52f6224dfb
--- /dev/null
+++ b/spring-5-security/src/main/resources/templatesextrafields/index.html
@@ -0,0 +1,24 @@
+
+
+
+ Spring Security - Login With Extra Fields
+
+
+
+
+
+ Logged in user:
|
+ domain:
Some Domain
+
+
+
+
+ Hello Spring Security
+ This is an unsecured page, but you can access the secured pages after authenticating.
+
+
+
diff --git a/spring-5-security/src/main/resources/templatesextrafields/login.html b/spring-5-security/src/main/resources/templatesextrafields/login.html
new file mode 100644
index 0000000000..cafec89c15
--- /dev/null
+++ b/spring-5-security/src/main/resources/templatesextrafields/login.html
@@ -0,0 +1,23 @@
+
+
+
+ Login page
+
+
+
+
+ Login page
+ Example: user / domain / password
+ Invalid user, password, or domain
+
+ Back to home page
+
+
diff --git a/spring-5-security/src/main/resources/templatesextrafields/user/index.html b/spring-5-security/src/main/resources/templatesextrafields/user/index.html
new file mode 100644
index 0000000000..a4c1535100
--- /dev/null
+++ b/spring-5-security/src/main/resources/templatesextrafields/user/index.html
@@ -0,0 +1,13 @@
+
+
+
+ Spring Security - Login With Extra Fields
+
+
+
+
+
+ This is a secured page!
+ Back to home page
+
+
diff --git a/spring-5-security/src/test/java/com/baeldung/securityextrafields/SecurityExtraFieldsTest.java b/spring-5-security/src/test/java/com/baeldung/securityextrafields/SecurityExtraFieldsTest.java
new file mode 100644
index 0000000000..cf0701708d
--- /dev/null
+++ b/spring-5-security/src/test/java/com/baeldung/securityextrafields/SecurityExtraFieldsTest.java
@@ -0,0 +1,103 @@
+package com.baeldung.securityextrafields;
+
+import static org.junit.Assert.assertEquals;
+import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
+import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrlPattern;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.mock.web.MockHttpSession;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.GrantedAuthority;
+import org.springframework.security.core.context.SecurityContext;
+import org.springframework.security.web.FilterChainProxy;
+import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
+import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
+import org.springframework.test.context.junit4.SpringRunner;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.MvcResult;
+import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+import org.springframework.web.context.WebApplicationContext;
+
+@RunWith(SpringRunner.class)
+@SpringJUnitWebConfig
+@SpringBootTest(classes = SpringExtraLoginFieldsApplication.class)
+public class SecurityExtraFieldsTest {
+
+ @Autowired
+ private FilterChainProxy springSecurityFilterChain;
+
+ @Autowired
+ private WebApplicationContext wac;
+
+ private MockMvc mockMvc;
+
+ @Before
+ public void setup() {
+ this.mockMvc = MockMvcBuilders.webAppContextSetup(wac)
+ .apply(springSecurity(springSecurityFilterChain)).build();
+ }
+
+ @Test
+ public void givenRootPathAccess_thenRedirectToIndex() throws Exception {
+ this.mockMvc.perform(get("/"))
+ .andExpect(status().is3xxRedirection())
+ .andExpect(redirectedUrlPattern("/index*"));
+ }
+
+ @Test
+ public void givenSecuredResource_whenAccessUnauthenticated_thenRequiresAuthentication() throws Exception {
+ this.mockMvc.perform(get("/user/index"))
+ .andExpect(status().is3xxRedirection())
+ .andExpect(redirectedUrlPattern("**/login"));
+ }
+
+ @Test
+ public void givenAccessSecuredResource_whenAuthenticated_thenAuthHasExtraFields() throws Exception {
+ MockHttpServletRequestBuilder securedResourceAccess = get("/user/index");
+ MvcResult unauthenticatedResult = mockMvc.perform(securedResourceAccess)
+ .andExpect(status().is3xxRedirection())
+ .andReturn();
+
+ MockHttpSession session = (MockHttpSession) unauthenticatedResult.getRequest()
+ .getSession();
+ String loginUrl = unauthenticatedResult.getResponse()
+ .getRedirectedUrl();
+
+ User user = getUser();
+
+ mockMvc.perform(post(loginUrl)
+ .param("username", user.getUsername())
+ .param("password", user.getPassword())
+ .param("domain", user.getDomain())
+ .session(session)
+ .with(csrf()))
+ .andExpect(status().is3xxRedirection())
+ .andExpect(redirectedUrlPattern("**/user/index"))
+ .andReturn();
+
+ mockMvc.perform(securedResourceAccess.session(session))
+ .andExpect(status().isOk());
+
+ SecurityContext securityContext
+ = (SecurityContext) session.getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
+ Authentication auth = securityContext.getAuthentication();
+ assertEquals(((User)auth.getPrincipal()).getDomain(), user.getDomain());
+ }
+
+ private User getUser() {
+ Collection extends GrantedAuthority> authorities = new ArrayList<>();
+ return new User("myusername", "mydomain", "password", true, true, true, true, authorities);
+ }
+}