[Toc]

#1. PasswordEncoder 采用密码加密

使用前面的例子。可以看出我们数据库密码是采用明文的,我们在登录的时候也需要将传递的明文密码使用对应的算法加密后再与保存好的密码比较,这样比较好,Spring-Security也有支持通过在authentication-provider下定义一个password-encoder我们可以定义当前AuthenticationProvider需要在进行认证时需要使用的password-encoder。password-encoder是一个PasswordEncoder的实例,我们可以直接使用它,如:

    <!-- 认证管理器,配置了管理员与角色的关系  -->
<security:authentication-manager alias="authenticationManager">
<!-- accountService 实现了UserDetailsService接口
-->
<security:authentication-provider user-service-ref="accountService">
<!-- 密码采用md5加密 -->
<security:password-encoder hash="md5" />
</security:authentication-provider>
</security:authentication-manager>
上面的例子只是简单是使用md5加密。
  • 其属性hash表示我们将用来进行加密的哈希算法,系统已经为我们实现的有plaintext、sha、sha-256、md4、md5、和。它们对应的PasswordEncoder实现类如下:
加密算法 PasswordEncoder实现类
plaintext PlaintextPasswordEncoder
sha ShaPasswordEncoder
sha-256 ShaPasswordEncoder,使用时new ShaPasswordEncoder(256)
md4 Md4PasswordEncoder
md5 Md5PasswordEncoder
LdapShaPasswordEncoder
LdapShaPasswordEncoder
  • 使用BASE64编码加密后的密码

<security:password-encoder hash="md5" base64="true"/>
  • 加密时使用salt


      <!-- (1)下面的配置将使用常量“abc”作为salt。 -->

   <security:authentication-manager>

      <security:authentication-provider user-service-ref="userDetailsService">

         <security:password-encoder hash="md5" base64="true">

            <security:salt-source system-wide="abc"/>

         </security:password-encoder>

      </security:authentication-provider>

   </security:authentication-manager>

       <!--(2)下面的配置将使用UserDetails的username作为salt。 -->

   <security:authentication-manager>

      <security:authentication-provider user-service-ref="userDetailsService">

         <security:password-encoder hash="md5" base64="true">

            <security:salt-source user-property="username"/>

         </security:password-encoder>

      </security:authentication-provider>

   </security:authentication-manager>

来源: http://www.mincoder.com/article/3505.shtml

#2. 获取当前的用户信息

spring-security 登录的时候,会将用户信息放到 session中,其中的key为 “SPRING_SECURITY_CONTEXT”,spring-security也给我们提供了获取方案


Account account = (Account) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
  • 如果想管理我们用户信息。我们可以添加一个sessionAttribute监听器,来监听SPRING_SECURITY_CONTEXT,当spring-security中SPRING_SECURITY_CONTEXT有变化时,我们可以同步到我们自己的session中。这样方便我们自己管理用户信息。

