source: springapp/src/main/java/mk/profesori/springapp/Security/CustomAuthenticationSuccessHandler.java@ 6eba109

main
Last change on this file since 6eba109 was 6eba109, checked in by unknown <mlviktor23@…>, 2 years ago

implemented authentication in react

  • Property mode set to 100644
File size: 2.7 KB
Line 
1package mk.profesori.springapp.Security;
2
3import java.io.IOException;
4import java.util.Collection;
5import java.util.HashMap;
6import java.util.Map;
7
8import javax.servlet.http.HttpServletRequest;
9import javax.servlet.http.HttpServletResponse;
10import javax.servlet.http.HttpSession;
11
12import org.apache.commons.logging.Log;
13import org.apache.commons.logging.LogFactory;
14import org.springframework.security.core.Authentication;
15import org.springframework.security.core.GrantedAuthority;
16import org.springframework.security.web.DefaultRedirectStrategy;
17import org.springframework.security.web.RedirectStrategy;
18import org.springframework.security.web.WebAttributes;
19import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
20
21public 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}
Note: See TracBrowser for help on using the repository browser.