package kr.wisestone.owl.util; import com.google.common.collect.Lists; import kr.wisestone.owl.domain.User; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.context.SecurityContext; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.session.SessionRegistry; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.web.authentication.WebAuthenticationDetails; import org.springframework.security.web.context.HttpSessionSecurityContextRepository; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.util.Collection; import java.util.HashSet; import java.util.Map; import java.util.Optional; /** * Utility class for Spring Security. */ public final class SecurityUtils { private SecurityUtils() { } /** * Get the login of the current user. */ public static String getCurrentUserLogin() { SecurityContext securityContext = SecurityContextHolder.getContext(); Authentication authentication = securityContext.getAuthentication(); String userName = null; if (authentication != null) { if (authentication.getPrincipal() instanceof UserDetails) { UserDetails springSecurityUser = (UserDetails) authentication.getPrincipal(); userName = springSecurityUser.getUsername(); } else if (authentication.getPrincipal() instanceof String) { userName = (String) authentication.getPrincipal(); } } return userName; } /** * Check if a user is authenticated. * * @return true if the user is authenticated, false otherwise */ public static boolean isAuthenticated() { SecurityContext securityContext = SecurityContextHolder.getContext(); Collection extends GrantedAuthority> authorities = securityContext.getAuthentication().getAuthorities(); if (authorities != null) { for (GrantedAuthority authority : authorities) { if (authority.getAuthority().equals("ROLE_ANONYMOUS")) { return false; } } } return true; } /** * Return the current user, or throws an exception, if the user is not * authenticated yet. * * @return the current user */ public static User getCurrentUser() { SecurityContext securityContext = SecurityContextHolder.getContext(); Authentication authentication = securityContext.getAuthentication(); if (authentication != null) { if (authentication.getPrincipal() instanceof User) { return (User) authentication.getPrincipal(); } } return null; } public static void setUserToSession(User user) { SecurityContext securityContext = SecurityContextHolder.getContext(); securityContext.setAuthentication(new UsernamePasswordAuthenticationToken(user, "password", new HashSet<>())); } public static void addUserToSession(User user) { SecurityContext securityContext = SecurityContextHolder.getContext(); securityContext.setAuthentication(new UsernamePasswordAuthenticationToken(user, "password")); } /** * If the current user has a specific authority (security role). * *
The name of this method comes from the isUserInRole() method in the Servlet API
*/ public static boolean isCurrentUserInRole(String authority) { SecurityContext securityContext = SecurityContextHolder.getContext(); Authentication authentication = securityContext.getAuthentication(); if (authentication != null) { if (authentication.getPrincipal() instanceof UserDetails) { UserDetails springSecurityUser = (UserDetails) authentication.getPrincipal(); return springSecurityUser.getAuthorities().contains(new SimpleGrantedAuthority(authority)); } } return false; } public static User getCurrentUserFromWebSocket(Map