网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

SpringSecurity5中默认密码编码器的示例分析

这篇文章将为大家详细讲解有关Spring Security5中默认密码编码器的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

顺德网站建设公司创新互联建站,顺德网站设计制作,有大型网站制作公司丰富经验。已为顺德数千家提供企业网站建设服务。企业网站搭建\外贸网站制作要多少钱,请找那个售后服务好的顺德做网站的公司定做!

1.概述

在Spring Security 4中,可以使用内存中身份验证以纯文本格式存储密码。

对版本5中的密码管理过程进行了重大改进,为密码编码和解码引入了更安全的默认机制。这意味着如果您的Spring应用程序以纯文本格式存储密码,升级到Spring Security 5可能会导致问题。

在这个简短的教程中,我们将描述其中一个潜在的问题,并展示该问题的解决方案。

2. Spring Security 4

我们首先展示一个标准的安全配置,它提供简单的内存中身份验证(适用于Spring 4):

@Configuration
public class InMemoryAuthWebSecurityConfigurer 
 extends WebSecurityConfigurerAdapter {
 
 @Override
 protected void configure(AuthenticationManagerBuilder auth) 
 throws Exception {
 auth.inMemoryAuthentication()
  .withUser("spring")
  .password("secret")
  .roles("USER");
 }
 
 @Override
 protected void configure(HttpSecurity http) throws Exception {
 http.authorizeRequests()
  .antMatchers("/private/**")
  .authenticated()
  .antMatchers("/public/**")
  .permitAll()
  .and()
  .httpBasic();
 }
}

此配置定义所有/私有/映射方法的身份验证以及/ public /下所有内容的公共访问。

如果我们在Spring Security 5下使用相同的配置,我们会收到以下错误:

java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"

该错误告诉我们由于没有为我们的内存中身份验证配置密码编码器,因此无法解码给定的密码。

3. Spring Security 5

我们可以通过使用PasswordEncoderFactories类定义DelegatingPasswordEncoder来解决此错误。

我们使用此编码器通过AuthenticationManagerBuilder配置我们的用户:

@Configuration
public class InMemoryAuthWebSecurityConfigurer 
 extends WebSecurityConfigurerAdapter {
 
 @Override
 protected void configure(AuthenticationManagerBuilder auth) 
 throws Exception {
 PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
 auth.inMemoryAuthentication()
  .withUser("spring")
  .password(encoder.encode("secret"))
  .roles("USER");
 }
}

现在,通过这种配置,我们使用BCrypt以以下格式存储我们的内存中密码:

{bcrypt}$2a$10$MF7hYnWLeLT66gNccBgxaONZHbrSMjlUofkp50sSpBw2PJjUqU.zS

虽然我们可以定义自己的一组密码编码器,但建议坚持使用PasswordEncoderFactories中提供的默认编码器。

3.1.迁移现有密码

我们可以通过以下方式将现有密码更新为推荐的Spring Security 5标准:

更新纯文本存储密码及其编码值:

String encoded = new BCryptPasswordEncoder().encode(plainTextPassword);

前缀散列存储的密码及其已知的编码器标识符:

{bcrypt}$2a$10$MF7hYnWLeLT66gNccBgxaONZHbrSMjlUofkp50sSpBw2PJjUqU.zS
{sha256}97cde38028ad898ebc02e690819fa220e88c62e0699403e94fff291cfffaf8410849f27605abcbc0

当存储密码的编码机制未知时,请求用户更新其密码

关于“Spring Security5中默认密码编码器的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


网页题目:SpringSecurity5中默认密码编码器的示例分析
链接地址:http://bjjierui.cn/article/jooisj.html

其他资讯