《Spring Security3》第三章第四部分翻译(修改密码)

实现修改密码管理

现在我们将要对基于内存的UserDetailsService进行简单的扩展以使其支持用户修改密码。因为这个功能对用户名和密码存于数据库的场景更有用,所以基于o.s.s.core.userdetails.memory.InMemoryDaoImpl扩展的实现不会关注存储机制,而是关注框架对这种方式扩展的整体流程和设计。在第四章中,我们将通过将其转移到数据库后台存储来进一步扩展我们的基本功能。

扩展基于内存的凭证存储以支持修改密码

Spring Security框架提供的InMemoryDaoImpl内存凭证存储使用了一个简单的map来存储用户名以及关联的UserDetails。InMemoryDaoImpl使用的UserDetails实现类是o.s.s.core.userdetails.User,这个实现类将会在Spring Security API中还会看到。

 

这个扩展的设计有意的进行了简化并省略了一些重要的细节,如需要用户在修改密码前提供他们的旧密码。添加这些功能将作为练习留给读者。

用InMemoryChangePasswordDaoImpl扩展InMemoryDaoImpl

我们要首先写自定义的类来扩展基本的InMemoryDaoImpl,并提供允许用户修改密码的方法。因为用户是不可改变的对象,所以我们copy已经存在的User对象,只是将密码替换为用户提交的值。在这里我们定义一个接口在后面的章节中将会重用,这个接口提供了修改密码功能的一个方法:

 

Java代码   收藏代码
  1. package com.packtpub.springsecurity.security;  
  2. // imports omitted  
  3. public interface IChangePassword extends UserDetailsService {    
  4. void changePassword(String username, String password);  
  5. }  

 以下的代码为基于内存的用户数据存储提供了修改密码功能:

 

Java代码   收藏代码
  1. package com.packtpub.springsecurity.security;  
  2. public class InMemoryChangePasswordDaoImpl extends InMemoryDaoImpl   
  3. implements IChangePassword {  
  4.   @Override  
  5.   public void changePassword(String username,   
  6.                 String password) {  
  7.     // get the UserDetails  
  8.     User userDetails =   
  9.         (User) getUserMap().getUser(username);  
  10.     // create a new UserDetails with the new password  
  11.     User newUserDetails =   
  12.         new User(userDetails.getUsername(),password,  
  13.         userDetails.isEnabled(),   
  14.         userDetails.isAccountNonExpired(),  
  15.       userDetails.isCredentialsNonExpired(),  
  16.         userDetails.isAccountNonLocked(),  
  17.         userDetails.getAuthorities());  
  18.     // add to the map  
  19.     getUserMap().addUser(newUserDetails);  
  20.   }  
  21. }  

 比较幸运的是,只有一点代码就能将这个简单的功能加到自定义的子类中了。我们接下来看看添加自定义UserDetailsService到pet store应用中会需要什么样的配置。

配置Spring Security来使用InMemoryChangePasswordDaoImpl

现在,我们需要重新配置Spring Security的XML配置文件以使用新的UserDetailsService实现。这可能比我们预想的要困难一些,因为<user-service>元素在Spring Security的处理过程中有特殊的处理。需要明确声明我们的自定义bean并移除我们先前声明的<user-service>元素。我们需要把:

 

Xml代码   收藏代码
  1. <authentication-manager alias="authenticationManager">  
  2.   <authentication-provider>  
  3.     <user-service id="userService">  
  4.       <user authorities="ROLE_USER" name="guest" password="guest"/>  
  5.     </user-service>  
  6.   </authentication-provider>  
  7. </authentication-manager>  

 修改为:

 

Xml代码   收藏代码
  1. <authentication-provider user-service-ref="userService"/>  

 在这里我们看到的user-service-ref属性,引用的是一个id为userService的Spring Bean。所以在dogstore-base.xml Spring Beans配置文件中,声明了如下的bean:

 

Xml代码   收藏代码
  1. <bean id="userService" class="com.packtpub.springsecurity.security.  
  2. InMemoryChangePasswordDaoImpl">  
  3.   <property name="userProperties">  
  4.     <props>  
  5.       <prop key="guest">guest,ROLE_USER</prop>  
  6.     </props>  
  7.   </property>  
  8. </bean>  

 你可能会发现,这里声明用户的语法不如<user-service>包含的<user>元素更易读。遗憾的是,<user>元素只能使用在默认的InMemoryDaoImpl实现类中,我们不能在自定义的UserDetailsService中使用了。在这里例子中,这个限制使得事情稍微复杂了一点,但是在实际中,没有人会愿意长期的将用户定义信息放在配置文件中。对于感兴趣的读者,Spring Security 3参考文档中的6.2节详细描述了以逗号分隔的提供用户信息的语法。

【高效使用基于内存的UserDetailsService。有一个常见的场景使用基于内存的UserDetailsService和硬编码的用户列表,那就是编写安全组件的单元测试。编写单元测试的人员经常编码或配置最简单的场景来测试组件的功能。使用基于内存的UserDetailsService以及定义良好的用户和GrantedAuthority值为测试编写人员提供了很可控的测试环境。】

 

到现在,你可以重启JBCP Pets应用,应该没有任何的配置错误报告。我们将在这个练习的最后的两步中,完成UI的功能。

构建一个修改密码的页面

