From 99887d2f1b2a2ef5257b3a3dbe06eb7dd195c939 Mon Sep 17 00:00:00 2001 From: Chris Oberle Date: Tue, 16 Jan 2018 18:26:43 -0500 Subject: [PATCH] finish moving example code to spring-5-security module --- spring-5-security/pom.xml | 222 ++++---- .../securityextrafields/SecurityConfig.java | 2 +- .../SecurityExtraFieldsTest.java | 25 +- spring-5/pom.xml | 512 +++++++++--------- ...gSecurity5ExtraLoginFieldsApplication.java | 15 - .../CustomAuthenticationFilter.java | 54 -- .../CustomUserDetailsService.java | 32 -- .../CustomUserRepository.java | 26 - .../securityextrafields/SecurityConfig.java | 63 --- .../baeldung/securityextrafields/User.java | 23 - .../securityextrafields/UserRepository.java | 7 - .../securityextrafields/WebController.java | 51 -- .../src/main/resources/static/css/main.css | 18 - .../src/main/resources/templates/index.html | 24 - .../src/main/resources/templates/login.html | 23 - .../main/resources/templates/user/index.html | 13 - 16 files changed, 374 insertions(+), 736 deletions(-) rename {spring-5 => spring-5-security}/src/test/java/com/baeldung/securityextrafields/SecurityExtraFieldsTest.java (83%) delete mode 100644 spring-5/src/main/java/com/baeldung/SpringSecurity5ExtraLoginFieldsApplication.java delete mode 100644 spring-5/src/main/java/com/baeldung/securityextrafields/CustomAuthenticationFilter.java delete mode 100644 spring-5/src/main/java/com/baeldung/securityextrafields/CustomUserDetailsService.java delete mode 100644 spring-5/src/main/java/com/baeldung/securityextrafields/CustomUserRepository.java delete mode 100644 spring-5/src/main/java/com/baeldung/securityextrafields/SecurityConfig.java delete mode 100644 spring-5/src/main/java/com/baeldung/securityextrafields/User.java delete mode 100644 spring-5/src/main/java/com/baeldung/securityextrafields/UserRepository.java delete mode 100644 spring-5/src/main/java/com/baeldung/securityextrafields/WebController.java delete mode 100644 spring-5/src/main/resources/static/css/main.css delete mode 100644 spring-5/src/main/resources/templates/index.html delete mode 100644 spring-5/src/main/resources/templates/login.html delete mode 100644 spring-5/src/main/resources/templates/user/index.html diff --git a/spring-5-security/pom.xml b/spring-5-security/pom.xml index c94e921a03..9300882459 100644 --- a/spring-5-security/pom.xml +++ b/spring-5-security/pom.xml @@ -1,110 +1,114 @@ - - 4.0.0 - com.baeldung - spring-5-security - 0.0.1-SNAPSHOT - jar - - spring-5-security - spring 5 security sample project - - - org.springframework.boot - spring-boot-starter-parent - 2.0.0.M7 - - - - - - - org.springframework.boot - spring-boot-starter-security - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-thymeleaf - - - org.thymeleaf.extras - thymeleaf-extras-springsecurity4 - - - - - org.springframework.security - spring-security-oauth2-client - - - org.springframework.security - spring-security-oauth2-jose - - - - org.springframework.boot - spring-boot-starter-test - test - - - org.springframework.security - spring-security-test - test - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - org.apache.maven.plugins - maven-surefire-plugin - - 3 - true - methods - true - - **/*IntegrationTest.java - **/*LiveTest.java - - - - - - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - - - UTF-8 - UTF-8 - 1.8 - + + 4.0.0 + com.baeldung + spring-5-security + 0.0.1-SNAPSHOT + jar + + spring-5-security + spring 5 security sample project + + + org.springframework.boot + spring-boot-starter-parent + 2.0.0.M7 + + + + + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.thymeleaf.extras + thymeleaf-extras-springsecurity4 + + + + + org.springframework.security + spring-security-oauth2-client + + + org.springframework.security + spring-security-oauth2-jose + + + + org.springframework + spring-test + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.security + spring-security-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.apache.maven.plugins + maven-surefire-plugin + + 3 + true + methods + true + + **/*IntegrationTest.java + **/*LiveTest.java + + + + + + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + UTF-8 + UTF-8 + 1.8 + \ No newline at end of file diff --git a/spring-5-security/src/main/java/com/baeldung/securityextrafields/SecurityConfig.java b/spring-5-security/src/main/java/com/baeldung/securityextrafields/SecurityConfig.java index 257cc42692..affb6d436d 100644 --- a/spring-5-security/src/main/java/com/baeldung/securityextrafields/SecurityConfig.java +++ b/spring-5-security/src/main/java/com/baeldung/securityextrafields/SecurityConfig.java @@ -15,7 +15,7 @@ import org.springframework.security.web.authentication.SimpleUrlAuthenticationFa import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; @EnableWebSecurity -@PropertySource("application-extrafields.properties") +@PropertySource("classpath:/application-extrafields.properties") public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired diff --git a/spring-5/src/test/java/com/baeldung/securityextrafields/SecurityExtraFieldsTest.java b/spring-5-security/src/test/java/com/baeldung/securityextrafields/SecurityExtraFieldsTest.java similarity index 83% rename from spring-5/src/test/java/com/baeldung/securityextrafields/SecurityExtraFieldsTest.java rename to spring-5-security/src/test/java/com/baeldung/securityextrafields/SecurityExtraFieldsTest.java index 09ee0e66a0..cf0701708d 100644 --- a/spring-5/src/test/java/com/baeldung/securityextrafields/SecurityExtraFieldsTest.java +++ b/spring-5-security/src/test/java/com/baeldung/securityextrafields/SecurityExtraFieldsTest.java @@ -11,47 +11,44 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import java.util.ArrayList; import java.util.Collection; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; +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.ContextConfiguration; import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.test.context.web.WebAppConfiguration; 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; -import com.baeldung.SpringSecurity5ExtraLoginFieldsApplication; - -@WebAppConfiguration -@SpringJUnitWebConfig @RunWith(SpringRunner.class) -@ContextConfiguration(classes = SpringSecurity5ExtraLoginFieldsApplication.class) +@SpringJUnitWebConfig +@SpringBootTest(classes = SpringExtraLoginFieldsApplication.class) public class SecurityExtraFieldsTest { @Autowired private FilterChainProxy springSecurityFilterChain; + @Autowired + private WebApplicationContext wac; + private MockMvc mockMvc; - @BeforeEach - public void setup(WebApplicationContext wac) { + @Before + public void setup() { this.mockMvc = MockMvcBuilders.webAppContextSetup(wac) .apply(springSecurity(springSecurityFilterChain)).build(); } - @DisplayName("Access of root path redirects to index") @Test public void givenRootPathAccess_thenRedirectToIndex() throws Exception { this.mockMvc.perform(get("/")) @@ -59,7 +56,6 @@ public class SecurityExtraFieldsTest { .andExpect(redirectedUrlPattern("/index*")); } - @DisplayName("Unauthenticated access of secured resource redirects to login page") @Test public void givenSecuredResource_whenAccessUnauthenticated_thenRequiresAuthentication() throws Exception { this.mockMvc.perform(get("/user/index")) @@ -67,7 +63,6 @@ public class SecurityExtraFieldsTest { .andExpect(redirectedUrlPattern("**/login")); } - @DisplayName("Succesfull auth on login page redirects and extra field exists") @Test public void givenAccessSecuredResource_whenAuthenticated_thenAuthHasExtraFields() throws Exception { MockHttpServletRequestBuilder securedResourceAccess = get("/user/index"); diff --git a/spring-5/pom.xml b/spring-5/pom.xml index 71f355f41a..19dd65d78f 100644 --- a/spring-5/pom.xml +++ b/spring-5/pom.xml @@ -1,262 +1,250 @@ - - - 4.0.0 - - com.baeldung - spring-5 - 0.0.1-SNAPSHOT - jar - - spring-5 - spring 5 sample project about new features - - - org.springframework.boot - spring-boot-starter-parent - 2.0.0.M7 - - - - - - org.springframework.boot - spring-boot-starter-data-jpa - - - org.springframework.boot - spring-boot-starter-security - - - org.springframework.boot - spring-boot-starter-validation - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-thymeleaf - - - org.thymeleaf.extras - thymeleaf-extras-springsecurity4 - - - org.springframework.boot - spring-boot-starter-webflux - - - org.springframework.boot - spring-boot-starter-hateoas - - - org.projectreactor - reactor-spring - ${reactor-spring.version} - - - javax.json.bind - javax.json.bind-api - ${jsonb-api.version} - - - - - - - - - - - - - - - org.apache.geronimo.specs - geronimo-json_1.1_spec - ${geronimo-json_1.1_spec.version} - - - org.apache.johnzon - johnzon-jsonb - ${johnzon.version} - - - - org.apache.commons - commons-lang3 - - - - - - org.springframework.boot - spring-boot-devtools - runtime - - - com.h2database - h2 - runtime - - - - org.springframework - spring-test - - - org.springframework.boot - spring-boot-starter-test - test - - - org.springframework.security - spring-security-test - test - - - - org.apache.commons - commons-collections4 - 4.1 - test - - - - org.junit.jupiter - junit-jupiter-api - ${junit.jupiter.version} - - - org.junit.jupiter - junit-jupiter-engine - ${junit.jupiter.version} - test - - - org.junit.platform - junit-platform-surefire-provider - ${junit.platform.version} - test - - - org.junit.platform - junit-platform-runner - ${junit.platform.version} - test - - - - org.springframework.restdocs - spring-restdocs-mockmvc - test - - - org.springframework.restdocs - spring-restdocs-webtestclient - test - - - org.springframework.restdocs - spring-restdocs-restassured - test - - - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - com.baeldung.Spring5Application - JAR - - - - - org.apache.maven.plugins - maven-surefire-plugin - - 3 - true - methods - true - - **/*IntegrationTest.java - **/*LiveTest.java - - - - - org.asciidoctor - asciidoctor-maven-plugin - ${asciidoctor-plugin.version} - - - generate-docs - package - - process-asciidoc - - - html - book - - ${snippetsDirectory} - - src/docs/asciidocs - target/generated-docs - - - - - - - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - - - UTF-8 - UTF-8 - 1.8 - 1.0.0 - 5.0.0 - 2.20 - 5.0.2.RELEASE - 1.0.1.RELEASE - 1.1.3 - 1.0 - 1.0 - 1.5.6 - ${project.build.directory}/generated-snippets - - - + + + 4.0.0 + + com.baeldung + spring-5 + 0.0.1-SNAPSHOT + jar + + spring-5 + spring 5 sample project about new features + + + org.springframework.boot + spring-boot-starter-parent + 2.0.0.M7 + + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.boot + spring-boot-starter-validation + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-webflux + + + org.springframework.boot + spring-boot-starter-hateoas + + + org.projectreactor + reactor-spring + ${reactor-spring.version} + + + javax.json.bind + javax.json.bind-api + ${jsonb-api.version} + + + + + + + + + + + + + + + org.apache.geronimo.specs + geronimo-json_1.1_spec + ${geronimo-json_1.1_spec.version} + + + org.apache.johnzon + johnzon-jsonb + ${johnzon.version} + + + + org.apache.commons + commons-lang3 + + + + + + org.springframework.boot + spring-boot-devtools + runtime + + + com.h2database + h2 + runtime + + + + org.springframework + spring-test + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.security + spring-security-test + test + + + + org.apache.commons + commons-collections4 + 4.1 + test + + + + org.junit.jupiter + junit-jupiter-api + ${junit.jupiter.version} + + + org.junit.jupiter + junit-jupiter-engine + ${junit.jupiter.version} + test + + + org.junit.platform + junit-platform-surefire-provider + ${junit.platform.version} + test + + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + + + + org.springframework.restdocs + spring-restdocs-mockmvc + test + + + org.springframework.restdocs + spring-restdocs-webtestclient + test + + + org.springframework.restdocs + spring-restdocs-restassured + test + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + com.baeldung.Spring5Application + JAR + + + + + org.apache.maven.plugins + maven-surefire-plugin + + 3 + true + methods + true + + **/*IntegrationTest.java + **/*LiveTest.java + + + + + org.asciidoctor + asciidoctor-maven-plugin + ${asciidoctor-plugin.version} + + + generate-docs + package + + process-asciidoc + + + html + book + + ${snippetsDirectory} + + src/docs/asciidocs + target/generated-docs + + + + + + + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + UTF-8 + UTF-8 + 1.8 + 1.0.0 + 5.0.0 + 2.20 + 5.0.2.RELEASE + 1.0.1.RELEASE + 1.1.3 + 1.0 + 1.0 + 1.5.6 + ${project.build.directory}/generated-snippets + + + diff --git a/spring-5/src/main/java/com/baeldung/SpringSecurity5ExtraLoginFieldsApplication.java b/spring-5/src/main/java/com/baeldung/SpringSecurity5ExtraLoginFieldsApplication.java deleted file mode 100644 index b6285dfc71..0000000000 --- a/spring-5/src/main/java/com/baeldung/SpringSecurity5ExtraLoginFieldsApplication.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.baeldung; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.ComponentScan; - -@ComponentScan(basePackages = {"com.baeldung.securityextrafields"}) -@SpringBootApplication -public class SpringSecurity5ExtraLoginFieldsApplication { - - public static void main(String[] args) { - SpringApplication.run(SpringSecurity5ExtraLoginFieldsApplication.class, args); - } - -} diff --git a/spring-5/src/main/java/com/baeldung/securityextrafields/CustomAuthenticationFilter.java b/spring-5/src/main/java/com/baeldung/securityextrafields/CustomAuthenticationFilter.java deleted file mode 100644 index b5d628628d..0000000000 --- a/spring-5/src/main/java/com/baeldung/securityextrafields/CustomAuthenticationFilter.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.baeldung.securityextrafields; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.springframework.security.authentication.AuthenticationServiceException; -import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.AuthenticationException; -import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; - -public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFilter { - - public static final String SPRING_SECURITY_FORM_DOMAIN_KEY = "domain"; - - @Override - public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) - throws AuthenticationException { - - if (!request.getMethod() - .equals("POST")) { - throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod()); - } - - UsernamePasswordAuthenticationToken authRequest = getAuthRequest(request); - setDetails(request, authRequest); - return this.getAuthenticationManager() - .authenticate(authRequest); - } - - private UsernamePasswordAuthenticationToken getAuthRequest(HttpServletRequest request) { - String username = obtainUsername(request); - String password = obtainPassword(request); - String domain = obtainDomain(request); - - if (username == null) { - username = ""; - } - if (password == null) { - password = ""; - } - if (domain == null) { - domain = ""; - } - - String usernameDomain = String.format("%s%s%s", username.trim(), - String.valueOf(Character.LINE_SEPARATOR), domain); - return new UsernamePasswordAuthenticationToken(usernameDomain, password); - } - - private String obtainDomain(HttpServletRequest request) { - return request.getParameter(SPRING_SECURITY_FORM_DOMAIN_KEY); - } -} diff --git a/spring-5/src/main/java/com/baeldung/securityextrafields/CustomUserDetailsService.java b/spring-5/src/main/java/com/baeldung/securityextrafields/CustomUserDetailsService.java deleted file mode 100644 index be02834852..0000000000 --- a/spring-5/src/main/java/com/baeldung/securityextrafields/CustomUserDetailsService.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.securityextrafields; - -import org.apache.commons.lang3.StringUtils; -import org.springframework.security.core.userdetails.UserDetails; -import org.springframework.security.core.userdetails.UserDetailsService; -import org.springframework.security.core.userdetails.UsernameNotFoundException; -import org.springframework.stereotype.Service; - -@Service("userDetailsService") -public class CustomUserDetailsService implements UserDetailsService { - - private final UserRepository userRepository; - - public CustomUserDetailsService(UserRepository userRepository) { - this.userRepository = userRepository; - } - - @Override - public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { - String[] usernameAndDomain = StringUtils.split(username, String.valueOf(Character.LINE_SEPARATOR)); - if (usernameAndDomain == null || usernameAndDomain.length != 2) { - throw new UsernameNotFoundException("Username and domain must be provided"); - } - User user = userRepository.findUser(usernameAndDomain[0], usernameAndDomain[1]); - if (user == null) { - throw new UsernameNotFoundException( - String.format("Username not found for domain, username=%s, domain=%s", - usernameAndDomain[0], usernameAndDomain[1])); - } - return user; - } -} diff --git a/spring-5/src/main/java/com/baeldung/securityextrafields/CustomUserRepository.java b/spring-5/src/main/java/com/baeldung/securityextrafields/CustomUserRepository.java deleted file mode 100644 index c86769b016..0000000000 --- a/spring-5/src/main/java/com/baeldung/securityextrafields/CustomUserRepository.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.baeldung.securityextrafields; - -import java.util.ArrayList; -import java.util.Collection; - -import org.apache.commons.lang3.StringUtils; -import org.springframework.security.core.GrantedAuthority; -import org.springframework.stereotype.Repository; - -@Repository("userRepository") -public class CustomUserRepository implements UserRepository { - - @Override - public User findUser(String username, String domain) { - if (StringUtils.isAnyBlank(username, domain)) { - return null; - } else { - Collection authorities = new ArrayList<>(); - User user = new User(username, domain, - "$2a$10$U3GhSMpsMSOE8Kqsbn58/edxDBKlVuYMh7qk/7ErApYFjJzi2VG5K", true, - true, true, true, authorities); - return user; - } - } - -} diff --git a/spring-5/src/main/java/com/baeldung/securityextrafields/SecurityConfig.java b/spring-5/src/main/java/com/baeldung/securityextrafields/SecurityConfig.java deleted file mode 100644 index e8e8567773..0000000000 --- a/spring-5/src/main/java/com/baeldung/securityextrafields/SecurityConfig.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.baeldung.securityextrafields; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.authentication.AuthenticationProvider; -import org.springframework.security.authentication.dao.DaoAuthenticationProvider; -import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; -import org.springframework.security.config.annotation.web.builders.HttpSecurity; -import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; -import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; -import org.springframework.security.core.userdetails.UserDetailsService; -import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; -import org.springframework.security.crypto.password.PasswordEncoder; -import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler; -import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; - -@EnableWebSecurity -public class SecurityConfig extends WebSecurityConfigurerAdapter { - - @Autowired - private UserDetailsService userDetailsService; - - @Override - protected void configure(HttpSecurity http) throws Exception { - - http - .addFilterBefore(authenticationFilter(), UsernamePasswordAuthenticationFilter.class) - .authorizeRequests() - .antMatchers("/css/**", "/index").permitAll() - .antMatchers("/user/**").authenticated() - .and() - .formLogin().loginPage("/login") - .and() - .logout() - .logoutUrl("/logout"); - } - - public CustomAuthenticationFilter authenticationFilter() throws Exception { - CustomAuthenticationFilter filter = new CustomAuthenticationFilter(); - filter.setAuthenticationManager(authenticationManagerBean()); - filter.setAuthenticationFailureHandler(failureHandler()); - return filter; - } - - @Autowired - public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { - auth.authenticationProvider(authProvider()); - } - - public AuthenticationProvider authProvider() { - DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); - provider.setUserDetailsService(userDetailsService); - provider.setPasswordEncoder(passwordEncoder()); - return provider; - } - - public SimpleUrlAuthenticationFailureHandler failureHandler() { - return new SimpleUrlAuthenticationFailureHandler("/login?error=true"); - } - - public PasswordEncoder passwordEncoder() { - return new BCryptPasswordEncoder(); - } -} diff --git a/spring-5/src/main/java/com/baeldung/securityextrafields/User.java b/spring-5/src/main/java/com/baeldung/securityextrafields/User.java deleted file mode 100644 index a5b3a434ae..0000000000 --- a/spring-5/src/main/java/com/baeldung/securityextrafields/User.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.baeldung.securityextrafields; - -import java.util.Collection; - -import org.springframework.security.core.GrantedAuthority; - -public class User extends org.springframework.security.core.userdetails.User { - - private static final long serialVersionUID = 1L; - - private final String domain; - - public User(String username, String domain, String password, boolean enabled, - boolean accountNonExpired, boolean credentialsNonExpired, - boolean accountNonLocked, Collection authorities) { - super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities); - this.domain = domain; - } - - public String getDomain() { - return domain; - } -} diff --git a/spring-5/src/main/java/com/baeldung/securityextrafields/UserRepository.java b/spring-5/src/main/java/com/baeldung/securityextrafields/UserRepository.java deleted file mode 100644 index 4ca65b13d5..0000000000 --- a/spring-5/src/main/java/com/baeldung/securityextrafields/UserRepository.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.baeldung.securityextrafields; - -public interface UserRepository { - - public User findUser(String username, String domain); - -} diff --git a/spring-5/src/main/java/com/baeldung/securityextrafields/WebController.java b/spring-5/src/main/java/com/baeldung/securityextrafields/WebController.java deleted file mode 100644 index 4a8abb4a83..0000000000 --- a/spring-5/src/main/java/com/baeldung/securityextrafields/WebController.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.baeldung.securityextrafields; - -import java.util.Optional; - -import org.springframework.security.authentication.AnonymousAuthenticationToken; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.RequestMapping; - -@Controller -public class WebController { - - @RequestMapping("/") - public String root() { - return "redirect:/index"; - } - - @RequestMapping("/index") - public String index(Model model) { - getDomain().ifPresent(d -> { - model.addAttribute("domain", d); - }); - return "index"; - } - - @RequestMapping("/user/index") - public String userIndex(Model model) { - getDomain().ifPresent(d -> { - model.addAttribute("domain", d); - }); - return "user/index"; - } - - @RequestMapping("/login") - public String login() { - return "login"; - } - - private Optional 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/src/main/resources/static/css/main.css b/spring-5/src/main/resources/static/css/main.css deleted file mode 100644 index 9299ee6158..0000000000 --- a/spring-5/src/main/resources/static/css/main.css +++ /dev/null @@ -1,18 +0,0 @@ -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/src/main/resources/templates/index.html b/spring-5/src/main/resources/templates/index.html deleted file mode 100644 index 52f6224dfb..0000000000 --- a/spring-5/src/main/resources/templates/index.html +++ /dev/null @@ -1,24 +0,0 @@ - - - - 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/src/main/resources/templates/login.html b/spring-5/src/main/resources/templates/login.html deleted file mode 100644 index cafec89c15..0000000000 --- a/spring-5/src/main/resources/templates/login.html +++ /dev/null @@ -1,23 +0,0 @@ - - - - Login page - - - - -

Login page

-

Example: user / domain / password

-

Invalid user, password, or domain

-
- : -
- : -
- : -
- -
-

Back to home page

- - diff --git a/spring-5/src/main/resources/templates/user/index.html b/spring-5/src/main/resources/templates/user/index.html deleted file mode 100644 index a4c1535100..0000000000 --- a/spring-5/src/main/resources/templates/user/index.html +++ /dev/null @@ -1,13 +0,0 @@ - - - - Spring Security - Login With Extra Fields - - - - -
-

This is a secured page!

-

Back to home page

- -