Java 使用Spring Security 3对密码进行哈希和盐析

Java 使用Spring Security 3对密码进行哈希和盐析,java,spring,hash,spring-security,saltedhash,Java,Spring,Hash,Spring Security,Saltedhash,如何散列密码并使用Spring Security 3对其进行加密?您可以按如下方式进行操作: 在application-context.xml(在web.xml中contextConfigLocation下定义)文件中定义bean(本例使用md5) 在你的方法中,或者任何你想要的地方 passwordEncoder.encodePassword("MyPasswordAsString", "mySaltAsStringOrObject"); 上面的调用应该返回一个salted散列(作为字符串)

如何散列密码并使用Spring Security 3对其进行加密?

您可以按如下方式进行操作:

在application-context.xml(在web.xml中
contextConfigLocation
下定义)文件中定义bean(本例使用
md5

在你的方法中,或者任何你想要的地方

passwordEncoder.encodePassword("MyPasswordAsString", "mySaltAsStringOrObject");
上面的调用应该返回一个salted散列(作为
字符串

应该这样做。我想你能找到你需要的罐子

更新

不用说,使用MD5不是最好的主意。理想情况下,您至少应该使用SHA-256。这可以通过

将上面的MD5 bean配置替换为:

<bean id="passwordEncoder" class="org.springframework.security.authentication.encoding.ShaPasswordEncoder">
     <constructor-arg value="256"/>
</bean>

最简单的方法,如:



HTH

最简单的似乎是SpringSecurity3.1,假设哈希处理的方式没有限制:

<bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>

<security:authentication-manager>
    <security:authentication-provider>
        <security:password-encoder ref="encoder"/>
        <security:jdbc-user-service data-source-ref="dataSource" users-by-username-query="select username,password, enabled from users where username=?" authorities-by-username-query="select u.username, ur.authority from users u, user_roles ur where u.username = ur.username and u.username =?"/>
    </security:authentication-provider>
</security:authentication-manager>


@Controller
@Stateless
public class UsersEJB {
    @PersistenceContext(unitName = "somePU")
    private EntityManager em;
    @Transactional
    public void create(Users users) {
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String hashedPassword = passwordEncoder.encode(users.getPassword());
        users.setPassword(hashedPassword);
        em.persist(users);
    }
}

@控制器
@无国籍
公共类UsersEJB{
@PersistenceContext(unitName=“somePU”)
私人实体管理者;
@交易的
公共void创建(用户){
PasswordEncoder PasswordEncoder=新的BCryptPasswordEncoder();
字符串hashedPassword=passwordEncoder.encode(users.getPassword());
users.setPassword(hashedPassword);
em.persist(用户);
}
}

+1:大多数人都是这样做的(在我看来也是正确的)谢谢Simeon,我想kamaci已经走了,但还没有接受任何答案。为什么不使用Spring beans,比如?显然,尽管你不会使用MD5。谢谢你的回答,投票结果是,但是请更新您的答案,以引用安全哈希算法。您不应该创建BCryptPasswordEncoder的新实例,而应该@Autowire到id为“encoder”的bean,这很好,但是没有任何解释,这并不完全有帮助。
<bean id="passwordEncoder" class="org.springframework.security.authentication.encoding.ShaPasswordEncoder">
     <constructor-arg value="256"/>
</bean>
<authentication-manager alias="authenticationManager">
    <authentication-provider user-service-ref="userDetailsService" >
        <password-encoder hash="sha">
            <salt-source user-property="username"/>
        </password-encoder>
    </authentication-provider>
</authentication-manager>
<bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>

<security:authentication-manager>
    <security:authentication-provider>
        <security:password-encoder ref="encoder"/>
        <security:jdbc-user-service data-source-ref="dataSource" users-by-username-query="select username,password, enabled from users where username=?" authorities-by-username-query="select u.username, ur.authority from users u, user_roles ur where u.username = ur.username and u.username =?"/>
    </security:authentication-provider>
</security:authentication-manager>


@Controller
@Stateless
public class UsersEJB {
    @PersistenceContext(unitName = "somePU")
    private EntityManager em;
    @Transactional
    public void create(Users users) {
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String hashedPassword = passwordEncoder.encode(users.getPassword());
        users.setPassword(hashedPassword);
        em.persist(users);
    }
}