Skip to content

Commit 3a1897d

Browse files
author
eugenp
committed
redirect after login
1 parent 909f05a commit 3a1897d

File tree

4 files changed

+76
-17
lines changed

4 files changed

+76
-17
lines changed

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

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,67 @@
77
import javax.servlet.http.HttpServletResponse;
88
import javax.servlet.http.HttpSession;
99

10+
import org.apache.commons.logging.Log;
11+
import org.apache.commons.logging.LogFactory;
1012
import org.springframework.security.core.Authentication;
13+
import org.springframework.security.web.DefaultRedirectStrategy;
14+
import org.springframework.security.web.RedirectStrategy;
1115
import org.springframework.security.web.WebAttributes;
12-
import org.springframework.security.web.authentication.AbstractAuthenticationTargetUrlRequestHandler;
1316
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
17+
import org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper;
1418

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() {
19+
public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
20+
protected final Log logger = LogFactory.getLog(this.getClass());
21+
22+
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
23+
24+
protected MySimpleUrlAuthenticationSuccessHandler() {
2725
super();
2826
}
2927

3028
/**
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.
29+
* Invokes the configured {@code RedirectStrategy} with the URL returned by the {@code determineTargetUrl} method.
30+
* <p>
31+
* The redirect will not be performed if the response has already been committed.
32+
*/
33+
protected void handle(final HttpServletRequest request, final HttpServletResponse response, final Authentication authentication) throws IOException, ServletException {
34+
final String targetUrl = determineTargetUrl(request, response);
35+
36+
if (response.isCommitted()) {
37+
logger.debug("Response has already been committed. Unable to redirect to " + targetUrl);
38+
return;
39+
}
40+
41+
redirectStrategy.sendRedirect(request, response, targetUrl);
42+
}
43+
44+
/**
45+
* Builds the target URL according to the logic defined in the main class Javadoc.
3346
*/
34-
public MySimpleUrlAuthenticationSuccessHandler(final String defaultTargetUrl) {
35-
setDefaultTargetUrl(defaultTargetUrl);
47+
protected String determineTargetUrl(final HttpServletRequest requestRaw, final HttpServletResponse response) {
48+
// Check for the parameter and use that if available
49+
50+
final SecurityContextHolderAwareRequestWrapper req = (SecurityContextHolderAwareRequestWrapper) requestRaw;
51+
final boolean isUser = req.isUserInRole("ROLE_USER");
52+
final boolean isAdmin = req.isUserInRole("ROLE_ADMIN");
53+
if (isUser) {
54+
return "/homepage.html";
55+
} else if (isAdmin) {
56+
return "/console.html";
57+
} else {
58+
throw new IllegalStateException();
59+
}
60+
}
61+
62+
/**
63+
* Allows overriding of the behavior when redirecting to a target URL.
64+
*/
65+
public void setRedirectStrategy(final RedirectStrategy redirectStrategy) {
66+
this.redirectStrategy = redirectStrategy;
67+
}
68+
69+
protected RedirectStrategy getRedirectStrategy() {
70+
return redirectStrategy;
3671
}
3772

3873
/**

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public void addViewControllers(final ViewControllerRegistry registry) {
2727

2828
registry.addViewController("/login.html");
2929
registry.addViewController("/homepage.html");
30+
registry.addViewController("/console.html");
3031
}
3132

3233
@Bean

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<user-service>
3535
<user name="user1" password="user1Pass" authorities="ROLE_USER" />
3636
<user name="user2" password="user2Pass" authorities="ROLE_USER" />
37+
<user name="admin1" password="admin1Pass" authorities="ROLE_ADMIN" />
3738
</user-service>
3839
</authentication-provider>
3940
</authentication-manager>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 body of the sample view</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="/perform_logout" />">Logout</a>
20+
21+
</body>
22+
</html>

0 commit comments

Comments
 (0)