Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 

147 rindas
4.9 KiB

  1. package com.ffii.fpsms.modules.common;
  2. import java.util.Optional;
  3. import org.springframework.dao.DataAccessException;
  4. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  5. import org.springframework.security.core.Authentication;
  6. import org.springframework.security.core.GrantedAuthority;
  7. import org.springframework.security.core.context.SecurityContext;
  8. import org.springframework.security.core.context.SecurityContextHolder;
  9. import org.springframework.security.core.userdetails.UserDetails;
  10. import org.springframework.security.core.userdetails.UserDetailsService;
  11. import org.springframework.security.core.userdetails.UsernameNotFoundException;
  12. import com.ffii.fpsms.modules.user.entity.User;
  13. /**
  14. * Security Utils - for Spring Security
  15. *
  16. * @author Patrick
  17. */
  18. public class SecurityUtils {
  19. /**
  20. * Obtains the current {@code SecurityContext}.
  21. *
  22. * @return the security context (never {@code null})
  23. */
  24. public static final SecurityContext getSecurityContext() {
  25. return SecurityContextHolder.getContext();
  26. }
  27. /**
  28. * @return the authenticated {@code Principal})
  29. * @see Authentication#getPrincipal()
  30. */
  31. public static final Optional<User> getUser() {
  32. try {
  33. return Optional.of((User) getSecurityContext().getAuthentication().getPrincipal());
  34. } catch (ClassCastException e) {
  35. // no authenticated principal
  36. return Optional.empty();
  37. } catch (NullPointerException e) {
  38. // no authentication information is available
  39. return Optional.empty();
  40. }
  41. }
  42. /**
  43. * Updates the Authentication Token with the user (e.g. user changed the password)
  44. *
  45. * @see SecurityContext#setAuthentication(Authentication)
  46. */
  47. public static final void updateUserAuthentication(final UserDetails user) {
  48. getSecurityContext().setAuthentication(new UsernamePasswordAuthenticationToken(user, user.getPassword(), user.getAuthorities()));
  49. }
  50. /**
  51. * Checks if the current user is GRANTED the {@code role}
  52. *
  53. * @param role
  54. * the {@code role} to check for
  55. * @return {@code true} if the current user is GRANTED the {@code role}, else {@code false}
  56. */
  57. public static final boolean isGranted(String role) {
  58. Authentication authentication = getSecurityContext().getAuthentication();
  59. if (authentication == null) return false;
  60. for (GrantedAuthority auth : authentication.getAuthorities()) {
  61. if (role.equals(auth.getAuthority())) return true;
  62. }
  63. return false;
  64. }
  65. /**
  66. * Checks if the current user is NOT GRANTED the {@code role}
  67. *
  68. * @param role
  69. * the {@code role} to check for
  70. * @return {@code true} if the current user is NOT GRANTED the {@code role}, else {@code false}
  71. */
  72. public static final boolean isNotGranted(String role) {
  73. return !isGranted(role);
  74. }
  75. /**
  76. * Checks if the current user is GRANTED ANY of the {@code role}s
  77. *
  78. * @param roles
  79. * the {@code role}s to check for
  80. * @return {@code true} if the current user is GRANTED ANY of the {@code role}s, else {@code false}
  81. */
  82. public static final boolean isGrantedAny(String... roles) {
  83. for (int i = 0; i < roles.length; i++) {
  84. if (isGranted(roles[i])) return true;
  85. }
  86. return false;
  87. }
  88. /**
  89. * Checks if the current user is NOT GRANTED ANY of the {@code role}s
  90. *
  91. * @param roles
  92. * the {@code role}s to check for
  93. * @return {@code true} if the current user is NOT GRANTED ANY of the {@code role}s, else {@code false}
  94. */
  95. public static final boolean isNotGrantedAny(String... roles) {
  96. return !isGrantedAny(roles);
  97. }
  98. /**
  99. * Checks if the current user is GRANTED ALL of the {@code role}s
  100. *
  101. * @param roles
  102. * the {@code role}s to check for
  103. * @return {@code true} if the current user is GRANTED ALL of the {@code role}s, else {@code false}
  104. */
  105. public static final boolean isGrantedAll(String... roles) {
  106. for (int i = 0; i < roles.length; i++) {
  107. if (isNotGranted(roles[i])) return false;
  108. }
  109. return true;
  110. }
  111. /**
  112. * Login a user non-interactively
  113. *
  114. * @param userService
  115. * any implementation of {@link UserDetailsService}
  116. * @param username
  117. * the username
  118. *
  119. * @throws UsernameNotFoundException
  120. * if the user could not be found or the user has no GrantedAuthority
  121. * @throws DataAccessException
  122. * if user could not be found for a repository-specific reason
  123. */
  124. public static final void loginUser(UserDetailsService userService, String username) {
  125. /* load the user, throw exception if user not found */
  126. UserDetails userDetails = userService.loadUserByUsername(username);
  127. /* create authentication token for the specified user */
  128. Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities());
  129. getSecurityContext().setAuthentication(authentication);
  130. }
  131. }