如何在独立应用程序中使用 Spring Security。我只需要使用 Spring Security 的身份验证部分。我需要根据 Windows Active Directory 对用户进行身份验证。网络上有很多在 Servlet 中使用 spring security 的示例,但找不到太多在独立应用程序中使用它们的示例。
我只是在寻找一些东西来完成这个方法
boolean isValidCredentials(String username, String password)
{
//TODO use spring security for authentication here..
}
您可以使用ActiveDirectoryLdapAuthenticationProvider http://static.springsource.org/spring-security/site/docs/3.1.x/apidocs/org/springframework/security/ldap/authentication/ad/ActiveDirectoryLdapAuthenticationProvider.html如果您只需要进行身份验证,请从 spring-security-ldap 获取。
只需在应用程序上下文中创建一个 bean,例如:
<bean id="adAuthProvider" class="org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider">
<constructor-arg value="your.domain" />
<constructor-arg value="ldap://your.ad.server" />
</bean>
然后像这样使用它
try {
adAuthProvider.authenticate(new UsernamePasswordAuthenticationToken("user", "password"));
} catch (AuthenticationException ae) {
// failed
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)