springboot2.x+security对明文密码加密

1---添加依赖

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

2---在与启动类同包下创建配置类

@Configuration
public class securityUtils extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/**").permitAll()//放行所有权限
        .and().csrf().disable();//相当于<security:csrf enabled=false/>

    }

}

3---service层注入并加入:

   @Autowired
    private BCryptPasswordEncoder encoder;



   public int insertUser(User user){
        user.setPassword(encoder.encode(user.getPassword()));//加入
        return userRepository.insertUser(user);
    }

4---启动类

   @Bean
    public BCryptPasswordEncoder encoder(){
        return  new BCryptPasswordEncoder();
    }
不经一番彻骨寒,哪有梅花扑鼻香?
原文地址:https://www.cnblogs.com/zongyao/p/13831145.html