withDefaultPasswordEncoder() 過時棄用問題


  在學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"); }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM