独立应用程序中的 Spring Security

Pra*_*han 5 security spring

如何在独立应用程序中使用 Spring Security。我只需要使用 Spring Security 的身份验证部分。我需要根据 Windows Active Directory 对用户进行身份验证。网络上有很多在 Servlet 中使用 spring security 的示例,但找不到太多在独立应用程序中使用它们的示例。

我只是在寻找一些东西来完成这个方法

boolean isValidCredentials(String username, String password)
{
    //TODO use spring security for authentication here..
}
Run Code Online (Sandbox Code Playgroud)

pap*_*pap 3

如果您只需要进行身份验证,可以使用spring-security-ldap 中的ActiveDirectoryLdapAuthenticationProvider 。

只需在应用程序上下文中创建一个 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>
Run Code Online (Sandbox Code Playgroud)

然后像这样使用它

try {
    adAuthProvider.authenticate(new UsernamePasswordAuthenticationToken("user", "password"));
} catch (AuthenticationException ae) {
    // failed
}
Run Code Online (Sandbox Code Playgroud)