Skip to content

Commit 909f05a

Browse files
author
eugenp
committed
cleanup work on mvc custom project
1 parent 6858e46 commit 909f05a

4 files changed

Lines changed: 72 additions & 41 deletions

File tree

‎spring-security-mvc-custom/README.md‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55

66
### Relevant Articles:
7-
- [Spring Security Form Login](http://www.baeldung.com/spring-security-login)
8-
- [Spring Security Logout](http://www.baeldung.com/spring-security-logout)
9-
- [Spring Security Expressions – hasRole Example](http://www.baeldung.com/spring-security-expressions-basic)
107

118

129
### Build the Project

‎spring-security-mvc-custom/src/main/java/org/baeldung/security/CustomLogoutSuccessHandler.java‎

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.baeldung.security;
2+
3+
import java.io.IOException;
4+
5+
import javax.servlet.ServletException;
6+
import javax.servlet.http.HttpServletRequest;
7+
import javax.servlet.http.HttpServletResponse;
8+
import javax.servlet.http.HttpSession;
9+
10+
import org.springframework.security.core.Authentication;
11+
import org.springframework.security.web.WebAttributes;
12+
import org.springframework.security.web.authentication.AbstractAuthenticationTargetUrlRequestHandler;
13+
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
14+
15+
/**
16+
* <tt>AuthenticationSuccessHandler</tt> which can be configured with a default URL which users should be
17+
* sent to upon successful authentication.
18+
* <p>
19+
* The logic used is that of the {@link AbstractAuthenticationTargetUrlRequestHandler parent class}.
20+
*
21+
* @author Luke Taylor
22+
* @since 3.0
23+
*/
24+
public class MySimpleUrlAuthenticationSuccessHandler extends AbstractAuthenticationTargetUrlRequestHandler implements AuthenticationSuccessHandler {
25+
26+
public MySimpleUrlAuthenticationSuccessHandler() {
27+
super();
28+
}
29+
30+
/**
31+
* Constructor which sets the <tt>defaultTargetUrl</tt> property of the base class.
32+
* @param defaultTargetUrl the URL to which the user should be redirected on successful authentication.
33+
*/
34+
public MySimpleUrlAuthenticationSuccessHandler(final String defaultTargetUrl) {
35+
setDefaultTargetUrl(defaultTargetUrl);
36+
}
37+
38+
/**
39+
* Calls the parent class {@code handle()} method to forward or redirect to the target URL, and
40+
* then calls {@code clearAuthenticationAttributes()} to remove any leftover session data.
41+
*/
42+
@Override
43+
public void onAuthenticationSuccess(final HttpServletRequest request, final HttpServletResponse response, final Authentication authentication) throws IOException, ServletException {
44+
handle(request, response, authentication);
45+
clearAuthenticationAttributes(request);
46+
}
47+
48+
/**
49+
* Removes temporary authentication-related data which may have been stored in the session
50+
* during the authentication process.
51+
*/
52+
protected final void clearAuthenticationAttributes(final HttpServletRequest request) {
53+
final HttpSession session = request.getSession(false);
54+
55+
if (session == null) {
56+
return;
57+
}
58+
59+
session.removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
60+
}
61+
62+
}

‎spring-security-mvc-custom/src/main/resources/webSecurityConfig.xml‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@
1515
<form-login
1616
login-page='/login.html'
1717
login-processing-url="/perform_login"
18-
default-target-url="/homepage.html"
19-
authentication-failure-url="/login.html?error=true"
20-
always-use-default-target="true"/>
18+
authentication-success-handler-ref="myAuthenticationSuccessHandler"
19+
authentication-failure-url="/login.html?error=true"
20+
/>
2121

22-
<logout
23-
logout-url="/perform_logout"
24-
delete-cookies="JSESSIONID"
25-
success-handler-ref="customLogoutSuccessHandler" />
22+
<logout
23+
logout-url="/perform_logout"
24+
delete-cookies="JSESSIONID"
25+
/>
2626

2727
</http>
28-
29-
<beans:bean name="customLogoutSuccessHandler" class="org.baeldung.security.CustomLogoutSuccessHandler" />
28+
29+
<beans:bean id="myAuthenticationSuccessHandler"
30+
class="org.baeldung.security.MySimpleUrlAuthenticationSuccessHandler" />
3031

3132
<authentication-manager>
3233
<authentication-provider>

0 commit comments

Comments
 (0)