Spring-Security密码加密问题

最近在了解spring security的时候发现了一个问题,就是我自己配置了一个加密过后,我自己登录的时候还会出现密码不正确的问题,但是我直接在我虚拟化的用户上直接加密却又可以登录

@Configuration
//WebSecurityConfigurerAdapter自定义的security策略
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
  //链式编程
  @Override
  //授权
  protected void configure(HttpSecurity http) throws Exception {
    //super.configure(http);
    //请求授权规则
    http.authorizeRequests()
      .antMatchers("/success").permitAll()
      .antMatchers("/vip1").hasRole("vip1");


    //没有权限自动跳转到登陆页(自带登录页)
    http.formLogin();

  }

  @Override
  //认证权限AuthenticationManagerBuilder 自定义的认证策略
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    //super.configure(auth);
    //    //inMemoryAuthentication配置一个虚拟化用户名密码
    auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
      //withUser用户名,password密码,roles角色
      .withUser("jiang").password(new BCryptPasswordEncoder().encode("123")).roles("vip1");

  }

这样的话就不会出现密码错误,但是

@Configuration
//WebSecurityConfigurerAdapter自定义的security策略
public class SecurityConfig extends WebSecurityConfigurerAdapter {

  //链式编程
  @Override
  //授权
  protected void configure(HttpSecurity http) throws Exception {
    //super.configure(http);
    //请求授权规则
    http.authorizeRequests()
      .antMatchers("/success").permitAll()
      .antMatchers("/vip1").hasRole("vip1");


    //没有权限自动跳转到登陆页(自带登录页)
    http.formLogin();

  }

  @Override
  //认证权限AuthenticationManagerBuilder 自定义的认证策略
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    //super.configure(auth);
    //    //inMemoryAuthentication配置一个虚拟化用户名密码
    auth.inMemoryAuthentication()
      //withUser用户名,password密码,roles角色
      .withUser("jiang").password("123").roles("vip1");

  }

//  //配置密码加密 从spring5开始就强制要求密码加密
  @Bean
  PasswordEncoder passwordEncoder(){
    return  new  BCryptPasswordEncoder();
  }

直接配置一个@Bean却不行

想问问怎么回事

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值