Skip to content

Commit df385e0

Browse files
author
Eugen
committed
Merge pull request eugenp#34 from egmp777/master
Spring Login Error Handling Additional changes -
2 parents 21cbc2b + 7cda135 commit df385e0

35 files changed

+1273
-39
lines changed

bin/.springBeans

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beansProjectDescription>
3+
<version>1</version>
4+
<pluginVersion><![CDATA[3.5.1.201404300732-RELEASE]]></pluginVersion>
5+
<configSuffixes>
6+
<configSuffix><![CDATA[xml]]></configSuffix>
7+
</configSuffixes>
8+
<enableImports><![CDATA[true]]></enableImports>
9+
<configs>
10+
</configs>
11+
<autoconfigs>
12+
</autoconfigs>
13+
<configSets>
14+
</configSets>
15+
</beansProjectDescription>

bin/pom.xml

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>org.baeldung</groupId>
6+
<artifactId>spring-security-login-error-handling</artifactId>
7+
<name>spring-security-login-error-handling</name>
8+
<packaging>war</packaging>
9+
<version>1.0.0-BUILD-SNAPSHOT</version>
10+
11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>1.1.4.RELEASE</version>
15+
</parent>
16+
17+
<dependencies>
18+
19+
<!-- Spring -->
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-web</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.springframework</groupId>
26+
<artifactId>spring-context</artifactId>
27+
<exclusions>
28+
<!-- Exclude Commons Logging in favor of SLF4j -->
29+
<exclusion>
30+
<groupId>commons-logging</groupId>
31+
<artifactId>commons-logging</artifactId>
32+
</exclusion>
33+
</exclusions>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.springframework</groupId>
37+
<artifactId>spring-core</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.springframework</groupId>
41+
<artifactId>spring-webmvc</artifactId>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.springframework</groupId>
45+
<artifactId>spring-jdbc</artifactId>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.springframework</groupId>
49+
<artifactId>spring-beans</artifactId>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.springframework</groupId>
53+
<artifactId>spring-aop</artifactId>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.springframework</groupId>
57+
<artifactId>spring-tx</artifactId>
58+
</dependency>
59+
<dependency>
60+
<groupId>org.springframework</groupId>
61+
<artifactId>spring-expression</artifactId>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.springframework</groupId>
65+
<artifactId>spring-web</artifactId>
66+
</dependency>
67+
<dependency>
68+
<groupId>org.springframework</groupId>
69+
<artifactId>spring-webmvc</artifactId>
70+
</dependency>
71+
<dependency>
72+
<groupId>org.springframework.security</groupId>
73+
<artifactId>spring-security-config</artifactId>
74+
<scope>runtime</scope>
75+
</dependency>
76+
77+
<!-- AspectJ -->
78+
<dependency>
79+
<groupId>org.aspectj</groupId>
80+
<artifactId>aspectjrt</artifactId>
81+
</dependency>
82+
83+
<!-- Validation -->
84+
<dependency>
85+
<groupId>org.hibernate</groupId>
86+
<artifactId>hibernate-validator</artifactId>
87+
</dependency>
88+
89+
<!-- logging -->
90+
<dependency>
91+
<groupId>org.slf4j</groupId>
92+
<artifactId>slf4j-api</artifactId>
93+
</dependency>
94+
<dependency>
95+
<groupId>ch.qos.logback</groupId>
96+
<artifactId>logback-classic</artifactId>
97+
<!-- <scope>runtime</scope> -->
98+
</dependency>
99+
<dependency>
100+
<groupId>org.slf4j</groupId>
101+
<artifactId>jcl-over-slf4j</artifactId>
102+
<!-- <scope>runtime</scope> --> <!-- some spring dependencies need to compile against jcl -->
103+
</dependency>
104+
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
105+
<groupId>org.slf4j</groupId>
106+
<artifactId>log4j-over-slf4j</artifactId>
107+
</dependency>
108+
109+
<!-- @Inject -->
110+
<dependency>
111+
<groupId>javax.inject</groupId>
112+
<artifactId>javax.inject</artifactId>
113+
<version>1</version>
114+
</dependency>
115+
116+
<!-- Servlet -->
117+
<dependency>
118+
<groupId>javax.servlet</groupId>
119+
<artifactId>javax.servlet-api</artifactId>
120+
</dependency>
121+
<dependency>
122+
<groupId>javax.servlet.jsp</groupId>
123+
<artifactId>jsp-api</artifactId>
124+
<version>2.1</version>
125+
<scope>provided</scope>
126+
</dependency>
127+
<dependency>
128+
<groupId>javax.servlet</groupId>
129+
<artifactId>jstl</artifactId>
130+
</dependency>
131+
132+
<!-- Taglibs -->
133+
<dependency>
134+
<groupId>org.springframework.security</groupId>
135+
<artifactId>spring-security-taglibs</artifactId>
136+
</dependency>
137+
138+
<!-- Test -->
139+
<dependency>
140+
<groupId>junit</groupId>
141+
<artifactId>junit</artifactId>
142+
<scope>test</scope>
143+
</dependency>
144+
145+
</dependencies>
146+
147+
<build>
148+
<finalName>spring-security-login-and-registration</finalName>
149+
<resources>
150+
<resource>
151+
<directory>src/main/resources</directory>
152+
<filtering>true</filtering>
153+
</resource>
154+
</resources>
155+
156+
<plugins>
157+
<plugin>
158+
<groupId>org.apache.maven.plugins</groupId>
159+
<artifactId>maven-compiler-plugin</artifactId>
160+
</plugin>
161+
<plugin>
162+
<groupId>org.apache.maven.plugins</groupId>
163+
<artifactId>maven-war-plugin</artifactId>
164+
</plugin>
165+
</plugins>
166+
</build>
167+
<properties>
168+
<java-version>1.7</java-version>
169+
<org.springframework-version>3.1.1.RELEASE</org.springframework-version>
170+
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
171+
<org.aspectj-version>1.6.10</org.aspectj-version>
172+
173+
<!-- logging -->
174+
<org.slf4j.version>1.7.6</org.slf4j.version>
175+
<logback.version>1.1.1</logback.version>
176+
177+
</properties>
178+
179+
</project>

