在以前的文章中,我们深入探讨了Spring安全性。 我们实现了由jdbc支持的安全性,基于自定义 jdbc查询的安全性以及从nosql数据库检索安全性的信息。

通过足够小心,我们会发现密码为纯文本格式。 尽管这在实际环境中可以很好地用于示例目的,但密码始终会进行编码并以编码方式存储在数据库中。

Spring Security以一种非常方便的方式支持密码编码。 它带有自己的预配置密码编码器,但是它也使我们能够创建自定义密码编码器。

SpringPassword安全附带有一些密码编码器,例如StandardPasswordEncoder,Md5PasswordEncoder和流行的BCryptPasswordEncoder。

package com.gkatzioura.spring.security;import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.StandardPasswordEncoder;/*** Created by gkatzioura on 10/5/16.*/
public class EncoderTest {private static final Logger LOGGER = LoggerFactory.getLogger(EncoderTest.class);@Testpublic void md5Encoder() {Md5PasswordEncoder md5PasswordEncoder = new Md5PasswordEncoder();String encoded = md5PasswordEncoder.encodePassword("test_pass",null);LOGGER.info("Md5 encoded "+encoded);}@Testpublic void bcryptEncoder() {BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();String encoded = bCryptPasswordEncoder.encode("test_pass");LOGGER.info("Becrypt encoded "+encoded);}@Testpublic void standardEncoder() {StandardPasswordEncoder standardPasswordEncoder = new StandardPasswordEncoder();String encoded = standardPasswordEncoder.encode("test_pass");LOGGER.info("Standard encoded "+encoded);}}

要添加密码编码,我们要做的就是在spring配置中设置密码编码器。

使用jdbc支持的spring安全配置,这非常简单,我们只需设置我们选择的密码编码器即可。 在本例中,我们将使用md5密码编码器。

package com.gkatzioura.spring.security.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;import javax.sql.DataSource;/*** Created by gkatzioura on 10/5/16.*/
@EnableWebSecurity
@Profile("encodedjdbcpassword")
public class PasswordEncodedSecurityConfig extends WebSecurityConfigurerAdapter {@Autowiredprivate DataSource dataSource;@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(new Md5PasswordEncoder()).usersByUsernameQuery("SELECT username,password,1 FROM Custom_Users_Encoded_pass where username=?").authoritiesByUsernameQuery("SELECT username,authority FROM Custom_Roles where username=?");}@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/public").permitAll().anyRequest().authenticated().and().formLogin().permitAll().and().logout().permitAll();}}

然后,我们将使用编码后的密码将用户添加到数据库。

drop table if exists Custom_Users_Encoded_pass;
create table Custom_Users_Encoded_pass(id bigint auto_increment, username varchar(255), password varchar(255));
-- real password is test_pass
insert into Custom_Users_Encoded_pass(username,password) values('TestUser','4ac1b63dca561d274c6055ebf3ed97db');

因此,通过尝试访问http:// localhost:8080 / secured,必须在登录提示中提供用户名TestUser和密码test_pass。

最后但并非最不重要的一点是,我们将必须更改gradle.build,以将encodejdbcpassword设置为默认配置文件。

bootRun {systemProperty "spring.profiles.active", "encodedjdbcpassword"
}

您可以在github上找到源代码。

翻译自: https://www.javacodegeeks.com/2016/10/spring-security-password-encoding.html