我们接下来将会建立一个允许用户修改密码的简单页面。



 这个页面将会通过一个简单的链接添加到“My Account”页面。首先,我们在/account/home.jsp文件中添加一个链接:

 

Html代码   收藏代码
  1. <p>  
  2.   Please find account functions below...  
  3. </p>  
  4. <ul>  
  5.   <li><a href="changePassword.do">Change Password</a></li>  
  6. </ul>  

 接下来,在/account/ changePassword.jsp文件中建立“Change Password”页面本身:

 

Html代码   收藏代码
  1. <?xml version="1.0" encoding="ISO-8859-1" ?>  
  2. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
  3. pageEncoding="ISO-8859-1"%>  
  4. <jsp:include page="../common/header.jsp">  
  5.   <jsp:param name="pageTitle" value="Change Password"/>  
  6. </jsp:include>  
  7. <h1>Change Password</h1>  
  8. <form method="post">  
  9.   <label for="password">New Password</label>:  
  10.   <input id="password" name="password" size="20" maxlength="50"   
  11. type="password"/>  
  12.   <br />  
  13.   <input type="submit" value="Change Password"/>   
  14. </form>  
  15. <jsp:include page="../common/footer.jsp"/>  
 

 

最后我们还要添加基于Spring MVC的AccountController来处理密码修改的请求(在前面的章节中我们没有介绍AccountController,它是账号信息主页的简单处理类)。

为AccountController添加修改密码的处理

我们需要将对自定义UserDetailsService的应用注入到com.packtpub.springsecurity.web.controller.AccountController,这样我们就能使用修改密码的功能了。Spring的@Autowired注解实现了这一功能:

 

Java代码   收藏代码
  1. @Autowired  
  2. private IChangePassword changePasswordDao;  

 两个接受请求的方法分别对应渲染form以及处理POST提交的form数据:

 

Java代码   收藏代码
  1. @RequestMapping(value="/account/changePassword.  
  2. do",method=RequestMethod.GET)  
  3. public void showChangePasswordPage() {    
  4. }  
  5. @RequestMapping(value="/account/changePassword.  
  6. do",method=RequestMethod.POST)  
  7. public String submitChangePasswordPage(@RequestParam("password")   
  8. String newPassword) {  
  9.   Object principal = SecurityContextHolder.getContext().  
  10. getAuthentication().getPrincipal();  
  11.   String username = principal.toString();  
  12.   if (principal instanceof UserDetails) {  
  13. username = ((UserDetails)principal).getUsername();  
  14. }  
  15.   changePasswordDao.changePassword(username, newPassword);  
  16.   SecurityContextHolder.clearContext();  
  17.   return "redirect:home.do";  
  18. }  

 完成这些配置后,重启应用,并在站点的“My Account”下找到“Change Password”功能。

练习笔记

比较精细的读者可能意识到这个修改密码的form相对于现实世界的应用来说太简单了。确实,很多的修改密码实现要复杂的多,并可能包含如下的功能:

l  密码确认——通过两个文本框,确保用户输入的密码是正确的;

l  旧密码确认——通过要求用户提供要修改的旧密码,增加安全性(这对使用remember me功能的场景特别重要);

l  密码规则校验——检查密码的复杂性以及密码是否安全。

 

你可能也会注意到当你使用这个功能的时,会被自动退出。这是因为SecurityContextHolder.clearContext()调用导致的,它会移除用户的SecurityContext并要求他们重新认证。在练习中,我们需要给用户做出提示或者找到方法让用户免于再次认证。

小结

在本章中,我们更细节的了解了认证用户的生命周期并对JBCP Pet  Store进行了结构性的修改。我们通过添加真正的登录和退出功能,进一步的满足了安全审计的要求,并提升了用户的体验。我们也学到了如下的技术:

l  配置并使用基于Spring MVC的自定义用户登录界面;

l  配置Spring Security的退出功能;

l  使用remember me功能;

l  通过记录IP地址,实现自定义的remember me功能;

l  实现修改密码功能;

l  自定义UserDetailsService和InMemoryDaoImpl。

在第四章中,我们将会使用基于数据库的认证信息存储并学习怎样保证数据库中的密码和其他敏感数据的安全。


练习代码:不知道对不对 


@RequestMapping(value="/account/changePassword.do",method=RequestMethod.POST)
public String submitChangePasswordPage(@RequestParam("password") String newPassword
,String password2,String oldPassword,Model model,HttpServletRequest  request) {
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();


String username = principal.toString();
String password = "";
if (principal instanceof UserDetails) {
 username = ((UserDetails)principal).getUsername();
 password = ((UserDetails)principal).getPassword();
}
if(!oldPassword.equals(password)){
model.addAttribute("a","oldPassword error");
return "account/changePassword";
}
if(!newPassword.equals(password2)){
model.addAttribute("b","newpassword error");
return "account/changePassword";
}
changePasswordDao.changePassword(username, newPassword);
SecurityContextHolder.clearContext();

UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, newPassword);
        try {
            token.setDetails(new WebAuthenticationDetails(request));
            request.getSession();
            SecurityContextHolder.getContext().setAuthentication(token);
            request.getSession().setAttribute(
                            HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY,
                            SecurityContextHolder.getContext());
        } catch (AuthenticationException e) {
            System.out.println("Authentication failed: " + e.getMessage());
            return "account/changePassword";
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值