bin/src/main/resources/logback.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<configuration>
2+
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
6+
</pattern>
7+
</encoder>
8+
</appender>
9+
10+
<logger name="org.springframework" level="WARN" />
11+
<logger name="org.springframework.transaction" level="WARN" />
12+
13+
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
14+
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
15+
16+
<root level="INFO">
17+
<appender-ref ref="STDOUT" />
18+
</root>
19+
20+
</configuration>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
message.username=Username required
2+
message.password=Password required
3+
message.unauth=Unauthorized Access !!
4+
message.badCredentials=Invalid Username or Password
5+
message.sessionExpired=Session Timed Out
6+
message.logoutError=Sorry, error logging out
7+
message.logoutSucc=You logged out successfully
8+
message.regSucc=You registrated correctly, please log in
9+
message.regError=There was a registration error please go back to registration
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
message.username=Por favor ingrese el nombre de usuario
2+
message.password=Por favor ingrese una clave
3+
message.unauth=Acceso denegado !!
4+
message.badCredentials=Usuario o clave invalida
5+
message.sessionExpired=La sesion expiro
6+
message.logoutError=Lo sentimos, hubo problemas en logout
7+
message.logoutSucc=Logout con exito
8+
message.regSucc=Se registro correctamente, por favor ingrese
9+
message.regError=Hubo un error, por favor vuelva a registrarse
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans:beans xmlns="http://www.springframework.org/schema/security"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
4+
xmlns:mvc="http://www.springframework.org/schema/mvc"
5+
xsi:schemaLocation="
6+
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
7+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
8+
9+
10+
<http use-expressions="true">
11+
<intercept-url pattern="/login*" access="permitAll" />
12+
<intercept-url pattern="/logout*" access="permitAll" />
13+
<intercept-url pattern="/registration*" access="permitAll" />
14+
<intercept-url pattern="/resources/**" access="permitAll" />
15+
<intercept-url pattern="/invalidSession*" access="isAnonymous()" />
16+
<intercept-url pattern="/**" access="isAuthenticated()" />
17+
<form-login login-page='/login.html'
18+
authentication-failure-url="/login.html?error=true"
19+
authentication-success-handler-ref="myAuthenticationSuccessHandler"
20+
default-target-url="/homepage.html" />
21+
<session-management invalid-session-url="/invalidSession.html"
22+
session-fixation-protection="none" />
23+
<logout invalidate-session="false" logout-success-url="/logout.html?logSucc=1"
24+
logout-url="/j_spring_security_logout" delete-cookies="JSESSIONID" />
25+
</http>
26+
<beans:bean id="myAuthenticationSuccessHandler"
27+
class="org.baeldung.security.MySimpleUrlAuthenticationSuccessHandler" />
28+
<authentication-manager>
29+
<authentication-provider>
30+
<user-service>
31+
<user name="user1" password="user1Pass" authorities="ROLE_USER" />
32+
<user name="admin1" password="admin1Pass" authorities="ROLE_ADMIN" />
33+
</user-service>
34+
</authentication-provider>
35+
</authentication-manager>
36+
</beans:beans>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns:p="http://www.springframework.org/schema/p"
4+
xmlns:mvc="http://www.springframework.org/schema/mvc"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
6+
7+
8+
9+
10+
</beans>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
2+
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
3+
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
4+
<html>
5+
<sec:authorize ifAnyGranted="ROLE_USER">
6+
<spring:message code="message.unauth" ></spring:message>
7+
</sec:authorize>
8+
<head></head>
9+
10+
11+
<body>
12+
13+
<head></head>
14+
<sec:authorize ifAnyGranted="ROLE_ADMIN">
15+
<H1> Hello Admin</H1>
16+
</sec:authorize>
17+
18+
<a href="<c:url value="/j_spring_security_logout" />">Logout</a>
19+
<a href="<c:url value="/home.html" />">Home</a>
20+
</body>
21+
22+
23+
</html>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
2+
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %>
3+
<html>
4+
<head></head>
5+
6+
<body>
7+
<h1>This is the landing page for the admin</h1>
8+
9+
<security:authorize access="hasRole('ROLE_USER')">
10+
This text is only visible to a user
11+
<br/>
12+
</security:authorize>
13+
14+
<security:authorize access="hasRole('ROLE_ADMIN')">
15+
This text is only visible to an admin
16+
<br/>
17+
</security:authorize>
18+
19+
<a href="<c:url value="/j_spring_security_logout" />">Logout</a>
20+
<a href="<c:url value="/admin.html" />">Administrator Page</a>
21+
22+
</body>
23+
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
2+
<%@ page session="true" %>
3+
<html>
4+
<head>
5+
<title>Home</title>
6+
</head>
7+
<body>
8+
<h1>
9+
Welcome back home!
10+
</h1>
11+
12+
</body>
13+
</html>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
2+
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
3+
<%@ page session="true" %>
4+
<html>
5+
<head></head>
6+
7+
<body>
8+
<body>
9+
<h1>This is the homepage for the user</h1>
10+
11+
<sec:authorize access="hasRole('ROLE_USER')">
12+
This text is only visible to a user
13+
<br />
14+
</sec:authorize>
15+
16+
<sec:authorize access="hasRole('ROLE_ADMIN')">
17+
This text is only visible to an admin
18+
<br />
19+
</sec:authorize>
20+
21+
<a href="<c:url value="/j_spring_security_logout" />">Logout</a>
22+
<a href="<c:url value="/home.html" />">Home</a>
23+
<a href="<c:url value="/admin.html" />">Administrator Page</a>
24+
25+
</body>
26+
27+
</body>
28+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
2+
<html>
3+
<head>
4+
<title>Home</title>
5+
</head>
6+
<body>
7+
<h1>
8+
<spring:message code="message.sessionExpired" ></spring:message>
9+
</h1>
10+
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)