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

 

原文地址:https://www.cnblogs.com/hsz-csy/p/9675641.html