Spring Security 5.x兼容多种密码加密方式
1 spring security PasswordEncoder
spring security 5不需要配置密码的加密方式,而是用户密码加前缀的方式表明加密方式,如:
{MD5}88e2d8cd1e92fd5544c8621508cd706b代表使用的是MD5加密方式;{bcrypt}$2a$10$eZeGvVV2ZXr/vgiVFzqzS.JLV878ApBgRT9maPK1Wrg0ovsf4YuI6代表使用的是bcrypt加密方式。
spring security官方推荐使用更加安全的bcrypt加密方式。
这样可以在同一系统中支持多种加密方式,迁移用户比较省事。spring security 5支持的加密方式在PasswordEncoderFactories中定义:
-
public class PasswordEncoderFactories {
-
public static PasswordEncoder createDelegatingPasswordEncoder() {
-
String encodingId = "bcrypt";
-
Map<String, PasswordEncoder> encoders = new HashMap();
-
encoders.put(encodingId, new BCryptPasswordEncoder());
-
encoders.put("ldap", new LdapShaPasswordEncoder());
-
encoders.put("MD4", new Md4PasswordEncoder());
-
encoders.put("MD5", new MessageDigestPasswordEncoder("MD5"));
-
encoders.put("noop", NoOpPasswordEncoder.getInstance());
-
encoders.put("pbkdf2", new Pbkdf2PasswordEncoder());
-
encoders.put("scrypt", new SCryptPasswordEncoder());
-
encoders.put("SHA-1", new MessageDigestPasswordEncoder("SHA-1"));
-
encoders.put("SHA-256", new MessageDigestPasswordEncoder("SHA-256"));
-
encoders.put("sha256", new StandardPasswordEncoder());
-
return new DelegatingPasswordEncoder(encodingId, encoders);
-
}
-
-
private PasswordEncoderFactories() {
-
}
-
}
2 测试
2.1 pom.xml
-
<?xml version="1.0" encoding="UTF-8"?>
-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-
<modelVersion>4.0.0</modelVersion>
-
-
<groupId>com.hfcsbc</groupId>
-
<artifactId>security</artifactId>
-
<version>0.0.1-SNAPSHOT</version>
-
<packaging>jar</packaging>
-
-
<name>security</name>
-
<description>Demo project for Spring Boot</description>
-
-
<parent>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-parent</artifactId>
-
<version>2.0.0.M7</version>
-
<relativePath/> <!-- lookup parent from repository -->
-
</parent>
-
-
<properties>
-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
-
<java.version>1.8</java.version>
-
</properties>
-
-
<dependencies>
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-security</artifactId>
-
</dependency>
-
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-test</artifactId>
-
<scope>test</scope>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.security</groupId>
-
<artifactId>spring-security-test</artifactId>
-
<scope>test</scope>
-
</dependency>
-
<dependency>
-
<groupId>org.projectlombok</groupId>
-
<artifactId>lombok</artifactId>
-
</dependency>
-
</dependencies>
-
-
<build>
-
<plugins>
-
<plugin>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-maven-plugin</artifactId>
-
</plugin>
-
</plugins>
-
</build>
-
-
<repositories>
-
<repository>
-
<id>spring-snapshots</id>
-
<name>Spring Snapshots</name>
-
<url>https://repo.spring.io/snapshot</url>
-
<snapshots>
-
<enabled>true</enabled>
-
</snapshots>
-
</repository>
-
<repository>
-
<id>spring-milestones</id>
-
<name>Spring Milestones</name>
-
<url>https://repo.spring.io/milestone</url>
-
<snapshots>
-
<enabled>false</enabled>
-
</snapshots>
-
</repository>
-
</repositories>
-
-
<pluginRepositories>
-
<pluginRepository>
-
<id>spring-snapshots</id>
-
<name>Spring Snapshots</name>
-
<url>https://repo.spring.io/snapshot</url>
-
<snapshots>
-
<enabled>true</enabled>
-
</snapshots>
-
</pluginRepository>
-
<pluginRepository>
-
<id>spring-milestones</id>
-
<name>Spring Milestones</name>
-
<url>https://repo.spring.io/milestone</url>
-
<snapshots>
-
<enabled>false</enabled>
-
</snapshots>
-
</pluginRepository>
-
</pluginRepositories>
-
</project>
-
2.2 测试
spring security 5.x默认使用bcrypt加密
-
@Slf4j
-
public class DomainUserDetailsService {
-
-
public static void main(String[] args){
-
PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
-
String encode = passwordEncoder.encode("password");
-
log.info("加密后的密码:" + encode);
-
log.info("bcrypt密码对比:" + passwordEncoder.matches("password", encode));
-
-
String md5Password = "{MD5}88e2d8cd1e92fd5544c8621508cd706b";//MD5加密前的密码为:password
-
log.info("MD5密码对比:" + passwordEncoder.matches("password", encode));
-
}
-
-
}
原文地址:https://blog.csdn.net/wiselyman/article/details/84915939
Spring Security 5.x兼容多种密码加密方式的更多相关文章
- laravel更改默认的登录密码加密方式
laravel更改默认的登录密码加密方式 laravel 默认用的登录密码加密方式是: $password = Hash::make('password'); 而我平时用的密码加密方式是: $pa ...
- 关于mysql8.0及以上版本连接navicat时候报错(密码加密方式需要修改)
首先这个原因是因为MySQL版本的密码加密方式变了,要把它修改成以前的方式(因为,navicat不支持这种方式) 1:先进入mysql: mysql -uroot -p123456; 2:查询密码加密 ...
- Spring Security笔记:使用BCrypt算法加密存储登录密码
在前一节使用数据库进行用户认证(form login using database)里,我们学习了如何把“登录帐号.密码”存储在db中,但是密码都是明文存储的,显然不太讲究.这一节将学习如何使用spr ...
- Spring Security中的MD5盐值加密
在 spring Security 文档中有这么一句话: "盐值的原理非常简单,就是先把密码和盐值指定的内容合并在一起,再使用md5对合并后的内容进行演算,这样一来,就算密码是一个很常见的字 ...
- Spring Security 5中的默认密码编码器
1.概述 在Spring Security 4中,可以使用内存中身份验证以纯文本格式存储密码. 对版本5中的密码管理过程进行了重大改进,为密码编码和解码引入了更安全的默认机制.这意味着如果您的Spri ...
- Ecstore会员密码加密方式破解
<?php //以下是加密方式,亲测有效 $string_md5 = md5(md5("密码")."用户名"."注册时间");//三个 ...
- Spring Boot 2.0 利用 Spring Security 实现简单的OAuth2.0认证方式1
0. 前言 之前帐号认证用过自己写的进行匹配,现在要学会使用标准了.准备了解和使用这个OAuth2.0协议. 1. 配置 1.1 配置pom.xml 有些可能会用不到,我把我项目中用到的所有包都贴出来 ...
- Django 自带密码加密,自定密码加密方式 及自定义验证方式
在django1.6中,默认的加密方式是pbkdf_sha256,具体算法不表,一直以来用django的自带用户验证都十分顺手,今天有需求,需要修改默认加密方式为md5,具体方法为: 在setting ...
- spring security结合数据库验证用户-XML配置方式
之前的用户信息我们都是使用的内存用户,测试例子可以,实际中使用肯定不行,需要结合数据库进行验证用户.这就是本节的重点: 项目目录如下: 在之前的项目中的依赖中添加两个依赖: <dependen ...
随机推荐
- 廖雪峰Python总结4
面向对象编程 将计算机程序视为一系列的命令集合.包含: 数据 操作数据的函数 Python中,所有的数据类型都可以视为对象. 面向对象特点:封装,继承,多态. 类的函数和普通函数:类的第一个参数永远是 ...
- JavaScript--关于实例对象带不带参数和构造函数带不带参数的关系
就是一句话: 构造函数创建对象时,也可以带参数,因为可以对对象进行一些属性的初始化,也就是你创建对象时,就带着这些属性,当然你也可以不带参数,后面实例化对象后再进行添加.而且,js函数的参数在定义函数 ...
- sqlserver 带返回值的存储过程
create proc test ) output as begin select @result = 'haha' ; end go ), @count int exec @count = test ...
- poj3422 最小费用流
一遍的话秩序要dp就好,但是这里要删去点.此题可以转化为最小费用流.开始我想了半天纠结怎么处理到过一次后值变0,看了书之后发现拆点解决了这个问题. 对于点t,拆为t-->t',容量为1,费用为负 ...
- oracle-ORA-01650错误
Unable to extend rollback segment 原因:没有足够的撤销空间用来处理所有活动事务
- [idea]idea配置tomcat 标签: tomcatidea 2017-03-12 22:12 402人阅读 评论(19)
我们在使用idea的时候,一定会遇到的一步,就是使用tomcat来发布我们的项目,那么,如何在idea中设置tomcat呢?下面就随小编来一起学习一下吧. 设置tomcat 打开设置界面 Run-&g ...
- PHPCMS快速建站系列之邮箱验证
1. 登录163邮箱,->设置,开启POP3服务->把SMTP服务器地址复制到PHPCMS后台. 2.开启客户端授权密码 3.填写相关信息,.可以在测试邮箱填入邮箱地址测试
- Python2 生成器 简介
1. A generator: provide a kind of function that can return an intermediate result ("the next va ...
- Laravel 登录后清空COOKIE 方法
需求 在Laravel 登陆立即清空保存的COOKIE数组 实现 # Http/Controllers/Auth/LoginController.php public function redirec ...
- Length of Last Word输出最后单词的字母个数
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...