Spring Boot Sercurity---用户登录时认证的三种方法

1.第一种,直接在yml文件中进行设置

2.第二种,直接在内存中匹配用户名和密码

首先我们要创建一个Sercurity的配置类,在配置类中实现config方法方法,

package com.example.sercurity.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration
public class SercurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //采用 BCryptPasswordEncoder方法对密码进行加密
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder ( );
        String encode = encoder.encode ( "123456" );
        //inMemoryAuthentication方法是对用户登录进行请求认证
        auth.inMemoryAuthentication ().withUser ( "admin" ).password ( encode ).roles ( "vip1" );
        //super.configure ( auth );
    }
}

此时运行,当你输入账号密码时后报错,因为缺少一个PasswordEncoder对象,错误如下:

 此时我们需要创建一个能返回PasswordEncoder对象的Bean类

@Bean
    PasswordEncoder password(){
        return  new BCryptPasswordEncoder();
    }

3.第三种,通过已定义的配置类连接数据库进行认证

具体请看此博客

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Amo@骄纵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值