[英]Handling roles when authenticated to active directory with spring security 3.1
[英]Spring Security with Active Directory and Database roles
我正在尝试将Spring Security与Active Directory用户名和密码一起使用。 此外,我有一个数据库Roles表,该表与包含Active Directory用户名的User表有关系(User表与Active Directory无关,它是完全独立的,我只是将用户名放在该表中,这样我就可以与角色匹配)
我正在关注这个示例http://krams915.blogspot.com/2012/01/spring-security-31-implement_1244.html
问题是我看不到CustomUserDetailsService类的loadUserByUsername方法中使用密码的位置。
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
try {
org.krams.domain.User domainUser = userRepository.findByUsername(username);
boolean enabled = true;
boolean accountNonExpired = true;
boolean credentialsNonExpired = true;
boolean accountNonLocked = true;
return new User(
domainUser.getUsername(),
domainUser.getPassword().toLowerCase(),
enabled,
accountNonExpired,
credentialsNonExpired,
accountNonLocked,
getAuthorities(domainUser.getRole().getRole()));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
据我了解,此行根据数据库userRepository.findByUsername(username);
验证用户userRepository.findByUsername(username);
但是,如何在其中使用lpad验证? 我可以在这里使用类似身份验证的工具吗? http://www.javaxt.com/Tutorials/Windows/How_to_Authenticate_Users_with_Active_Directory
更新:
我正在尝试使其与CustomUserDetailsContextMapper一起使用
XML
<authentication-manager >
<authentication-provider ref="ldapActiveDirectoryAuthProvider" />
</authentication-manager>
<beans:bean id="ldapActiveDirectoryAuthProvider"
class="org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider">
<beans:constructor-arg value="domain" />
<beans:constructor-arg value="ldap://NAME/"/>
<beans:property name="userDetailsContextMapper" ref="tdrUserDetailsContextMapper"/>
<beans:property name="useAuthenticationRequestCredentials" value="true"/>
</beans:bean>
<beans:bean id="tdrUserDetailsContextMapper" class="com.test8.security8.service.CustomUserDetailsContextMapper"/>
自定义类
public class CustomUserDetailsContextMapper implements UserDetailsContextMapper, Serializable{
private static final long serialVersionUID = 3962976258168853984L;
@Override
public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<? extends GrantedAuthority> authority) {
String role="admin";
System.out.println("TEST");
if(username.equals("usuario"))role="admin";
else role="user";
List authList = getAuthorities(role);
return new User(username, "", true, true, true, true, authList);
}
private List getAuthorities(String role) {
List authList = new ArrayList();
authList.add(new SimpleGrantedAuthority("ROLE_USER"));
//you can also add different roles here
//for example, the user is also an admin of the site, then you can add ROLE_ADMIN
//so that he can view pages that are ROLE_ADMIN specific
if (role != null && role.trim().length() > 0) {
if (role.equals("admin")) {
authList.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
}
}
return authList;
}
@Override
public void mapUserToContext(UserDetails arg0, DirContextAdapter arg1) {
// TODO Auto-generated method stub
}
}
我想我快要走了,但没有收到错误消息,但似乎无法映射任何用户, System.out.println("TEST")
; 行永远不会执行。 可能与URL /域名有关吗? 我确定网址正确无误,因为如果更改它,则会收到错误消息。
我最终按照M. Deinum的建议使用了UserDetailsContextMapper。 我还创建了海关SpringSecurityLdapTemplate和ActiveDirectoryLdapAuthenticationProvider。
在我的自定义ActiveDirectoryLdapAuthenticationProvider中,我更改了以下方法:
private LdapContext bindAsUser(String username, String password) {
// TODO. add DNS lookup based on domain
final String bindUrl = url;
Hashtable<String,String> env = new Hashtable<String,String>();
env.put(Context.SECURITY_AUTHENTICATION, "simple");
String bindPrincipal = createBindPrincipal(username);
env.put(Context.SECURITY_PRINCIPAL, bindPrincipal);
env.put(Context.PROVIDER_URL, bindUrl);
env.put(Context.SECURITY_CREDENTIALS, password);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.REFERRAL, "follow");
try {
return new InitialLdapContext(env, null);
} catch (NamingException e) {
if ((e instanceof AuthenticationException) || (e instanceof OperationNotSupportedException)) {
handleBindException(bindPrincipal, e);
throw badCredentials();
} else {
throw LdapUtils.convertLdapException(e);
}
}
}
这对我来说很重要: env.put(Context.REFERRAL,“ follow”);
并在SpringSecurityLdapTemplate自定义类中
public static DirContextOperations searchForSingleEntryInternal(DirContext ctx, SearchControls searchControls,
String base, String filter, Object[] params) throws NamingException {
final DistinguishedName ctxBaseDn = new DistinguishedName(ctx.getNameInNamespace());
final DistinguishedName searchBaseDn = new DistinguishedName(base);
final NamingEnumeration<SearchResult> resultsEnum = ctx.search(searchBaseDn, filter, params, buildControls(searchControls));
filter, searchControls);
if (logger.isDebugEnabled()) {
logger.debug("Searching for entry under DN '" + ctxBaseDn
+ "', base = '" + searchBaseDn + "', filter = '" + filter + "'");
}
Set<DirContextOperations> results = new HashSet<DirContextOperations>();
try {
while (resultsEnum.hasMore()) {
SearchResult searchResult = resultsEnum.next();
DirContextAdapter dca = new DirContextAdapter();
//dca=(DirContextAdapter) searchResult.getObject();
Assert.notNull(dca, "No object returned by search, DirContext is not correctly configured");
if (logger.isDebugEnabled()) {
logger.debug("Found DN: " + dca.getDn());
}
results.add(dca);
}
} catch (PartialResultException e) {
LdapUtils.closeEnumeration(resultsEnum);
logger.info("Ignoring PartialResultException");
}
if (results.size() == 0) {
throw new IncorrectResultSizeDataAccessException(1, 0);
}
if (results.size() > 1) {
throw new IncorrectResultSizeDataAccessException(1, results.size());
}
return results.iterator().next();
}
这对我有用,但这是一个非常特殊的场景。 感谢Deinum M向我指出正确的方向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.