SpringSecurity---基于内存的FormLogin
2024-08-28 10:15:06
SpringSecurity已经内置了一个登陆页面,所以目前我们就采取默认的登陆页面
一. 引入依赖
这步略过不表
二. 默认实现
添加接口
@RestController
public class TestController {
@GetMapping("/test")
public String test(){
return "this is test";
}
}
访问接口
会出现如下页面
默认的用户名为user,密码为控制台打印的一串字符
当然,也可以在application.yml中配置
三. 基于内存的简单认证
继承WebSecurityConfigurerAdapter
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
}
配置密码加密方式
SpringSecurity5.x后必须指定一种加密方式
不然会报错
这里我们使用BCryptPasswordEncoder
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
配置用户名和密码和角色
//主要是重写configure方法
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user1")
.password(passwordEncoder().encode("123456"))
.roles("root")
.and()
.withUser("user2")
.password(passwordEncoder().encode("123456"))
.roles("user");
}
虽然这里角色并没有实际的用处,但不加会报错
其实这两种,我感觉可以说是基本没什么用
暂时并不会涉及到授权,这会在后面用专门的文章去讲
完整代码会放在GitHub上,欢迎star
最新文章
- HAProxy介绍
- MySQL新建用户,授权,删除用户,修改密码
- C# webbrowser实现真正意义上的F5刷新
- HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)
- MySQL乱码解决办法
- Codeforce 567D
- JavaScript 函数惰性载入
- cocos2dx混合模式应用
- TOM大师脚本01-查找未建索引的外键
- BZOJ 1058: [ZJOI2007]报表统计( 链表 + set )
- 编写一种递归方法,它返回数N的二进制中表示1的个数。
- 论文笔记:Show, Attend and Tell: Neural Image Caption Generation with Visual Attention
- 项目经验分享[转自min.jiang]
- Spring之jdbcTemplate:增删改
- 杂项:SQLite
- spring-boot PageHelper
- Simple Package Tool 学习
- 关于spring xml文件中的xmlns,xsi:schemaLocation(转)
- #C++初学记录(高精度运算)(加法)
- C、C++文件操作大全
热门文章
- 如何在 Apple Watch S6上离线播放音乐
- Xcode 切换 iOS 模拟器的 Dark 模式
- Interview Questions All In One
- SVG animation(text, background)
- API 注解 &; Java API annotation
- svg opacity &; fill-opacity &; stroke-opacity
- The State of JavaScript 2019
- VAST助推NGK公链热度升温,日活超过以太坊!
- BGV再掀DeFi投资热潮,NGK全球启动大会圆满落幕
- Java Reference核心原理分析