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 Summary
ConstructorDescriptionRegexDelegatingUserDetailsService
(org.springframework.security.core.userdetails.UserDetailsService delegateService, String regex, int targetGroup) Creates a new instance with the given properties.RegexDelegatingUserDetailsService
(org.springframework.security.core.userdetails.UserDetailsService delegateService, String regex, int regexFlags, int targetGroup) Creates a new instance with the given properties.RegexDelegatingUserDetailsService
(org.springframework.security.core.userdetails.UserDetailsService delegateService, Pattern pattern, int targetGroup) Creates a new instance with the given properties. -
Method Summary
Modifier and TypeMethodDescriptionprotected org.springframework.security.core.userdetails.UserDetailsService
protected Pattern
protected int
org.springframework.security.core.userdetails.UserDetails
loadUserByUsername
(String username) Loads the user data for the given username, using the following logic: Attempts to match the given username against the configured regular expression.toString()
-
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 delegateUserDetailsService
which will be invoked to get the user data.regex
- The regular expression which will used to extract the username. APattern
object is created from the expression usingPattern.compile(String)
.targetGroup
- Thegroup
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 asRegexDelegatingUserDetailsService(UserDetailsService, String, int)
with the additionalregexFlags
parameter, which is used to create thePattern
object from the regular expression usingPattern.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 delegateUserDetailsService
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
- Thegroup
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 delegateUserDetailsService
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 interfaceorg.springframework.security.core.userdetails.UserDetailsService
- Throws:
org.springframework.security.core.userdetails.UsernameNotFoundException
-
getDelegateService
protected org.springframework.security.core.userdetails.UserDetailsService getDelegateService() -
getPattern
-
getTargetGroup
protected int getTargetGroup() -
toString
-