- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我对 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()
不幸的是,我无法找到替代方法来完成本教程而不使用已弃用的方法。如果可能的话,有人可以为此提供替代方案吗?
谢谢!
注意:我附上了我的错误的几个屏幕截图,以及我的 gradle 文件
最佳答案
编辑:删除旧答案,误解了问题。这是新的:
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")
.
这样,即使您将代码推送到公共(public)存储库,人们也不会知道密码,因为 ENCODED_PASSWORD
只显示密码的编码版本而不是纯文本版本,而是因为您知道 $2a$10$AIUufK8g6EFhBcumRRV2L.AQNz3Bjp7oDQVFiO5JJMBFZQ6x2/R/2
实际上是字符串 secret123
的编码密码,而其他人则不是,您的内存用户使用凭据 user:secret123
不会被泄露。
请注意,为了示例,我将其保留在静态变量中。
关于Java Spring Security - User.withDefaultPasswordEncoder() 已弃用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49847791/
我是一名优秀的程序员,十分优秀!