2012-06-18 43 views
4

如何在独立应用程序中使用Spring Security。我只需要使用Spring Security的Authentication部分。我需要根据Windows Active Directory对用户进行身份验证。在Web上有很多使用Spring安全性的例子,但在独立应用程序中找不到太多的例子。Spring Security在独立应用程序中的应用

我只是寻找的东西来完成这个方法

boolean isValidCredentials(String username, String password) 
{ 
    //TODO use spring security for authentication here.. 
} 

回答

1

您可以从弹簧安全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> 

然后使用它像

try { 
    adAuthProvider.authenticate(new UsernamePasswordAuthenticationToken("user", "password")); 
} catch (AuthenticationException ae) { 
    // failed 
} 
+0

很多感谢您的帮助[pap],这个工程 –

0

using-spring-security-in-a-swing-desktop-application

public Authentication authenticate(String username, String password) { 
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password); 

Authentication auth = _authProvider.authenticate(token); 
if (null != auth) { 
    SecurityContextHolder.getContext().setAuthentication(auth); 

    _eventPublisher.publishEvent(new InteractiveAuthenticationSuccessEvent(auth, this.getClass())); 

    return auth; 
} 
throw new BadCredentialsException("null authentication"); 
} 

我没有独自上面的代码试过,但看起来很合理。下面链接到javadoc为方便SecurityContextHolder