Spring Security 入门 基于内存存储认证信息

基于内存存储认证信息

@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * authentication [ɔːˌθentɪˈkeɪʃn] n.    身份验证; 认证;鉴定;
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //在内存中创建一个用户,设置密码和权限
        auth.inMemoryAuthentication()
                .withUser("devin")
                .password("123")
                .authorities("ADMIN");
    }

}

访问:http://localhost:8080/hello 重定向到登录页面
在这里插入图片描述

控制台抛出如下异常

java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"

问题原因:密码要存储加密后的。

@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder passwordEncoder() {
        // 设置默认的加密方式
        return new BCryptPasswordEncoder();
    }

    /**
     * authentication [ɔːˌθentɪˈkeɪʃn] n.    身份验证; 认证;鉴定;
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //对密码加密
        final String password = passwordEncoder().encode("123");
        //在内存中创建一个用户,设置密码和权限(创建了具体的用户信息以后就不会再生产默认的用户和密码)
        auth.inMemoryAuthentication()
                .withUser("devin")
                .password(password)
                .authorities("ADMIN");
    }
}

访问:http://localhost:8080/hello 重定向到登录页面
在这里插入图片描述
登录成功以后,重定向回原来的请求
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值