Skip to content

Commit 21e8c2d

Browse files
authored
Merge pull request #2310 from tbrugz/issue-2308
Adds setPrincipalSuffix() to AbstractLdapRealm & updates ActiveDirectoryRealm
2 parents bc863e7 + 864f046 commit 21e8c2d

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

core/src/main/java/org/apache/shiro/realm/activedirectory/ActiveDirectoryRealm.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ protected AuthenticationInfo queryForAuthenticationInfo(AuthenticationToken toke
108108
// Binds using the username and password provided by the user.
109109
LdapContext ctx = null;
110110
try {
111-
ctx = ldapContextFactory.getLdapContext(upToken.getUsername(), String.valueOf(upToken.getPassword()));
111+
ctx = ldapContextFactory.getLdapContext(getUsernameWithSuffix(upToken.getUsername()),
112+
String.valueOf(upToken.getPassword()));
112113
} finally {
113114
LdapUtils.closeContext(ctx);
114115
}
@@ -166,11 +167,7 @@ protected Set<String> getRoleNamesForUser(String username, LdapContext ldapConte
166167
SearchControls searchControls = new SearchControls();
167168
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
168169

169-
String userPrincipalName = username;
170-
if (principalSuffix != null
171-
&& !userPrincipalName.toLowerCase(Locale.ROOT).endsWith(principalSuffix.toLowerCase(Locale.ROOT))) {
172-
userPrincipalName += principalSuffix;
173-
}
170+
String userPrincipalName = getUsernameWithSuffix(username);
174171

175172
Object[] searchArguments = new Object[] {userPrincipalName};
176173

@@ -236,4 +233,12 @@ protected Collection<String> getRoleNamesForGroups(Collection<String> groupNames
236233
return roleNames;
237234
}
238235

236+
protected String getUsernameWithSuffix(String username) {
237+
if (principalSuffix != null
238+
&& !username.toLowerCase(Locale.ROOT).endsWith(principalSuffix.toLowerCase(Locale.ROOT))) {
239+
return username + principalSuffix;
240+
}
241+
return username;
242+
}
243+
239244
}

core/src/main/java/org/apache/shiro/realm/ldap/AbstractLdapRealm.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ public abstract class AbstractLdapRealm extends AuthorizingRealm {
100100
| M E T H O D S |
101101
============================================*/
102102

103+
/**
104+
* Used when initializing the default {@link LdapContextFactory}. This property is ignored if a custom
105+
* <tt>LdapContextFactory</tt> is specified.
106+
*
107+
* @param principalSuffix the suffix.
108+
*/
109+
public void setPrincipalSuffix(String principalSuffix) {
110+
this.principalSuffix = principalSuffix;
111+
}
103112

104113
/**
105114
* Used when initializing the default {@link LdapContextFactory}. This property is ignored if a custom

0 commit comments

Comments
 (0)