1

我已经使用 ldap 和纯文本成功实现了 spring 安全登录。现在我切换到 sha-256 来获取 ldap 中的密码哈希。我在 applicationContext-security.xml 中添加了密码编码器。但是,我无法使用 Spring Security 成功进行身份验证。我收到“您输入了无效的用户名或密码!”

这是我的代码:

applicationContext-security.xml

  <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:security="http://www.springframework.org/schema/security"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">

        <security:http auto-config="true" use-expressions="true" access-denied-page="/user/denied" >
            <security:form-login
                    login-page="/login" 
                    authentication-failure-url="/login?error=true" 
                    default-target-url="/admin/user"/>
            <security:logout invalidate-session="true" 
                    logout-success-url="/login" 
                    logout-url="/j_spring_security_logout"/>
        </security:http>
        <security:authentication-manager>
            <security:ldap-authentication-provider user-search-filter="uid={0}" user-search-base="ou=Users">
             <security:password-compare>
                 <security:password-encoder hash="sha-256" />
             </security:password-compare>
           </security:ldap-authentication-provider>
        </security:authentication-manager>

        <bean id="contextSource" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
            <constructor-arg value="ldap://192.168.1.100:389/dc=openLDAP" />
            <property name="userDn" value="cn=admin,dc=openLDAP" />
            <property name="password" value="password" />
        </bean>
</beans>

登录控制器:

@RequestMapping(value="/login", method=RequestMethod.GET)
public String login(@RequestParam(value="error", required=false) boolean error, ModelMap model, HttpServletRequest request) {
   HttpSession session = request.getSession();
   if(session.getAttribute("SPRING_SECURITY_CONTEXT" != null) {
      return "redirect:/home";
   }

   if(error) {
      model.put("error", "you have entered an invalid username or password!");
   }
   else {
      model.put("error", "");
   }

   return "login";
}

登录视图:

<form action="j_spring_security_check" method="post" >
   username: <input id="j_username" name="j_username" type="text" />
   password: <input id="j_password" name="j_password" type="password" />
</form>
4

1 回答 1

2

我认为您的问题是您在客户端(spring-security)对密码进行哈希处理,然后 OpenLDAP 再次对该字符串进行哈希处理,然后将结果与存储的密码哈希进行比较。两种组合之一应该起作用:

  1. 在 spring-security 中配置散列算法并将密码散列存储为 OpenLDAP 中的纯文本密码类型 -> 散列密码发送到您的 LDAP 并与存储的散列密码进行比较。
  2. 不要在 spring-security 中配置散列并将密码存储为 OpenLDAP 中的散列密码类型(就像您按照您的评论所做的那样)-> 纯文本密码被发送到您的 LDAP,由 LDAP 服务器散列,然后与存储的密码哈希。

我建议 2) 结合到您的 LDAP 服务器的 SSL 加密连接,以保护传输中的纯文本密码。如果您选择 2),您可以使用 SMD5 密码类型轻松启用密码哈希的加盐。1) 缺点是每个客户端都需要支持所选的哈希函数。

于 2014-01-06T08:30:24.933 回答