独立应用程序中的 Spring Security [英] Spring Security in Standalone Application

查看:60
本文介绍了独立应用程序中的 Spring Security的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在独立应用程序中使用 Spring Security.我只需要使用 Spring Security 的 Authentication 部分.我需要针对 Windows Active Directory 对用户进行身份验证.网络上有很多在 Servlet 中使用 spring 安全性的示例,但在独立应用程序中使用它们的例子不多.

How do I use Spring Security in a standalone application. I just need to use the Authentication portion of Spring Security. I need to authenticate users against Windows Active Directory. There are lots of examples in the web for using spring security in Servlets but couldn't find much for using them in standalone applications.

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

I am only looking for something to complete this method

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

推荐答案

您可以使用 ActiveDirectoryLdapAuthenticationProvider 来自 spring-security-ldap 如果您只需要进行身份验证.

You can use the ActiveDirectoryLdapAuthenticationProvider from spring-security-ldap if you just need to do authentication.

只需在您的应用程序上下文中创建一个 bean,例如:

Just create a bean in your application context like:

<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
}

这篇关于独立应用程序中的 Spring Security的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