A self hosted chat application with end-to-end encrypted messaging.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.6 KiB

5 years ago
  1. package org.ros.chatto.security;
  2. import java.io.IOException;
  3. import java.util.Collection;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import org.springframework.security.core.Authentication;
  8. import org.springframework.security.core.GrantedAuthority;
  9. import org.springframework.security.core.authority.SimpleGrantedAuthority;
  10. import org.springframework.security.web.DefaultRedirectStrategy;
  11. import org.springframework.security.web.RedirectStrategy;
  12. import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
  13. import org.springframework.stereotype.Component;
  14. @Component
  15. public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler {
  16. private static final SimpleGrantedAuthority SUPER_USER_AUTHORITY = new SimpleGrantedAuthority("ROLE_SUPER_USER");
  17. private static final SimpleGrantedAuthority ADMIN_AUTHORITY = new SimpleGrantedAuthority("ROLE_ADMIN");
  18. private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
  19. @Override
  20. public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
  21. Authentication authentication) throws IOException, ServletException {
  22. Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
  23. if (authorities.contains(ADMIN_AUTHORITY) || authorities.contains(SUPER_USER_AUTHORITY)) {
  24. redirectStrategy.sendRedirect(httpServletRequest, httpServletResponse, "/admin");
  25. } else {
  26. redirectStrategy.sendRedirect(httpServletRequest, httpServletResponse, "/user");
  27. }
  28. }
  29. }