spring-security - 如何在 Spring Security 中使用数据库字段中的盐

标签 spring-security

我正在使用 spring-security-3.1.0 和 spring-framework-3.0.6。
对于登录安全检查,我正在使用盐。但是在盐中使用盐时遇到问题
来源。
如果我使用 bean:property name="userPropertyToUse"value="username"那么
一切安好
但在 <beans:property name="userPropertyToUse" value="lalt"> 中有问题
即使很艰难,我已经为 "salt" 配置了所有必要的配置.
它播下这条信息
无法在用户对象上找到 salt 方法。类'org.springframework.security.core.userdetails.User'
有一个名为“盐”的方法或 setter/getter 吗?


我的 spring-security.xml 看起来像这样

<beans:bean id="saltSource" class="org.springframework.security.authentication.dao.ReflectionSaltSource">
    <beans:property name="userPropertyToUse" value="salt" />
</beans:bean> 
<beans:bean id="passwordEncoder" class="org.springframework.security.authentication.encoding.ShaPasswordEncoder"/>

<beans:bean id="loggerListener" class="org.springframework.security.authentication.event.LoggerListener" /> 

<authentication-manager>
    <authentication-provider user-service-ref="jdbcUserService">
        <password-encoder ref="passwordEncoder">
            <salt-source ref="saltSource"/>
        </password-encoder> 
    </authentication-provider>
</authentication-manager> 

<beans:bean id="jdbcUserService" class="controllers.CustomJdbcDaoImpl">
    <beans:property name="dataSource" ref="dataSource"/>
    <beans:property name="usersByUsernameQuery">
        <beans:value>SELECT U.userid AS username, 
            U.userpassword as password, 
            'true' AS enabled,
            U.userpasswordsalt AS salt 
            FROM users U WHERE U.userid=?
        </beans:value>
    </beans:property>
    <beans:property name="authoritiesByUsernameQuery">
        <beans:value>SELECT U.userid AS username,
            U.userrole as authority 
            FROM users U 
            WHERE U.userid=?
        </beans:value>
    </beans:property>
</beans:bean> 

我的盐 jdbcUserService.java 是

public class CustomJdbcDaoImpl extends JdbcDaoImpl {
    @Override
    protected List<UserDetails> loadUsersByUsername(String username) {
        return getJdbcTemplate().query(getUsersByUsernameQuery(),new String[] {username},
            new RowMapper<UserDetails>() {
                public UserDetails mapRow(ResultSet rs, int rowNum)throws SQLException {
                    String username = rs.getString(1);
                    String password = rs.getString(2);
                    boolean enabled = rs.getBoolean(3);
                    String salt = rs.getString(4);
                    System.out.println("CustomJdbcDaoImpl Salt : "+salt);
                    return new SaltedUser(username, password,enabled, true, true, true,AuthorityUtils.NO_AUTHORITIES, salt);
                }
            });
    } 
}

我的 SaltedUser.java 是

public class SaltedUser extends User{
    private String salt;
    public SaltedUser(String username, String password,boolean enabled,
            boolean accountNonExpired, boolean credentialsNonExpired,
            boolean accountNonLocked, List<GrantedAuthority>authorities, String salt) {
        super(username, password, enabled,accountNonExpired, credentialsNonExpired,accountNonLocked, authorities);
        this.salt = salt;
        System.out.println("SaltedUser Salt : "+salt);
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }
}

任何人都可以帮助我......?

最佳答案

您需要覆盖 createUserDetails创建最终 UserDetails 的方法类返回的实现。查看 JdbcDaoImpl 的来源.

请注意,如果您不是为已经有密码散列系统的遗留系统构建它,那么使用类似 BCrypt 的东西。对您的密码进行编码将是一个更好、更简单的选择。

关于spring-security - 如何在 Spring Security 中使用数据库字段中的盐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9631977/

相关文章:

java - 将 Spring MVC 应用程序从 JSP 迁移到 AngularJS

java - 在 AuthenticationSuccessHandler 中添加权限

java - Spring security 授权从数据库请求值

spring-mvc - Spring Security记住我的cookie Unicode字符错误

java - Spring "The request was rejected because the URL was not normalized."如何判断使用了什么 url?

spring-security - 应该使用什么代替 1.4.0.RELEASE 中弃用的 SpringBootServletInitializer

authentication - 在Grails Spring Security插件中自定义登录

java - 为什么 Spring Security HTTP BASIC 重定向到错误的 URL?

web-services - 防止 token 共享的最佳方法是什么?

grails - 如何使用 Spring Security 插件获取当前用户角色?