使用Weblogic LDAP身份验证器配置Spring Security

时间:2012-03-27 11:32:28

标签: spring security ldap weblogic

我正在尝试使用在Oracle Weblogic Server 10.3中配置的LDAP身份验证器在我的应用程序中设置Spring Security。

我一直在互联网上搜索,但我找到的只是如何将LDAP设置到spring-security.xml中,但没有任何关于以某种方式将我在服务器上的配置导入其中,所以当我尝试要登录,它会使用服务器上的身份验证器检查用户和密码。

我想这样做,因为我无法访问LDAP的配置(它在生产环境中),所以我必须直接将数据发送给它。

有没有办法实现这个目标?

1 个答案:

答案 0 :(得分:0)

您需要创建自定义AuthenticationHandler并在Spring Security配置中声明它,或者您可以编写自己的UserDetailsS​​ervice来为您执行LDAP查询。

<authentication-manager>
    <authentication-provider user-service-ref="jbossLdapController" >
       <password-encoder hash="sha" base64="true" ref="passwordEncoder">
          <salt-source ref="saltSource"/>
       </password-encoder>
    </authentication-provider>
</authentication-manager>

有了这个,你可以创建一个实现UserDetailsService的类来获得它的所有功能,而不必使用典型的自定义身份验证处理程序手动实现它。

public class JBossLdapController implements UserDetailsService {
      ReflectionSaltSource saltSource;
      public void setSaltSource(ReflectionSaltSource saltSource) {
         this.saltSource = saltSource;
      }

      ShaPasswordEncoder passwordEncoder;
      public void setPasswordEncoder(ShaPasswordEncoder passwordEncoder) {
         this.passwordEncoder = passwordEncoder;
      }

      // LDAP stuff

      @Override
      public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException, DataAccessException {
         // Build user details, call LDAP query.  User details functionality of Spring will automatically compare user supplied credentials with hash and salt source versus username and encrypted password in UserDetails object.
      }

请记住,这只是一个如何在不放弃Spring Security UserDetails功能的情况下完成此操作的示例。当然,如果不了解有关LDAP提供程序的更多信息,密码可能不会使用SHA进行SHA加密。