diff --git a/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginPageFilter.java b/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginPageFilter.java new file mode 100644 index 0000000000..3c6d076756 --- /dev/null +++ b/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginPageFilter.java @@ -0,0 +1,43 @@ +package com.baeldung.loginredirect; + +import org.apache.http.HttpStatus; +import org.springframework.security.authentication.AnonymousAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.web.filter.GenericFilterBean; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +class LoginPageFilter extends GenericFilterBean { + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + HttpServletRequest servletRequest = (HttpServletRequest) request; + HttpServletResponse servletResponse = (HttpServletResponse) response; + + if (isAuthenticated() && "/loginUser".equals(servletRequest.getRequestURI())) { + + String encodedRedirectURL = ((HttpServletResponse) response).encodeRedirectURL( + servletRequest.getContextPath() + "/userMainPage"); + + servletResponse.setStatus(HttpStatus.SC_TEMPORARY_REDIRECT); + servletResponse.setHeader("Location", encodedRedirectURL); + } + + chain.doFilter(servletRequest, servletResponse); + } + + private boolean isAuthenticated() { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if (authentication == null || AnonymousAuthenticationToken.class.isAssignableFrom(authentication.getClass())) { + return false; + } + return authentication.isAuthenticated(); + } +} \ No newline at end of file diff --git a/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginPageInterceptor.java b/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginPageInterceptor.java new file mode 100644 index 0000000000..aa93201f37 --- /dev/null +++ b/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginPageInterceptor.java @@ -0,0 +1,39 @@ +package com.baeldung.loginredirect; + +import org.apache.http.HttpStatus; +import org.springframework.security.authentication.AnonymousAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; +import org.springframework.web.util.UrlPathHelper; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +class LoginPageInterceptor extends HandlerInterceptorAdapter { + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { + + UrlPathHelper urlPathHelper = new UrlPathHelper(); + if ("/loginUser".equals(urlPathHelper.getLookupPathForRequest(request)) && isAuthenticated()) { + + String encodedRedirectURL = response.encodeRedirectURL( + request.getContextPath() + "/userMainPage"); + response.setStatus(HttpStatus.SC_TEMPORARY_REDIRECT); + response.setHeader("Location", encodedRedirectURL); + + return false; + } else { + return true; + } + } + + private boolean isAuthenticated() { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if (authentication == null || AnonymousAuthenticationToken.class.isAssignableFrom(authentication.getClass())) { + return false; + } + return authentication.isAuthenticated(); + } +} diff --git a/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginRedirectApplication.java b/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginRedirectApplication.java new file mode 100644 index 0000000000..1e44240449 --- /dev/null +++ b/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginRedirectApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.loginredirect; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ImportResource; + +@SpringBootApplication +@ImportResource({"classpath*:spring-security-login-redirect.xml"}) +class LoginRedirectApplication { + public static void main(String[] args) { + SpringApplication.run(LoginRedirectApplication.class, args); + } +} \ No newline at end of file diff --git a/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginRedirectMvcConfig.java b/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginRedirectMvcConfig.java new file mode 100644 index 0000000000..ca2ddcb020 --- /dev/null +++ b/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginRedirectMvcConfig.java @@ -0,0 +1,14 @@ +package com.baeldung.loginredirect; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +class LoginRedirectMvcConfig implements WebMvcConfigurer { + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(new LoginPageInterceptor()); + } +} \ No newline at end of file diff --git a/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginRedirectSecurityConfig.java b/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginRedirectSecurityConfig.java new file mode 100644 index 0000000000..8bd3200608 --- /dev/null +++ b/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginRedirectSecurityConfig.java @@ -0,0 +1,43 @@ +package com.baeldung.loginredirect; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +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.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; + +@Configuration +@EnableWebSecurity +class LoginRedirectSecurityConfig extends WebSecurityConfigurerAdapter { + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.inMemoryAuthentication().withUser("user").password(encoder().encode("user")).roles("USER"); + } + + @Override + protected void configure(HttpSecurity http) throws Exception { + + http + .addFilterAfter(new LoginPageFilter(), UsernamePasswordAuthenticationFilter.class) + + .authorizeRequests() + .antMatchers("/loginUser").permitAll() + .antMatchers("/user*").hasRole("USER") + + .and().formLogin().loginPage("/loginUser").loginProcessingUrl("/user_login") + .failureUrl("/loginUser?error=loginError").defaultSuccessUrl("/userMainPage").permitAll() + + .and().logout().logoutUrl("/user_logout").logoutSuccessUrl("/loginUser").deleteCookies("JSESSIONID") + .and().csrf().disable(); + } + + @Bean + public static PasswordEncoder encoder() { + return new BCryptPasswordEncoder(); + } +} diff --git a/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/UsersController.java b/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/UsersController.java new file mode 100644 index 0000000000..308495d2ab --- /dev/null +++ b/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/UsersController.java @@ -0,0 +1,32 @@ +package com.baeldung.loginredirect; + +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.web.bind.annotation.GetMapping; + +@Controller +class UsersController { + + @GetMapping("/userMainPage") + public String getUserPage() { + return "userMainPage"; + } + + @GetMapping("/loginUser") + public String getUserLoginPage() { + if (isAuthenticated()) { + return "redirect:userMainPage"; + } + return "loginUser"; + } + + private boolean isAuthenticated() { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if (authentication == null || AnonymousAuthenticationToken.class.isAssignableFrom(authentication.getClass())) { + return false; + } + return authentication.isAuthenticated(); + } +} diff --git a/spring-security-modules/spring-security-mvc-boot-2/src/main/resources/spring-security-login-redirect.xml b/spring-security-modules/spring-security-mvc-boot-2/src/main/resources/spring-security-login-redirect.xml new file mode 100644 index 0000000000..e711abce1f --- /dev/null +++ b/spring-security-modules/spring-security-mvc-boot-2/src/main/resources/spring-security-login-redirect.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-security-modules/spring-security-mvc-boot-2/src/main/resources/templates/userMainPage.html b/spring-security-modules/spring-security-mvc-boot-2/src/main/resources/templates/userMainPage.html new file mode 100644 index 0000000000..501d019790 --- /dev/null +++ b/spring-security-modules/spring-security-mvc-boot-2/src/main/resources/templates/userMainPage.html @@ -0,0 +1,10 @@ + + + + +Baeldung Login Redirect + + +Welcome user! Logout + + \ No newline at end of file