Class RegexDelegatingUserDetailsService

java.lang.Object
com.appiancorp.suiteapi.common.spring.security.RegexDelegatingUserDetailsService
All Implemented Interfaces:
org.springframework.security.core.userdetails.UserDetailsService

public class RegexDelegatingUserDetailsService extends Object implements org.springframework.security.core.userdetails.UserDetailsService
This class decorates another UserDetailsService implementation, allowing for a regular expression to be used to extract the username from the value passed into loadUserByUsername(String).

For example, if the usernames provided for authentication have the format "john.smith@example.com", but the usernames stored in the system are without the domain ("john.smith"), this class can be used to extract the username without the domain and pass it to the delegate UserDetailsService.

Sample XML configuration:


<bean id="userDetailsService" class="com.appiancorp.suiteapi.common.spring.security.RegexDelegatingUserDetailsService">
  <constructor-arg ref="myUserDetailsService"/>
  <constructor-arg value="(.+)@(.+)"/>
  <constructor-arg value="1"/>
</bean>
<bean id="myUserDetailsService" class="com.example.security.auth.MyUserDetailsService"/>
  • Constructor Details

    • RegexDelegatingUserDetailsService

      public RegexDelegatingUserDetailsService(org.springframework.security.core.userdetails.UserDetailsService delegateService, String regex, int targetGroup)
      Creates a new instance with the given properties.
      Parameters:
      delegateService - The delegate UserDetailsService which will be invoked to get the user data.
      regex - The regular expression which will used to extract the username. A Pattern object is created from the expression using Pattern.compile(String).
      targetGroup - The group within the regular expression that should be used to extract the username.
    • RegexDelegatingUserDetailsService

      public RegexDelegatingUserDetailsService(org.springframework.security.core.userdetails.UserDetailsService delegateService, String regex, int regexFlags, int targetGroup)
      Creates a new instance with the given properties. This constructor is the same as RegexDelegatingUserDetailsService(UserDetailsService, String, int) with the additional regexFlags parameter, which is used to create the Pattern object from the regular expression using Pattern.compile(String, int).
    • RegexDelegatingUserDetailsService

      public RegexDelegatingUserDetailsService(org.springframework.security.core.userdetails.UserDetailsService delegateService, Pattern pattern, int targetGroup)
      Creates a new instance with the given properties.
      Parameters:
      delegateService - The delegate UserDetailsService which will be invoked to get the user data.
      pattern - The compiled representation of a regular expression which will used to extract the username.
      targetGroup - The group within the regular expression that should be used to extract the username.
  • Method Details

    • loadUserByUsername

      public org.springframework.security.core.userdetails.UserDetails loadUserByUsername(String username) throws org.springframework.security.core.userdetails.UsernameNotFoundException
      Loads the user data for the given username, using the following logic:
      • Attempts to match the given username against the configured regular expression.
      • If the username matches the regular expression, retrieves the portion of the given username captured by the configured group. This value is passed as to the configured delegate UserDetailsService to actually load the user data.
      • If the given username doesn't match the regular expression, or if it doesn't contain the specified group, the given username is passed to the delegate UserDetailsService as is.
      Specified by:
      loadUserByUsername in interface org.springframework.security.core.userdetails.UserDetailsService
      Throws:
      org.springframework.security.core.userdetails.UsernameNotFoundException
    • getDelegateService

      protected org.springframework.security.core.userdetails.UserDetailsService getDelegateService()
    • getPattern

      protected Pattern getPattern()
    • getTargetGroup

      protected int getTargetGroup()
    • toString

      public String toString()
      Overrides:
      toString in class Object