Spring Boot: How to specify the PasswordEncoder?

前端 未结 15 1152
醉梦人生
醉梦人生 2020-12-02 06:27

Currently I got the main class:

package com.recweb.springboot;

import org.springframework.boot.SpringApplication;
im         


        
15条回答
  •  爱一瞬间的悲伤
    2020-12-02 06:59

    You need to have some sort of a password encoder, but

    withDefaultPasswordEncoder()
    

    is deprecated and no more suitable for production. Use this instead:

    PasswordEncoder encoder =
         PasswordEncoderFactories.createDelegatingPasswordEncoder();
    
    ...
    
    UserDetails user = User.withUsername("someusername")
                           .password(encoder.encode("somepassword"))
                           .roles("USER").build();
    

    Ref: https://docs.spring.io/spring-security/site/docs/5.0.2.BUILD-SNAPSHOT/api/index.html?org/springframework/security/core/userdetails/User.html

提交回复
热议问题