Class NtsSecurityUtils

  • All Implemented Interfaces:
    org.springframework.beans.factory.InitializingBean

    @Component
    public final class NtsSecurityUtils
    extends Object
    implements org.springframework.beans.factory.InitializingBean
    Utility class for Spring Security.
    • Constructor Detail

      • NtsSecurityUtils

        @Autowired
        public NtsSecurityUtils​(NtsProperties ntsProperties)
      • NtsSecurityUtils

        public NtsSecurityUtils​(String rolesClaim,
                                String rolePrefix,
                                boolean reverseOrderOfDisplayName)
    • Method Detail

      • getCurrentUserLogin

        public static Optional<String> getCurrentUserLogin()
        Get the login of the current user.
        Returns:
        the login of the current user.
      • getCurrentUserDisplayName

        public static Optional<String> getCurrentUserDisplayName()
        Get the display name of the current user.
        Returns:
        the display name of the current user.
      • toDisplayName

        public static String toDisplayName​(String firstName,
                                           String lastName)
      • isAuthenticated

        public static boolean isAuthenticated()
        Check if a user is authenticated.
        Returns:
        true if the user is authenticated, false otherwise.
      • hasCurrentUserAnyOfAuthorities

        public static boolean hasCurrentUserAnyOfAuthorities​(String... authorities)
        Checks if the current user has any of the authorities.
        Parameters:
        authorities - the authorities to check.
        Returns:
        true if the current user has any of the authorities, false otherwise.
      • hasCurrentUserNoneOfAuthorities

        public static boolean hasCurrentUserNoneOfAuthorities​(String... authorities)
        Checks if the current user has none of the authorities.
        Parameters:
        authorities - the authorities to check.
        Returns:
        true if the current user has none of the authorities, false otherwise.
      • hasCurrentUserThisAuthority

        public static boolean hasCurrentUserThisAuthority​(String authority)
        Checks if the current user has a specific authority.
        Parameters:
        authority - the authority to check.
        Returns:
        true if the current user has the authority, false otherwise.
      • extractAuthorityFromClaims

        public static List<org.springframework.security.core.GrantedAuthority> extractAuthorityFromClaims​(Map<String,​Object> claims)
      • getCurrentUserClaim

        public static Optional<Object> getCurrentUserClaim​(String claim)
        Get claim value of current user by claim name
        Parameters:
        claim - the name of claim in JWT
        Returns:
        Optional of claim value
      • afterPropertiesSet

        public void afterPropertiesSet()
        Specified by:
        afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean