Skip to content

Commit 495a135

Browse files
author
eugenp
committed
redirect after login work
1 parent 984a0bf commit 495a135

4 files changed

Lines changed: 27 additions & 39 deletions

File tree

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.apache.commons.logging.LogFactory;
1313
import org.springframework.security.core.Authentication;
1414
import org.springframework.security.core.GrantedAuthority;
15+
import org.springframework.security.core.context.SecurityContextHolder;
1516
import org.springframework.security.web.DefaultRedirectStrategy;
1617
import org.springframework.security.web.RedirectStrategy;
1718
import org.springframework.security.web.WebAttributes;
@@ -46,11 +47,9 @@ protected void handle(final HttpServletRequest request, final HttpServletRespons
4647
* Builds the target URL according to the logic defined in the main class Javadoc.
4748
*/
4849
protected String determineTargetUrl(final HttpServletRequest requestRaw, final HttpServletResponse response) {
49-
// Check for the parameter and use that if available
50-
5150
boolean isUser = false;
5251
boolean isAdmin = false;
53-
final Authentication authentication = org.springframework.security.core.context.SecurityContextHolder.getContext().getAuthentication();
52+
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
5453
final Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
5554
for (final GrantedAuthority grantedAuthority : authorities) {
5655
if (grantedAuthority.getAuthority().equals("ROLE_USER")) {
Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,31 @@
11
<?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-
xsi:schemaLocation="
2+
<beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
3+
xsi:schemaLocation="
54
http://www.springframework.org/schema/security
65
http://www.springframework.org/schema/security/spring-security-3.1.xsd
76
http://www.springframework.org/schema/beans
87
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
98

10-
<http use-expressions="true" >
11-
<intercept-url pattern="/anonymous*" access="isAnonymous()" />
9+
<http use-expressions="true">
10+
<intercept-url pattern="/anonymous*" access="isAnonymous()" />
1211
<intercept-url pattern="/login*" access="permitAll" />
1312
<intercept-url pattern="/**" access="isAuthenticated()" />
1413

15-
<form-login
16-
login-page='/login.html'
17-
login-processing-url="/perform_login"
18-
authentication-success-handler-ref="myAuthenticationSuccessHandler"
19-
authentication-failure-url="/login.html?error=true"
20-
/>
21-
22-
<logout
23-
logout-url="/perform_logout"
24-
delete-cookies="JSESSIONID"
25-
/>
26-
27-
</http>
14+
<form-login login-page='/login.html' authentication-success-handler-ref="myAuthenticationSuccessHandler" authentication-failure-url="/login.html?error=true" />
2815

29-
<beans:bean id="myAuthenticationSuccessHandler"
30-
class="org.baeldung.security.MySimpleUrlAuthenticationSuccessHandler" />
16+
<logout delete-cookies="JSESSIONID" />
3117

32-
<authentication-manager>
33-
<authentication-provider>
34-
<user-service>
35-
<user name="user1" password="user1Pass" authorities="ROLE_USER" />
36-
<user name="user2" password="user2Pass" authorities="ROLE_USER" />
37-
<user name="admin1" password="admin1Pass" authorities="ROLE_ADMIN" />
38-
</user-service>
39-
</authentication-provider>
40-
</authentication-manager>
18+
</http>
19+
20+
<beans:bean id="myAuthenticationSuccessHandler" class="org.baeldung.security.MySimpleUrlAuthenticationSuccessHandler" />
21+
22+
<authentication-manager>
23+
<authentication-provider>
24+
<user-service>
25+
<user name="user1" password="user1Pass" authorities="ROLE_USER" />
26+
<user name="admin1" password="admin1Pass" authorities="ROLE_ADMIN" />
27+
</user-service>
28+
</authentication-provider>
29+
</authentication-manager>
4130

4231
</beans:beans>

‎spring-security-mvc-custom/src/main/webapp/WEB-INF/view/console.jsp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<head></head>
55

66
<body>
7-
<h1>This is the body of the sample view</h1>
7+
<h1>This is the landing page for the admin</h1>
88

99
<security:authorize access="hasRole('ROLE_USER')">
1010
This text is only visible to a user
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
2-
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %>
2+
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags"%>
33
<html>
44
<head></head>
55

66
<body>
7-
<h1>This is the body of the sample view</h1>
7+
<h1>This is the homepage for the user</h1>
88

99
<security:authorize access="hasRole('ROLE_USER')">
1010
This text is only visible to a user
11-
<br/>
11+
<br />
1212
</security:authorize>
13-
13+
1414
<security:authorize access="hasRole('ROLE_ADMIN')">
1515
This text is only visible to an admin
16-
<br/>
16+
<br />
1717
</security:authorize>
1818

1919
<a href="<c:url value="/perform_logout" />">Logout</a>
20-
20+
2121
</body>
2222
</html>

0 commit comments

Comments
 (0)