我很惊讶,我发现在这个主题上搜索很少。我使用Spring Security来允许用户登录到Spring MVC应用程序。我还在服务层中使用Hibernate来保存对数据库的更改(我将用户信息保留在我用于应用程序其余部分的同一个数据库中)。在这种环境下(使用Spring Security的优点)我怎么能够让用户更改自己的密码?如何在使用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;
}
我新的这个,所以任何帮助将不胜感激。谢谢。
对于通过更改任何其他属性(例如电子邮件)来更改密码通常没有什么特别之处。 Spring Security旨在验证和授权。这只是一个数据库更新。 –
所以在弹簧安全性中没有使这种自动/简单的功能? –
没有我知道的功能。 –