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

最新文章

  1. HAProxy介绍
  2. MySQL新建用户,授权,删除用户,修改密码
  3. C# webbrowser实现真正意义上的F5刷新
  4. HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)
  5. MySQL乱码解决办法
  6. Codeforce 567D
  7. JavaScript 函数惰性载入
  8. cocos2dx混合模式应用
  9. TOM大师脚本01-查找未建索引的外键
  10. BZOJ 1058: [ZJOI2007]报表统计( 链表 + set )
  11. 编写一种递归方法,它返回数N的二进制中表示1的个数。
  12. 论文笔记:Show, Attend and Tell: Neural Image Caption Generation with Visual Attention
  13. 项目经验分享[转自min.jiang]
  14. Spring之jdbcTemplate:增删改
  15. 杂项:SQLite
  16. spring-boot PageHelper
  17. Simple Package Tool 学习
  18. 关于spring xml文件中的xmlns,xsi:schemaLocation(转)
  19. #C++初学记录(高精度运算)(加法)
  20. C、C++文件操作大全

热门文章

  1. 如何在 Apple Watch S6上离线播放音乐
  2. Xcode 切换 iOS 模拟器的 Dark 模式
  3. Interview Questions All In One
  4. SVG animation(text, background)
  5. API 注解 & Java API annotation
  6. svg opacity & fill-opacity & stroke-opacity
  7. The State of JavaScript 2019
  8. VAST助推NGK公链热度升温,日活超过以太坊!
  9. BGV再掀DeFi投资热潮,NGK全球启动大会圆满落幕
  10. Java Reference核心原理分析