security第四集 用户认证体系之内存
·
在上篇《Spring Cloud+vue3 整合 Comfyui 实战:从零搭建AI人物写真工厂,实现光影复刻与发型穿搭定制》中,我们简单的介绍了一下这个项目。
如果你想先在线体验这个写真工厂的「图生图」实际效果,欢迎点击文末【官方网站】链接进行预览。支持无限制卸甲
从本篇开始,我将对写真工厂项目进行技术点深度解剖,逐一拆解每个核心微服务的实现细节。本文先从「认证中心微服务」入手,重点讲清楚Spring Security在项目中的标准用法与最佳实践。
- 改变security默认用户名密码的内存方式,就是继承WebSecurityConfigurerAdapter 适配器,并重写configure(AuthenticationManagerBuilder builder)方法
- 新建类 MyWebSecurityConfigAdapter 并继承 WebSecurityConfigurerAdapter
package com.kai.oauth.securityservice.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;
@Configuration
public class MyWebSecurityConfigAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//inMemoryAuthentication 内存方式存储
auth.inMemoryAuthentication()
//设置用户名密码 ,{noop}表示密码不加密
.withUser("zhao").password("{noop}123456")
//设置角色 "USER"是security默认提供的一个角色
.roles("USER")
//多个用户创建
.and()
.withUser("qian").password("{noop}123456")
.roles("USER");
}
}
- 重启项目,浏览器再次访问 http://localhost:8080/hello,用设置的用户,登录,如果成功输出Hello World!,说明基于内存的用户配置成功
- 注:基于内存的配置成功后,yml配置会失效
更多推荐

所有评论(0)