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