我在LDap目录中进行搜索。用户成功通过身份验证。我想跳过身份验证并转到主页。我们可以在Spring-boot中做到吗?
这不是典型的Ldap登录。我的Ldap没有密码变量。我没有找到其他方法。 我想在ldap子三中搜索用户ID。如果存在用户ID,则用户可以登录。
@Override
protected void configure(HttpSecurity http) throws Exception {
String baseDN = "OU=ACCOUNTS,OU=MYGRUPPE,DC=MYGRUPPE,DC=COM";
String filter = "(&(objectClass=person)(UserLoginVariable=010101))";
LDAPConnection connection = getConnection();
http.authorizeRequests().anyRequest().authenticated().and().formLogin().permitAll().and().addFilterBefore(new Filter() {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
if (request.getParameter("username") != null && request.getParameter("password") != null) {
try {
List<SearchResultEntry> results = getResults(connection, baseDN, filter);
for (SearchResultEntry searchResultEntry : results) {
boolean control = userAuthentication(searchResultEntry.getAttributeValue("cn"),
request.getParameter("password"));
if (control) {
//FIXME
//Userfound operation successful!;
//Go Homepage
}
}
} catch (LDAPSearchException e) {
e.printStackTrace();
}
}
chain.doFilter(request, response);
}
}, UsernamePasswordAuthenticationFilter.class);
}
public LDAPConnection getConnection() throws LDAPException {
LDAPConnection con = new LDAPConnection("mygruppe.com", 389);
con.bind("CN=testuser,OU=ACCOUNTS,OU=MYGRUPPE,DC=MYGRUPPE,DC=COM",
"password");
return con;
}
public List<SearchResultEntry> getResults(LDAPConnection connection, String baseDN, String filter)
throws LDAPSearchException {
SearchResult searchResult;
searchResult = connection.search(baseDN, SearchScope.ONE, filter);
return searchResult.getSearchEntries();
}
public boolean userAuthentication(String userName, String password) {
Hashtable<String, String> authEnv = new Hashtable<String, String>();
String base = "OU=ACCOUNTS,OU=MYGRUPPE,DC=MYGRUPPE,DC=COM";
String dn = "CN=" + userName + "," + base;
String ldapURL = "ldap://ldapserver:389";
authEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
authEnv.put(Context.PROVIDER_URL, ldapURL);
authEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
authEnv.put(Context.SECURITY_PRINCIPAL, dn);
authEnv.put(Context.SECURITY_CREDENTIALS, password);
try {
DirContext authContext = new InitialDirContext(authEnv);
authContext.close();
return true;
}
}