获取内容资料
Java编程

千锋扣丁学堂java

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

2.SpringSecurity4

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

千锋扣丁学堂java

@Configurationpublic 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/下所有内容的公共访问。

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

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

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

3.SpringSecurity5

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

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

@Configurationpublic 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.迁移现有密码

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

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

String encoded = new BCryptPasswordEncoder.encode(plainTextPassword);

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

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

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

在这个快速示例中,我们使用新的密码存储机制将有效的Spring4内存中认证配置更新到Spring5。与往常一样,您可以在GitHub项目中找到源代码。

以上就是关于千锋扣丁学堂Java培训之Spring Security 五种默认密码编码器,希望对大家有所帮助,想要了解更多关于Java开发方面内容的小伙伴,请关注扣丁学堂Java培训官网、等平台,扣丁学堂IT职业在线学习教育有专业的Java讲师为您指导,此外扣丁学堂老师精心推出的Java视频教程定能让你快速掌握Java从入门到精通开发实战技能。扣丁学堂Java技术交流群:850353792。

Similar Posts

发表评论

邮箱地址不会被公开。 必填项已用*标注