之前我们都是使用MD5 Md5PasswordEncoder 或者SHA ShaPasswordEncoder 的哈希算法进行密码加密,在spring security中依然使用只要指定使用自定义加密算法就行,现在推荐spring使用的BCrypt BCryptPasswordEncoder,一种基于随机生成salt的根据强大的哈希加密算法。
首先我们使用spring提供的加密方法对密码 123456 进行加密:
1、使用MD5加密:
package com.petter.util;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
/**
* @author hongxf
* @since 2017-04-11 10:52
*/
public class MD5EncoderGenerator {
public static void main(String[] args) {
Md5PasswordEncoder encoder = new Md5PasswordEncoder();
System.out.println(encoder.encodePassword("123456", "hongxf"));
}
}
修改数据库中的用户hxf密码为 7cbdf569746dd62484eb25a55b7df2dc
2、使用BCrypt加密:
package com.petter.util;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
/**
* @author hongxf
* @since 2017-04-10 10:01
*/
public class PasswordEncoderGenerator {
public static void main(String[] args) {
String password = "123456";
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String hashedPassword = passwordEncoder.encode(password);
System.out.println(hashedPassword);
}
}
修改数据库中的用户hxf密码为 $2a$10$f0DEGrkIpYyzcFrf/fTMSOAKl1Y/XHpKaijWdfiWnOOzGTEs8diLi
这里需要注意,保证数据库密码字段的长度为60或者大于60,否则字符串会被截断。
 
一、使用MD5加密算法:
spring security已经废弃了org.springframework.security.authentication.encoding.PasswordEncoder接口,推荐使用org.springframework.security.crypto.password.PasswordEncoder接口
这里需要自定义。
1、建立自定义密码加密实现类CustomPasswordEncoder
package com.petter.config;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
/**
* @author hongxf
* @since 2017-04-11 10:39
*/
public class CustomPasswordEncoder implements PasswordEncoder {
@Override
public String encode(CharSequence rawPassword) {
Md5PasswordEncoder encoder = new Md5PasswordEncoder();
return encoder.encodePassword(rawPassword.toString(), "hongxf");
}
@Override
public boolean matches(CharSequence rawPassword, String encodedPassword) {
Md5PasswordEncoder encoder = new Md5PasswordEncoder();
return encoder.isPasswordValid(encodedPassword, rawPassword.toString(), "hongxf");
}
}
2、在SecurityConfig中进行配置
@Bean
public PasswordEncoder passwordEncoder(){
return new CustomPasswordEncoder();
//return new BCryptPasswordEncoder();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
authenticationProvider.setPasswordEncoder(passwordEncoder());
auth.authenticationProvider(authenticationProvider);
}
直接把自定义的类设置即可。
同样适用于SHA加密。
二、使用BCrypt加密算法:
1、只需要在SecurityConfig中进行配置
@Bean
public PasswordEncoder passwordEncoder(){
//return new CustomPasswordEncoder();
return new BCryptPasswordEncoder();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
authenticationProvider.setPasswordEncoder(passwordEncoder());
auth.authenticationProvider(authenticationProvider);
}
 
PS:如果使用的是jdbcAuthentication,安装如下配置即可
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(passwordEncoder())
.usersByUsernameQuery("select username,password, enabled from users where username = ?")
.authoritiesByUsernameQuery("select username, role from user_roles where username = ?");
}
启动测试即可
 
 
 

