ygl*_*odt 5 spring spring-security spring-ldap
在基于 Spring Security 3.2 的应用程序中,我需要根据用户名和远程 IP 地址中的特定模式,针对两个不同的提供者对用户进行身份验证。
如果它们符合某些规则,则应根据 进行身份验证,否则应使用使用现有自定义实现的ActiveDirectoryLdapAuthenticationProvider标准进行身份验证。AuthenticationProviderUserDetailsService
我需要延长什么?AuthenticationManager或者AuthenticationProvider?任何示例代码将不胜感激:-)
注意:我已经成功尝试<authentication-provider />在 中添加两个节点<authentication-manager />,并且效果很好。但令我困扰的是,我的 Ldap 服务器每次身份验证尝试都会受到攻击(即使是那些不适合它的尝试)
您可以创建一个包装器,它检查模式/IP 地址(如果匹配)调用委托,否则返回 null。
public class FilteringAuthenticationProvider implements AuthenticationProvider {
private final AuthenticationProvider delegate;
public FilteringAuthenticationProvider(AuthenticationProvider delegate) { this.delegate=delegate;}
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
Object details = authentication.getDetails();
String username = authentication.getPrincipal().toString();
String remoteAddress = null;
if (details instanceof WebAuthenticationDetails) {
remoteAddress = ((WebAuthenticationDetails) details).getRemoteAddress();
}
if (matches(remoteAddress, username)) {
return delegate.authenticate(authentication);
}
return null
}
private boolean matches(String remoteAddress, String Username) {
// your checking logic here
}
}
Run Code Online (Sandbox Code Playgroud)
像这样的东西。然后在您的安全配置中配置它并让它包装ActiveDirectoryLdapAuthenticationProvider.
<sec:authentication-manager>
<sec:authentication-provider ref="filteringLdapProvider" />
<sec:authentication-provider>
<user-service ref="customUserDetailsService" />
</sec:authentication-provider>
</sec:authentication-manager>
<bean id="filteringLdapProvider" class="FilteringAuthenticationProvider">
<constructor-arg ref="ldapProvider" />
</bean>
<bean id="ldapProvider" class="ActiveDirectoryLdapAuthenticationProvider">
...
</bean>
Run Code Online (Sandbox Code Playgroud)
像这样的东西。
| 归档时间: |
|
| 查看次数: |
2129 次 |
| 最近记录: |