1 | package mk.profesori.springapp.Security;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.util.Collection;
|
---|
5 | import java.util.HashMap;
|
---|
6 | import java.util.Map;
|
---|
7 |
|
---|
8 | import javax.servlet.http.HttpServletRequest;
|
---|
9 | import javax.servlet.http.HttpServletResponse;
|
---|
10 | import javax.servlet.http.HttpSession;
|
---|
11 |
|
---|
12 | import org.apache.commons.logging.Log;
|
---|
13 | import org.apache.commons.logging.LogFactory;
|
---|
14 | import org.springframework.security.core.Authentication;
|
---|
15 | import org.springframework.security.core.GrantedAuthority;
|
---|
16 | import org.springframework.security.web.DefaultRedirectStrategy;
|
---|
17 | import org.springframework.security.web.RedirectStrategy;
|
---|
18 | import org.springframework.security.web.WebAttributes;
|
---|
19 | import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
---|
20 |
|
---|
21 | public class CustomAuthenticationSuccessHandler
|
---|
22 | implements AuthenticationSuccessHandler {
|
---|
23 |
|
---|
24 | protected Log logger = LogFactory.getLog(this.getClass());
|
---|
25 |
|
---|
26 | private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
|
---|
27 |
|
---|
28 | @Override
|
---|
29 | public void onAuthenticationSuccess(HttpServletRequest request,
|
---|
30 | HttpServletResponse response, Authentication authentication)
|
---|
31 | throws IOException {
|
---|
32 |
|
---|
33 | handle(request, response, authentication);
|
---|
34 | clearAuthenticationAttributes(request);
|
---|
35 | }
|
---|
36 |
|
---|
37 | protected void handle(
|
---|
38 | HttpServletRequest request,
|
---|
39 | HttpServletResponse response,
|
---|
40 | Authentication authentication) throws IOException {
|
---|
41 |
|
---|
42 | String targetUrl = determineTargetUrl(authentication, request.getSession().getId());
|
---|
43 |
|
---|
44 | if (response.isCommitted()) {
|
---|
45 | logger.debug(
|
---|
46 | "Response has already been committed. Unable to redirect to "
|
---|
47 | + targetUrl);
|
---|
48 | return;
|
---|
49 | }
|
---|
50 |
|
---|
51 | redirectStrategy.sendRedirect(request, response, targetUrl);
|
---|
52 | }
|
---|
53 |
|
---|
54 | protected String determineTargetUrl(final Authentication authentication, String sessionId) {
|
---|
55 |
|
---|
56 | Map<String, String> roleTargetUrlMap = new HashMap<>();
|
---|
57 | roleTargetUrlMap.put("REGULAR", "/public/loginSuccessRegular?sessionId=" + sessionId);
|
---|
58 | roleTargetUrlMap.put("MODERATOR", "/public/loginSuccessModerator?sessionId=" + sessionId);
|
---|
59 |
|
---|
60 | final Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
|
---|
61 | for (final GrantedAuthority grantedAuthority : authorities) {
|
---|
62 | String authorityName = grantedAuthority.getAuthority();
|
---|
63 | if (roleTargetUrlMap.containsKey(authorityName)) {
|
---|
64 | return roleTargetUrlMap.get(authorityName);
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | throw new IllegalStateException();
|
---|
69 | }
|
---|
70 |
|
---|
71 | protected void clearAuthenticationAttributes(HttpServletRequest request) {
|
---|
72 | HttpSession session = request.getSession(false);
|
---|
73 | if (session == null) {
|
---|
74 | return;
|
---|
75 | }
|
---|
76 | session.removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
|
---|
77 | }
|
---|
78 |
|
---|
79 | }
|
---|