栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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.第三种,通过已定义的配置类连接数据库进行认证

具体请看此博客

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/778317.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号