diff --git a/spring-security-login-error-handling/.springBeans b/spring-security-login-error-handling/.springBeans new file mode 100644 index 0000000000..8096aa036b --- /dev/null +++ b/spring-security-login-error-handling/.springBeans @@ -0,0 +1,15 @@ + + + 1 + + + + + + + + + + + + diff --git a/spring-security-login-error-handling/pom.xml b/spring-security-login-error-handling/pom.xml new file mode 100644 index 0000000000..f806df0391 --- /dev/null +++ b/spring-security-login-error-handling/pom.xml @@ -0,0 +1,226 @@ + + + 4.0.0 + org.baeldung + spring-security-login-error-handling + spring-security-login-error-handling + war + 1.0.0-BUILD-SNAPSHOT + + 1.7 + 3.1.1.RELEASE + 3.2.4.RELEASE + 1.6.10 + 1.6.6 + + + org.springframework.boot + spring-boot-starter-parent + 1.1.1.RELEASE + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework + spring-context + + + + commons-logging + commons-logging + + + + + org.springframework + spring-core + + + + org.springframework + spring-webmvc + + + + org.springframework + spring-jdbc + + + + org.springframework + spring-beans + + + + org.springframework + spring-aop + + + org.springframework + spring-tx + + + org.springframework + spring-expression + + + org.springframework + spring-web + + + org.springframework + spring-webmvc + + + org.springframework.security + spring-security-config + runtime + + + + org.aspectj + aspectjrt + + + + javax.validation + validation-api + 1.1.0.Final + + + org.hibernate + hibernate-validator + + + + org.slf4j + slf4j-api + + + org.slf4j + jcl-over-slf4j + runtime + + + org.slf4j + slf4j-log4j12 + runtime + + + log4j + log4j + + + javax.mail + mail + + + javax.jms + jms + + + com.sun.jdmk + jmxtools + + + com.sun.jmx + jmxri + + + runtime + + + + + javax.inject + javax.inject + 1 + + + + + javax.servlet + servlet-api + 2.5 + provided + + + javax.servlet.jsp + jsp-api + 2.1 + provided + + + javax.servlet + jstl + + + + + org.springframework.security + spring-security-taglibs + + + + junit + junit + test + + + + + SpringSecurityLogin + + + src/main/resources + true + + + + + maven-eclipse-plugin + + + + org.springframework.ide.eclipse.core.springnature + + + org.springframework.ide.eclipse.core.springbuilder + + true + true + + + + org.apache.maven.plugins + maven-compiler-plugin + + + 1.7 + 1.7 + -Xlint:all + true + true + + + + org.apache.maven.plugins + maven-war-plugin + + + + org.codehaus.mojo + exec-maven-plugin + + + org.test.int1.Main + + + + + diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/security/MySimpleUrlAuthenticationSuccessHandler.java b/spring-security-login-error-handling/src/main/java/org/baeldung/security/MySimpleUrlAuthenticationSuccessHandler.java new file mode 100644 index 0000000000..88862d603e --- /dev/null +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/security/MySimpleUrlAuthenticationSuccessHandler.java @@ -0,0 +1,81 @@ +package org.baeldung.security; + +import java.io.IOException; +import java.util.Collection; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.web.DefaultRedirectStrategy; +import org.springframework.security.web.RedirectStrategy; +import org.springframework.security.web.WebAttributes; +import org.springframework.security.web.authentication.AuthenticationSuccessHandler; + +public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSuccessHandler { + protected Log logger = LogFactory.getLog(this.getClass()); + + private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); + + public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException { + handle(request, response, authentication); + HttpSession session = request.getSession(false); + if (session != null) { + session.setMaxInactiveInterval(30); + } + clearAuthenticationAttributes(request); + } + + protected void handle(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException { + String targetUrl = determineTargetUrl(authentication); + + if (response.isCommitted()) { + logger.debug("Response has already been committed. Unable to redirect to " + targetUrl); + return; + } + + redirectStrategy.sendRedirect(request, response, targetUrl); + } + + protected String determineTargetUrl(Authentication authentication) { + boolean isUser = false; + boolean isAdmin = false; + Collection authorities = authentication.getAuthorities(); + for (GrantedAuthority grantedAuthority : authorities) { + if (grantedAuthority.getAuthority().equals("ROLE_USER")) { + isUser = true; + break; + } else if (grantedAuthority.getAuthority().equals("ROLE_ADMIN")) { + isAdmin = true; + break; + } + } + if (isUser) { + return "/homepage.html"; + } else if (isAdmin) { + return "/console.html"; + } else { + throw new IllegalStateException(); + } + } + + protected void clearAuthenticationAttributes(HttpServletRequest request) { + HttpSession session = request.getSession(false); + if (session == null) { + return; + } + session.removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION); + } + + public void setRedirectStrategy(RedirectStrategy redirectStrategy) { + this.redirectStrategy = redirectStrategy; + } + + protected RedirectStrategy getRedirectStrategy() { + return redirectStrategy; + } +} \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/spring/MvcConfig.java b/spring-security-login-error-handling/src/main/java/org/baeldung/spring/MvcConfig.java new file mode 100644 index 0000000000..3cecdd9588 --- /dev/null +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/spring/MvcConfig.java @@ -0,0 +1,77 @@ +package org.baeldung.spring; + +import java.util.Locale; + +import org.springframework.context.MessageSource; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.support.ReloadableResourceBundleMessageSource; +import org.springframework.web.servlet.LocaleResolver; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.i18n.CookieLocaleResolver; +import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; +import org.springframework.web.servlet.view.InternalResourceViewResolver; +import org.springframework.web.servlet.view.JstlView; + +@Configuration +@EnableWebMvc +public class MvcConfig extends WebMvcConfigurerAdapter { + + public MvcConfig() { + super(); + } + + // API + + @Override + public void addViewControllers(final ViewControllerRegistry registry) { + super.addViewControllers(registry); + + registry.addViewController("/login.html"); + registry.addViewController("/logout.html"); + registry.addViewController("/homepage.html"); + registry.addViewController("/home.html"); + registry.addViewController("/invalidSession.html"); + registry.addViewController("/console.html"); + registry.addViewController("/admin.html"); + registry.addViewController("/registration.html"); + } + + @Bean + public ViewResolver viewResolver() { + final InternalResourceViewResolver bean = new InternalResourceViewResolver(); + bean.setViewClass(JstlView.class); + bean.setPrefix("/WEB-INF/view/"); + bean.setSuffix(".jsp"); + + return bean; + } + + @Override + public void addInterceptors(InterceptorRegistry registry) { + LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor(); + localeChangeInterceptor.setParamName("lang"); + registry.addInterceptor(localeChangeInterceptor); + } + + @Bean + public LocaleResolver localeResolver() { + CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver(); + cookieLocaleResolver.setDefaultLocale(Locale.ENGLISH); + return cookieLocaleResolver; + } + + @Bean + public MessageSource messageSource() { + ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); + messageSource.setBasename("classpath:messages"); + messageSource.setUseCodeAsDefaultMessage(true); + messageSource.setDefaultEncoding("UTF-8"); + messageSource.setCacheSeconds(0); + return messageSource; + } +} \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/spring/SecSecurityConfig.java b/spring-security-login-error-handling/src/main/java/org/baeldung/spring/SecSecurityConfig.java new file mode 100644 index 0000000000..99efdf4237 --- /dev/null +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/spring/SecSecurityConfig.java @@ -0,0 +1,13 @@ +package org.baeldung.spring; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.ImportResource; + +@Configuration +@ImportResource({ "classpath:webSecurityConfig.xml" }) +public class SecSecurityConfig { + + public SecSecurityConfig() { + super(); + } +} diff --git a/spring-security-login-error-handling/src/main/resources/logback.xml b/spring-security-login-error-handling/src/main/resources/logback.xml new file mode 100644 index 0000000000..1146dade63 --- /dev/null +++ b/spring-security-login-error-handling/src/main/resources/logback.xml @@ -0,0 +1,20 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/resources/messages_en.properties b/spring-security-login-error-handling/src/main/resources/messages_en.properties new file mode 100644 index 0000000000..3e05a6b76a --- /dev/null +++ b/spring-security-login-error-handling/src/main/resources/messages_en.properties @@ -0,0 +1,9 @@ +message.username=Username required +message.password=Password required +message.unauth=Unauthorized Access !! +message.badCredentials=Invalid Username or Password +message.sessionExpired=Session Timed Out +message.logoutError=Sorry, error logging out +message.logoutSucc=You logged out successfully +message.regSucc=You registrated correctly, please log in +message.regError=There was a registration error please go back to registration \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/resources/messages_es_ES.properties b/spring-security-login-error-handling/src/main/resources/messages_es_ES.properties new file mode 100644 index 0000000000..842a899e43 --- /dev/null +++ b/spring-security-login-error-handling/src/main/resources/messages_es_ES.properties @@ -0,0 +1,9 @@ +message.username=Por favor ingrese el nombre de usuario +message.password=Por favor ingrese una clave +message.unauth=Acceso denegado !! +message.badCredentials=Usuario o clave invalida +message.sessionExpired=La sesion expiro +message.logoutError=Lo sentimos, hubo problemas en logout +message.logoutSucc=Logout con exito +message.regSucc=Se registro correctamente, por favor ingrese +message.regError=Hubo un error, por favor vuelva a registrarse \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/resources/webSecurityConfig.xml b/spring-security-login-error-handling/src/main/resources/webSecurityConfig.xml new file mode 100644 index 0000000000..809fdd164d --- /dev/null +++ b/spring-security-login-error-handling/src/main/resources/webSecurityConfig.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/mvc-servlet.xml b/spring-security-login-error-handling/src/main/webapp/WEB-INF/mvc-servlet.xml new file mode 100644 index 0000000000..b885d2c10a --- /dev/null +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/mvc-servlet.xml @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/admin.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/admin.jsp new file mode 100644 index 0000000000..12f9f7aba9 --- /dev/null +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/admin.jsp @@ -0,0 +1,23 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> +<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> +<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> + + + + + + + + + + + +

Hello Admin

+
+ + ">Logout + ">Home + + + + diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/console.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/console.jsp new file mode 100644 index 0000000000..05a930731b --- /dev/null +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/console.jsp @@ -0,0 +1,23 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> +<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %> + + + + +

This is the landing page for the admin

+ + + This text is only visible to a user +
+
+ + + This text is only visible to an admin +
+
+ + ">Logout + ">Administrator Page + + + \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/home.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/home.jsp new file mode 100644 index 0000000000..fe6e572b99 --- /dev/null +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/home.jsp @@ -0,0 +1,13 @@ +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> +<%@ page session="true" %> + + + Home + + +

+ Welcome back home! +

+ + + diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/homepage.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/homepage.jsp new file mode 100644 index 0000000000..fab96383df --- /dev/null +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/homepage.jsp @@ -0,0 +1,28 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> +<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> +<%@ page session="true" %> + + + + + +

This is the homepage for the user

+ + + This text is only visible to a user +
+
+ + + This text is only visible to an admin +
+
+ + ">Logout + ">Home + ">Administrator Page + + + + + \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/invalidSession.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/invalidSession.jsp new file mode 100644 index 0000000000..175c498117 --- /dev/null +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/invalidSession.jsp @@ -0,0 +1,12 @@ +<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> + + + Home + + +

+ +

+ + + diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/login.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/login.jsp new file mode 100644 index 0000000000..95559b0455 --- /dev/null +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/login.jsp @@ -0,0 +1,77 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> +<%@ taglib prefix="sec" + uri="http://www.springframework.org/security/tags"%> +<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> +<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> + +<%@ page session="false"%> + +
+ +
+
+ +
+ +
+
+ + +
+ +
+ Register +
+ + + + + + + + + +

Login

+ English | + Spanish +
+ + + + + + + + + + + + + +
User:
Password:
+ +
+
Current Locale : ${pageContext.response.locale} + + \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/logout.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/logout.jsp new file mode 100644 index 0000000000..e8618b74e3 --- /dev/null +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/logout.jsp @@ -0,0 +1,24 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> +<%@ taglib prefix="sec" + uri="http://www.springframework.org/security/tags"%> +<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> + +
+ +
+
+ +
+ +
+
+ + + +Logged Out + + + + Login + + \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/registration.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/registration.jsp new file mode 100644 index 0000000000..474a1817b5 --- /dev/null +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/registration.jsp @@ -0,0 +1,12 @@ +<%@ page language="java" contentType="text/html; charset=US-ASCII" + pageEncoding="US-ASCII"%> + + + + +Registration + + +

This is the registration page

+ + \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/web.xml b/spring-security-login-error-handling/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..463b309377 --- /dev/null +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,41 @@ + + + + contextClass + + org.springframework.web.context.support.AnnotationConfigWebApplicationContext + + + + contextConfigLocation + org.baeldung.spring + + + org.springframework.web.context.ContextLoaderListener + + + mvc + org.springframework.web.servlet.DispatcherServlet + 1 + + + mvc + / + + + springSecurityFilterChain + org.springframework.web.filter.DelegatingFilterProxy + + + springSecurityFilterChain + /* + + + localizationFilter + org.springframework.web.filter.RequestContextFilter + + + localizationFilter + /* + + \ No newline at end of file diff --git a/spring-security-login-error-handling/src/test/java/.springBeans b/spring-security-login-error-handling/src/test/java/.springBeans new file mode 100644 index 0000000000..8096aa036b --- /dev/null +++ b/spring-security-login-error-handling/src/test/java/.springBeans @@ -0,0 +1,15 @@ + + + 1 + + + + + + + + + + + + diff --git a/spring-security-login-error-handling/src/test/java/pom.xml b/spring-security-login-error-handling/src/test/java/pom.xml new file mode 100644 index 0000000000..5ff5926f60 --- /dev/null +++ b/spring-security-login-error-handling/src/test/java/pom.xml @@ -0,0 +1,225 @@ + + + 4.0.0 + com.egm + SpringSecurityLogin + SpringSecurityLogin + war + 1.0.0-BUILD-SNAPSHOT + + 1.7 + 3.1.1.RELEASE + 3.2.4.RELEASE + 1.6.10 + 1.6.6 + + + org.springframework.boot + spring-boot-starter-parent + 1.1.1.RELEASE + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework + spring-context + + + + commons-logging + commons-logging + + + + + org.springframework + spring-core + + + + org.springframework + spring-webmvc + + + + org.springframework + spring-jdbc + + + + org.springframework + spring-beans + + + + org.springframework + spring-aop + + + org.springframework + spring-tx + + + org.springframework + spring-expression + + + org.springframework + spring-web + + + org.springframework + spring-webmvc + + + org.springframework.security + spring-security-config + runtime + + + + org.aspectj + aspectjrt + + + + javax.validation + validation-api + 1.1.0.Final + + + org.hibernate + hibernate-validator + + + + org.slf4j + slf4j-api + + + org.slf4j + jcl-over-slf4j + runtime + + + org.slf4j + slf4j-log4j12 + runtime + + + log4j + log4j + + + javax.mail + mail + + + javax.jms + jms + + + com.sun.jdmk + jmxtools + + + com.sun.jmx + jmxri + + + runtime + + + + + javax.inject + javax.inject + 1 + + + + + javax.servlet + servlet-api + 2.5 + provided + + + javax.servlet.jsp + jsp-api + 2.1 + provided + + + javax.servlet + jstl + + + + org.springframework.security + spring-security-taglibs + + + + junit + junit + test + + + + + SpringSecurityLogin + + + src/main/resources + true + + + + + maven-eclipse-plugin + + + + org.springframework.ide.eclipse.core.springnature + + + org.springframework.ide.eclipse.core.springbuilder + + true + true + + + + org.apache.maven.plugins + maven-compiler-plugin + + + 1.7 + 1.7 + -Xlint:all + true + true + + + + org.apache.maven.plugins + maven-war-plugin + + + + org.codehaus.mojo + exec-maven-plugin + + + org.test.int1.Main + + + + +