openldap,activedirectory密码验证支持

openldap,activedirectory密码验证支持,需要先完成用户同步
This commit is contained in:
MaxKey
2021-03-26 20:04:25 +08:00
parent 1b25e476ae
commit 237ec64787
8 changed files with 136 additions and 48 deletions

View File

@@ -54,6 +54,12 @@ public abstract class AbstractAuthenticationRealm {
protected LoginHistoryService loginHistoryService;
protected AbstractRemeberMeService remeberMeService;
protected boolean ldapSupport;
protected AbstractAuthenticationRealm ldapAuthenticationRealm;
/**
*

View File

@@ -53,9 +53,13 @@ public class DefaultJdbcAuthenticationRealm extends AbstractAuthenticationRealm
*/
public boolean passwordMatches(UserInfo userInfo, String password) {
boolean passwordMatches = false;
_logger.info("password : "
+ PasswordReciprocal.getInstance().rawPassword(userInfo.getUsername(), password));
passwordMatches = passwordEncoder.matches(password,userInfo.getPassword());
if(ldapSupport) {
passwordMatches =this.ldapAuthenticationRealm.passwordMatches(userInfo, password);
}else {
_logger.debug("password : "
+ PasswordReciprocal.getInstance().rawPassword(userInfo.getUsername(), password));
passwordMatches = passwordEncoder.matches(password,userInfo.getPassword());
}
_logger.debug("passwordvalid : " + passwordMatches);
if (!passwordMatches) {
passwordPolicyValidator.setBadPasswordCount(userInfo);

View File

@@ -17,6 +17,7 @@
package org.maxkey.authn.realm.jdbc;
import org.maxkey.authn.realm.AbstractAuthenticationRealm;
import org.maxkey.authn.support.rememberme.AbstractRemeberMeService;
import org.maxkey.persistence.db.LoginHistoryService;
import org.maxkey.persistence.db.LoginService;
@@ -59,5 +60,27 @@ public class JdbcAuthenticationRealm extends DefaultJdbcAuthenticationRealm {
}
public JdbcAuthenticationRealm(
PasswordEncoder passwordEncoder,
PasswordPolicyValidator passwordPolicyValidator,
LoginService loginService,
LoginHistoryService loginHistoryService,
AbstractRemeberMeService remeberMeService,
JdbcTemplate jdbcTemplate,
AbstractAuthenticationRealm ldapAuthenticationRealm,
boolean ldapSupport
) {
this.passwordEncoder =passwordEncoder;
this.passwordPolicyValidator=passwordPolicyValidator;
this.loginService = loginService;
this.loginHistoryService = loginHistoryService;
this.remeberMeService = remeberMeService;
this.jdbcTemplate = jdbcTemplate;
this.ldapAuthenticationRealm = ldapAuthenticationRealm;
this.ldapSupport = ldapSupport;
}
}

View File

@@ -46,7 +46,7 @@ public final class LdapServer implements IAuthenticationServer {
*/
@Override
public boolean authenticate(String username, String password) {
String queryFilter = "("+filterAttribute+"="+username+")";
String queryFilter = String.format(filterAttribute, username);
_logger.info(" filter : " + queryFilter);
String dn="";
SearchControls constraints = new SearchControls();
@@ -69,7 +69,7 @@ public final class LdapServer implements IAuthenticationServer {
} catch (NamingException e) {
_logger.error("query throw NamingException:" + e.getMessage());
} finally {
ldapUtils.close();
//ldapUtils.close();
}
LdapUtils ldapPassWordValid=new LdapUtils(ldapUtils.getProviderUrl(),dn,password);