package com.hp.listener; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.security.core.context.SecurityContext; import org.springframework.security.core.context.SecurityContextHolder; import com.hp.model.Account; import com.hp.utils.ConfigUtil; public class UserHttpSessionAttributeListener implements HttpSessionAttributeListener { protected final Log logger = LogFactory.getLog(UserHttpSessionAttributeListener.class); private SecurityContext context = null; private final String springSecurityContext = "SPRING_SECURITY_CONTEXT"; /** * session添加 */ @Override public void attributeAdded(HttpSessionBindingEvent event) { // logger.info("attributeAdded---->" + event.getName() + event.getValue()); // 当前session中有新的属性时触发 SPRING_SECURITY_CONTEXT 是spring存放的key if (event.getName().equals(springSecurityContext)) { context = SecurityContextHolder.getContext(); Account account = (Account) context.getAuthentication().getPrincipal(); event.getSession().setAttribute(ConfigUtil.getSessionInfoName(), account); } } @Override public void attributeRemoved(HttpSessionBindingEvent event) { // 在注销的时候,security已经销毁的整个session } /** * session替换 */ @Override public void attributeReplaced(HttpSessionBindingEvent event) { if (event.getName().equals(springSecurityContext)) { // System.out.println("----session中更新登陆信息------"); context = SecurityContextHolder.getContext(); Account account = (Account) context.getAuthentication().getPrincipal(); event.getSession().setAttribute(ConfigUtil.getSessionInfoName(), account); } } }

Spring-Security (学习记录五)--配置登录时,密码采用md5加密,以及获取登录信息属性监听同步自己想要的登录信息的更多相关文章

  1. Spring Security 学习记录

    一.核心拦截器详细说明 1.WebAsyncManagerIntegrationFilter 根据请求封装获取WebAsyncManager 从WebAsyncManager获取/注册Security ...

  2. SpringBoot + Spring Security 学习笔记(五)实现短信验证码+登录功能

    在 Spring Security 中基于表单的认证模式,默认就是密码帐号登录认证,那么对于短信验证码+登录的方式,Spring Security 没有现成的接口可以使用,所以需要自己的封装一个类似的 ...

  3. spring security实现记录用户登录时间等信息

    目录 spring security实现记录用户登录时间等信息 一.原理分析 二.实现方式 2.1 自定义AuthenticationSuccessHandler实现类 2.2 在spring-sec ...

  4. Spring Security学习笔记

    Spring Web Security是Java web开发领域的一个认证(Authentication)/授权(Authorisation)框架,基于Servlet技术,更确切的说是基于Servle ...

  5. [转]Spring Security学习总结一

    [总结-含源码]Spring Security学习总结一(补命名空间配置) Posted on 2008-08-20 10:25 tangtb 阅读(43111) 评论(27)  编辑  收藏 所属分 ...

  6. SpringBoot + Spring Security 学习笔记(三)实现图片验证码认证

    整体实现逻辑 前端在登录页面时,自动从后台获取最新的验证码图片 服务器接收获取生成验证码请求,生成验证码和对应的图片,图片响应回前端,验证码保存一份到服务器的 session 中 前端用户登录时携带当 ...

  7. [转]Spring Security学习总结二

    原文链接: http://www.blogjava.net/redhatlinux/archive/2008/08/20/223148.html http://www.blogjava.net/red ...

  8. spring boot rest 接口集成 spring security(1) - 最简配置

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  9. Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客

    ==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...

随机推荐

  1. Delphi Close、Halt、terminate、ExitProcess的区别

    Close:1.只关闭本窗体2.当Close是一个主窗体时,程序会退出.3.Close会发生FormClose事件,FormCloseQuery事件4.主窗体close以后程序就Application ...

  2. go结构体上的函数

    go结构体上的函数 我们可以将一个方法和一个结构体关联: type Saiyan struct { Name string Power int } func (s *Saiyan) Super() { ...

  3. iscroll refresh无效解决办法

    最近用iscroll.js 写移动页面,效果还是挺好的.但,还是会遇到重新初始化的问题. var myScroll = new IScroll('#rule_wrapper',{ click:true ...

  4. mockjs 使用以及反向校验

    一.背景 前端开发需要依赖后端接口 后端接口输出慢.接口规范随时可能会变,而前端毫无感知 前端需要自己 mock 假数据 json 文件 假数据 json 数据内容是静态的,测试不同返回情况需要修改 ...

  5. Tyvj 1518 CPU监控(线段树)

    题目描述: Bob需要一个程序来监视CPU使用率.这是一个很繁琐的过程,为了让问题更加简单,Bob会慢慢列出今天会在用计算机时做什么事. Bob会干很多事,除了跑暴力程序看视频之外,还会做出去玩玩和用 ...

  6. 暴力——cf1202C

    直接去考虑细节很多,不如暴力做 即在四个方向到达最远前向反方向走一步,答案肯定是从这四种情况+不多走里出的 #include<bits/stdc++.h> using namespace ...

  7. HTML标签类总结

    1.a标签除了可以作为连接也可以发送邮箱,a标签里的文本颜色不能继承父级的. 2.有几个特殊的块级元素只能包含内嵌元素,不能再包含块级元素,这几个特殊的标签是:h1.h2.h3.h4.h5.h6.p. ...

  8. 包管理工具(npm、yarn)

    npm包管理工具 1. npm的包安装分为本地安装(local).全局安装(global)两种,从敲的命令行来看,差别只是有没有-g而已. 2. 这两种安装方式的区别: 本地安装(安装在命令行运行所在 ...

  9. jquery中的ajax方法参数的用法和他的含义

    jquery中的ajax方法参数的用法和他的含义: 1.url:  要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type:  要求为String类型的参数,请求方式(pos ...

  10. 24. Jmeter GUI 及NON GUI实现分布式

    什么是分布式: Jmeter的集群模式可以让我们将多台机器联合起来一起产生负载,从而弥补单台机器负载生成能力不足的问题. 假设我们的测试计划会产生100个threads,我们使用6台机器进行分布式测试 ...