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 ...
随机推荐
- qt获取屏幕
m_pDesktopWidget = QApplication::desktop(); // 屏体数量,包含扩展屏 int screenCount = m_pDesktopWidget->scr ...
- JavaScript--函数中this的几种指向
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 读JS高性能总结——DOM编程(一)
DOM是一个与语言无关的API,它在浏览器中的借口却是用JS来实现的. 浏览器通常会把DOM和JS独立实现. 在IE中,JS的实现名是JScript,位于jscript.dll文件中,DOM实现则是m ...
- Java版阿里云通信短信发送API接口实例(新)
阿里云通信(原名阿里大于)的短信服务(Short Message Service)是阿里云为用户提供的一种通信服务的能力,支持快速发送短信验证码.短信通知等. 完美支撑双11期间2亿用户,发送6亿短信 ...
- D3D10/11中的遮挡查询的使用
原文:D3D10/11中的遮挡查询的使用 在D3D10/11中,有D3D10_QUERY/D3D11_QUERY接口,通过QUERY接口,我们可以查询GPU的一些状态,比如GPU的时间戳信 ...
- 重温Observer模式--热水器·改
引言 在 C#中的委托和事件 一文的后半部分,讲述了Observer(观察者)模式,并使用委托和事件实现了这个模式.实际上,不使用委托和事件,一样可以实现Observer模式.在本文中,我将使用GOF ...
- iOS 9 学习系列:UIStack View
http://www.cocoachina.com/ios/20150921/13492.html 在 iOS9 中,Apple 引入了 UIStackView,他让你的应用可以通过简单的方式,纵向或 ...
- 四.使用JDBC进行批处理操作
1 create table testbatch 2 ( 3 id int primary key, 4 name varchar(20) 5 ); 在实际的项目开发中,有时候需要向数据库发送一批SQ ...
- 【JZOJ4848】【GDOI2017模拟11.3】永恒的契约
题目描述 宅邸迅速的燃烧着,必须带贝蒂走出禁书库!凭着感觉,又一次直接找到禁书库的门. "你,是那个人嘛?"400年了,当初圣域建立结界时没有进入圣域,被伤了心的人工精灵贝蒂,与强 ...
- python开发资源链接
1.docker docker Windows版下载:https://oomake.com/download/docker-windows docker 英文官网:https://www.docker ...