spring security3.x学习(16)_JdbcUserDetailManager的使用

来源:互联网 发布:c语言中volatile byte 编辑:程序博客网 时间:2024/04/30 05:14
我们看一看UserDetailsService为我们提供了那些实现类。

里边还提供了一个名为JdbcuserDetailsManager的JdbcDaoImpl(UserDetailsService的子类)的实现类。那他究竟是如何扩展jdbcDaoImpl的呢?



他的类中,又定义了很多的SQL语句,而且添加了很多方法,很明显,他对JdbcDaoImpl进行了功能上的扩展。
书中提供了一些扩展的举例:



如果是这样的话,上次的修改密码的功能,它也帮助我们实现了,写到这,我们可以猜出来,其实配置它和配置上一个自定义的JdbcDaoImpl是一样的。
    <bean id="jdbcUserService" class="org.springframework.security.provisioning.JdbcUserDetailsManager" >
        <property name= "dataSource" ref="dataSource" />
        <property name="authenticationManager" ref="authenticationManager" />
    </bean >
spring security配置:
    <authentication-manager alias="authenticationManager" >
        <authentication-provider user-service-ref="jdbcUserService" />
    </authentication-manager >
这个配置,唯一一个优点区别的就是要在jdbcUserService中配置一下authenticationManager,那么为什么要设置这个属性呢?、我们通过查看源码就可以找到答案的:
/*     */    protected void initDao() throws ApplicationContextException
/*     */    {
/* 129 */     if (this. authenticationManager == null) {
/* 130 */       this.logger.info( "No authentication manager set. Reauthentication of users when changing passwords will not be performed.");
/*     */      }
/*     */
/* 134 */     super.initDao();
/*     */    }


/*     */    public void changePassword(String oldPassword, String newPassword) throws AuthenticationException {
/* 192 */     Authentication currentUser = SecurityContextHolder.getContext().getAuthentication();
/*     */
/* 194 */     if (currentUser == null)
/*     */      {
/* 196 */       throw new AccessDeniedException("Can't change password as no Authentication object found in context for current user.");
/*     */      }
/*     */
/* 200 */     String username = currentUser.getName();
/*     */
/* 203 */     if (this. authenticationManager != null) {
/* 204 */       this.logger .debug("Reauthenticating user '" + username + "' for password change request.");
/*     */
/* 206 */       this.authenticationManager .authenticate(new UsernamePasswordAuthenticationToken(username, oldPassword));
/*     */      } else {
/* 208 */       this.logger.debug( "No authentication manager set. Password won't be re-checked.");
/*     */      }
/*     */
/* 211 */     this.logger.debug( "Changing password for user '" + username + "'");
/*     */
/* 213 */     getJdbcTemplate().update(this .changePasswordSql , new Object[] { newPassword, username });
/*     */
/* 215 */     SecurityContextHolder.getContext().setAuthentication(createNewAuthentication(currentUser, newPassword));
/*     */
/* 217 */     this.userCache.removeUserFromCache(username);
/*     */    }

我们可以推断出来,只有更改密码时会使用到,所以如果不设置authenticationManager属性的话,更改密码会失败的。
spring mvc中是这样修改密码的:
    @RequestMapping(value= "/account/changePassword.do",method=RequestMethod.POST)
    public String submitChangePasswordPage(@RequestParam ("password" ) String newPassword) {
       Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

       String username = principal.toString();
        if (principal instanceof UserDetails) {
         username = ((UserDetails)principal).getUsername();
       }
       
        changePasswordDao.changePassword( username, newPassword);
        SecurityContextHolder. clearContext();
       
        return "redirect:home.do" ;
    }
那么。这里边涉及到了一个叫做SecurityContextHolder的对象,这个对象是做什么用的呢?
它可以获取认证信息(通过认证流程以后会返回一个新的Authentication,前几次我们已经说过了):SecurityContextHolder.getContext().getAuthentication();
返回Authentication然后通过getPrincipal()方法获取安全实体(我们把它想象成安全对像就行),返回值是Object类型,也就是说,他可以使任何对象类型,一般来说是UserDetails,但有时也是username(这个一般是UserDetails中的一部分).
获取用户名完成以后,可以通过我们前边定义好的修改密码类(JdbcUserDetailsManager)实现修改密码。
接着又有一句话叫做:SecurityContextHolder. clearContext(); 那么它完成了什么功能呢,很遗憾,我再电子书和官方的帮助文档中都没有找到,所以我查询了spring security API:


就是说,当我调用这个方法以后,我们将清除Security 上下文中的所有当前线程中的值(看起来挺好用的)。