在學springsecurity5.X時,在demo里,內存配置用戶的時候,提示withDefaultPasswordEncoder過時,特查看了源碼,官方給出的理由是:
/** @deprecated */ @Deprecated public static User.UserBuilder withDefaultPasswordEncoder() { logger.warn("User.withDefaultPasswordEncoder() is considered unsafe for production and is only intended for sample applications."); PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder(); User.UserBuilder var10000 = builder(); encoder.getClass(); return var10000.passwordEncoder(encoder::encode); }
@Deprecated 棄用的意思
日志里也寫得清楚棄用的原因:不安全
所以,換了個寫法,如下:
/** * 在內存中配置一個用戶,admin/admin分別是用戶名和密碼,這個用戶擁有USER角色。 * withDefaultPasswordEncoder 被遺棄,原因是不安全,只能在例子中使用 * @param auth * @throws Exception */ @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { // withDefaultPasswordEncoder被棄用,用以下方式
PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder(); auth.inMemoryAuthentication() // .withUser(User.withDefaultPasswordEncoder().username("admin")
.withUser("admin") .password(encoder.encode("admin")).roles("USER"); }