Spring安全性和密码编码相关推荐

  1. spring 安全编码_Spring安全性和密码编码

    spring 安全编码 在以前的文章中,我们深入探讨了Spring安全性. 我们实现了由jdbc支持的安全性,基于自定义 jdbc查询的安全性以及从nosql数据库检索安全性的信息. 通过足够小心,我 ...

  2. Spring Security和自定义密码编码

    在上一篇文章中,我们使用jdbc和md5密码编码将密码编码添加到了我们的spring安全配置中. 但是,在定制UserDetailsS​​ervices的情况下,我们需要对安全配置进行一些调整. 我们 ...

  3. Spring Security 之密码存储

    相关文章: OAuth2的定义和运行流程 Spring Security OAuth实现Gitee快捷登录 Spring Security OAuth实现GitHub快捷登录 Spring Secur ...

  4. Spring Security:密码编码器PasswordEncoder介绍与Debug分析

    博主在之前已经介绍了Spring Security的用户UserDetails与用户服务UserDetailsService,本篇博客介绍Spring Security的密码编码器PasswordEn ...

  5. spring安全性_具有PreAuthorize的Spring方法安全性

    spring安全性 朋友不允许朋友写用户身份验证. 厌倦了管理自己的用户? 立即尝试Okta的API和Java SDK. 数分钟之内即可在任何应用程序中对用户进行身份验证,管理和保护. 本教程将探讨使 ...

  6. Spring Boot 配置文件密码加密方法

    参考:https://github.com/ulisesbocchio/jasypt-spring-boot Spring Boot 配置文件密码加密两种方案 - ken007 - 博客园 1.引入j ...

  7. 04_《计算机安全原理与实践》密码编码工具(下)

    04_<计算机安全原理与实践>密码编码工具(下)含本章理论性习题解答 前言 本专栏上一篇博文介绍了消息认证的机理和非对称加密体系,从实际应用角度,二者是不可分割的.本章将在上一章的基础上进 ...

  8. Spring Security BCryptPasswordEncoder 密码加盐

    Spring Security BCryptPasswordEncoder 密码加盐 引入spring-boot-starter-security 的Jar包 <dependency>&l ...

  9. Spring HtmlUtils把HTML编码转义,可将HTML标签互相转义

    Spring HtmlUtils把HTML编码转义,可将HTML标签互相转义 org.springframework.web.util.HtmlUtils 可以实现HTML标签及转义字符之间的转换.  ...

最新文章

  1. 在云中利用开源软件进行开发以提高创新能力
  2. win2008 mysql优化_SQL server 2008 数据库优化常用脚本
  3. HTML和CSS基础知识
  4. C++统计10亿以内所有的质素(素数)的实现算法(附完整源码)
  5. 国产操作系统要起来,这款Linux是你的菜吗?
  6. configparser logging
  7. RUNOOB python 67 数组的元素互换
  8. 阿里云对象存储OSS与文件存储NAS的区别
  9. python笔记38-使用zmail发各种邮件案例代码
  10. 计算机专业答辩网站怎么保证安全性,答辩(计算机专业答辩题目选集)
  11. GRE 9月15日机经
  12. vue 生成二维码(中间logo),下载二维码,复制链接(vue + vue-qr+clipboard)
  13. 如何用SPSS或Excel做中介效应的Sobe检验?
  14. 如何写好一篇技术文章?
  15. Golang底层原理剖析之上下文Context
  16. 开关电源入门01-开关电源的基本形式
  17. proteus中 基于STC89C51的ADC0809模数转换仿真
  18. 计算机桌面右键新建展不开,电脑右键菜单没有新建记事本怎么办?
  19. 【DXR/RayTracingGems】屏幕空间光子映射Screen-Space PM(SSPM)
  20. cordova-plugin-vibration 设备震动整理

热门文章

  1. Spring Boot 2.0 正式发布
  2. winform实现简单的计算器V1版本
  3. servlet session 跟踪用户上次访问时间
  4. maven(3)maven3.3.9使用入门
  5. 从零开始学spring-boot(2)-集成spring-data-jpa
  6. junit junit_穿越JUnit流
  7. 世界是沙粒还是宇宙_看到一个沙粒世界:再一次你好世界
  8. java crud_Java 8流中的数据库CRUD操作
  9. cuba 平台_CUBA平台的理念
  10. maven junit测试_使用Maven Failsafe和JUnit @Category将集成测试与单元测试分开