spring security使用哈希加密的密码的更多相关文章

  1. 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_09-SpringSecurityOauth2研究-Oauth2密码模式授权

    密码模式(Resource Owner Password Credentials)与授权码模式的区别是申请令牌不再使用授权码,而是直接 通过用户名和密码即可申请令牌. 测试如下: Post请求:htt ...

  2. 关于 spring security 对用户名和密码的校验过程

    1.执行 AuthenticationManager 认证方法 authenticate(UsernamePasswordAuthenticationToken) 2.ProviderManager ...

  3. springboot+spring security +oauth2.0 demo搭建(password模式)(认证授权端与资源服务端分离的形式)

    项目security_simple(认证授权项目) 1.新建springboot项目 这儿选择springboot版本我选择的是2.0.6 点击finish后完成项目的创建 2.引入maven依赖  ...

  4. Spring Security 介绍与Demo

    一.Spring Security 介绍 Spring Security 是针对Spring项目的安全框架,也是Spring Boot底层安全模块的默认技术选型.我们仅需引入spring-boot-s ...

  5. Spring Security 自定义登录认证(二)

    一.前言 本篇文章将讲述Spring Security自定义登录认证校验用户名.密码,自定义密码加密方式,以及在前后端分离的情况下认证失败或成功处理返回json格式数据 温馨小提示:Spring Se ...

  6. 用Spring Security, JWT, Vue实现一个前后端分离无状态认证Demo

    简介 完整代码 https://github.com/PuZhiweizuishuai/SpringSecurity-JWT-Vue-Deom 运行展示 后端 主要展示 Spring Security ...

  7. SpringBoot集成Spring Security(4)——自定义表单登录

    通过前面三篇文章,你应该大致了解了 Spring Security 的流程.你应该发现了,真正的 login 请求是由 Spring Security 帮我们处理的,那么我们如何实现自定义表单登录呢, ...

  8. Spring Boot整合Spring Security自定义登录实战

    本文主要介绍在Spring Boot中整合Spring Security,对于Spring Boot配置及使用不做过多介绍,还不了解的同学可以先学习下Spring Boot. 本demo所用Sprin ...

  9. Spring Security Web应用入门环境搭建

    在使用Spring Security配置Web应用之前,首先要准备一个基于Maven的Spring框架创建的Web应用(Spring MVC不是必须的),本文的内容都是基于这个前提下的. pom.xm ...

随机推荐

  1. 【转】Linux下mysql操作

    本文转自:http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/10/18/2216279.html 1.linux下启动mysql的命令:my ...

  2. python bottle学习(一)快速入门

    from bottle import (run, route, get, post, put, delete) # bottle中添加路由的两种方法 # 第一种,使用route装饰器,需要指定meth ...

  3. 【转载&总结】后缀数组及广泛应用

    转自:http://blog.csdn.net/yxuanwkeith/article/details/50636898 五分钟搞懂后缀数组!后缀数组解析以及应用(附详解代码) 作者:YxuanwKe ...

  4. 《从零开始学Swift》学习笔记(Day 56)——命名规范Swift编码规范之命名规范

    原创文章,欢迎转载.转载请注明:关东升的博客 程序代码中到处都是自己定义的名字,取一个有样并且符合规范的名字非常重要. 命名方法很多,但是比较有名的,广泛接受命名法有: 匈牙利命名,一般只是命名变量, ...

  5. MySQL中的注释(有三种)

    MysQL支持三种注释: .#... (推荐这种,具有通性) ."-- ..." (注意--后面有一个空格) ./*...*/

  6. 史上最易懂的大数据 OTO

    史上最易懂的大数据 OTO http://network.51cto.com/art/201503/467068.htm 终于有人把O2O.C2C.B2B.B2C的区别讲透了 http://tech. ...

  7. 【转】Startssl SSL 证书申请图解

    一.什么是 SSL 证书,什么是 HTTPS 网站? SSL证书是数字证书的一种,类似于驾驶证.护照和营业执照的电子副本.SSL证书通过在客户端浏览器和Web服务器之间建立一条SSL安全通道(Secu ...

  8. python得到一个10位随机数的方法及拓展

    https://blog.csdn.net/qq_33324608/article/details/78866760 无意中看到一个写10位随机数的方法,很有想法,然后就从学了一下随机数,相关东西都记 ...

  9. MQ中间件对比

  10. 事件对象event之e.targtet || e.srcElement

    p.onclick = function (event) { var e = event || window.event, target = e.target ? e.target : e.srcEl ...