我是java spring security的新手,并且遵循Spring.io tutorial guide.
作为其中的一部分,我根据需要编辑了WebSecurityConfig类:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/","/home").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Bean
@Override
public UserDetailsService userDetailsService() {
UserDetails user =
User.withDefaultPasswordEncoder()
.username("user")
.password("password")
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}
}
在userDetailService()方法中,它使用withDefaultPasswordEncoder(),现在已弃用,如文档中所示:withDefaultPasswordEncoder()
不幸的是,我没有找到替代方案,在不使用弃用方法的情况下完成本教程.
如果可能,有人能为此提供替代方案吗?
谢谢!
User.withDefaultPasswordEncoder()仍然可以用于演示,你不必担心这是你正在做什么 – 即使它已被弃用 – 但在生产中,你的源代码中不应该有纯文本密码.
您应该做什么而不是使用当前的userDetailsService()方法是following:
private static final String ENCODED_PASSWORD = "$2a$10$AIUufK8g6EFhBcumRRV2L.AQNz3Bjp7oDQVFiO5JJMBFZQ6x2/R/2";
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.passwordEncoder(passwordEncoder())
.withUser("user").password(ENCODED_PASSWORD).roles("USER");
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
其中ENCODED_PASSWORD是用BCrypt编码的secret123.你也可以像下面这样编程编码:passwordEncoder().encode(“secret123”).
这样,即使您将代码推送到公共存储库,人们也不会知道密码,因为ENCODED_PASSWORD只显示密码的编码版本而不是纯文本版本,但是因为您知道$2a $10 $AIUufK8g6EFhBcumRRV2L.AQNz3Bjp7oDQVFiO5JJMBFZQ6x2 / R / 2实际上是字符串secret123的编码密码,而其他人没有,您的内存用户凭证用户:secret123将不会受到损害.
请注意,为了示例,我将其保留在静态变量中.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。