使用Spring Security时如何处理密码更改

时间:2014-02-22 19:01:42

标签: hibernate spring-mvc spring-security change-password

我很惊讶我在这个问题上找不到什么。我正在使用Spring Security来允许用户登录Spring MVC应用程序。我也在服务层使用Hibernate来持久更改db(我将用户信息保存在我用于应用程序其余部分的同一个db中)。在这种环境中(利用Spring Security的优势),如何让用户更改自己的密码?

这是我的spring-security.xml

    <http auto-config="true" use-expressions="true">
        <intercept-url pattern="/essays/auth/login" access="permitAll"/>
        <intercept-url pattern="/essays/auth/logout" access="permitAll"/>
        <intercept-url pattern="/essays/auth/denied" access="hasRole('ROLE_USER')"/>
        <intercept-url pattern="/" access="hasRole('ROLE_USER')"/>
        /* more intercept urls */

        <form-login login-page="/essays/auth/login"
                    authentication-failure-url="/essays/auth/login?error=true"
                    default-target-url="/essays/main/student/search"
                    always-use-default-target="true"/>

        <access-denied-handler error-page="/essays/auth/denied"/>

        <logout invalidate-session="true"
                logout-success-url="/essays/auth/login"
                logout-url="/essays/auth/logout"/>
    </http>

    <authentication-manager>
        <authentication-provider user-service-ref="customUserDetailsService">
            <password-encoder hash="md5"/>
        </authentication-provider>
    </authentication-manager>

我实施了自定义用户详细信息服务:

@Service
@Transactional(readOnly = true)
public class CustomUserDetailsService implements UserDetailsService {

    @Resource
    private UserService userService;

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        try {
            org.jana.domain.User domainUser = userService.getByUsername(username);
            boolean enabled = true;
            boolean accountNonExpired = true;
            boolean credentialsNonExpired = true;
            boolean accountNonLocked = true;
            return new User(
                    domainUser.getUsername(),
                    domainUser.getPassword().toLowerCase(),
                    enabled,
                    accountNonExpired,
                    credentialsNonExpired,
                    accountNonLocked,
                    getAuthorities(domainUser.getRole().getRole()));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
...

这是处理Hibernate的服务:

@Service("userService")
@Transactional
public class UserService {

    @Resource(name="sessionFactory")
    private SessionFactory sessionFactory;

    public User getByUsername(String username) {

        Session session = sessionFactory.getCurrentSession();

        String hql = "FROM User u WHERE u.username = :username";
        Query query = session.createQuery(hql);
        query.setParameter("username", username);
        User user = (User)query.uniqueResult();

        return user;
    }

我是新手,所以任何帮助都将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:3)

我处理密码修改,就像修改任何其他实体字段一样。

在这种情况下,您可以为hipotetic用户对象创建更新表单。当您在db中保存用户实体时,可能需要保存散列密码,处理salt等。但这不是弹簧安全工作。