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 上下文中的所有当前线程中的值(看起来挺好用的)。
- spring security3.x学习(16)_JdbcUserDetailManager的使用
- spring security3.x学习(16)_JdbcUserDetailManager的使用
- spring security3.x学习笔记
- spring security3.x学习(1)
- spring security3.x学习(14)_关系型数据库使用的UserDetailsService(JdbcDaoImpl)
- spring security3.x学习(14)_关系型数据库使用的UserDetailsService(JdbcDaoImpl)
- spring security3.x学习(4)_认证管理的过程
- spring security3.x学习(10)_自定义的登录页面
- spring security3.x学习(19)_tomcat的SSL
- spring security3.x学习(22)_关于ip的过滤器
- spring security3.x学习(4)_认证管理的过程
- spring security3.x学习(10)_自定义的登录页面
- spring security3.x学习(19)_tomcat的SSL
- spring security3.x学习(22)_关于ip的过滤器
- spring security3.x学习(11)_退出
- spring security3.x学习(12)_remember me
- spring security3.x学习(17)_GBAC
- spring security3.x学习(11)_退出
- UVALive 3605 Roommate
- spring security3.x学习(15)_扩展JdbcDaoImpl。
- 如何设等概率的随机函数
- hibernate中表名和Modle类名不同时的配置
- vim使用笔记
- spring security3.x学习(16)_JdbcUserDetailManager的使用
- 黑马程序员_正则表达式
- mybatis generator 配置文件解析
- cocos2d-x 场景切换时由资源预加载问题引起的场景无法切换的问题
- Code Jam 2010 Round 1B Problem A
- 求两个串中的第一个最长子串
- 阿里研发一笔试题foo(2^31-3)
- 2014年创新工场校园招聘笔试题及答案
- Linux下几个常用的快捷键