源代码链接
安装httpie 需要 python 环境
pip install --upgrade httpie
进入D:Project目录,在此目录下打开CMD,调用httpie,创建 oauth2 项目
http -d https://start.spring.io/starter.zip javaVersion==17 groupId==com.my.demo artifactId==oauthService name==oauth-service baseDir==oauth-service bootVersion==2.6.6.RELEASE dependencies==cloud-starter
![](https://e16s.com/wp-content/uploads/2022/07/frc-6cdfd34c477d3d11e6d9d1501d7ab417.png)
image.png
将生成的oauthService.zip解压缩到当前目录,然后进入到oauth-service文件夹
![](https://e16s.com/wp-content/uploads/2022/07/frc-cacb319079a47bdf74dd5eb986dc4fa6.png)
image.png
导入数据库脚本 脚本地址
![](https://e16s.com/wp-content/uploads/2022/07/frc-debe33d410541d27a89522e026d59a8d.png)
image.png
可见 user_1的 isactive 为 0,用户的密码都是 {noop}123456 的明文方式
sts中导入项目
![](https://e16s.com/wp-content/uploads/2022/07/frc-652a26b6b71a531dc1896da96d73fbb6.png)
image.png
![](https://e16s.com/wp-content/uploads/2022/07/frc-49877b94d276a75bcf7f5f32bcb92a32.png)
image.png
修改 POM文件,添加引用 javax.xml.bind等等一堆库 是为解决springSecurityFilterChain的编译错误
POM文件
修改 application.properties 配置文件
server.port=8509
spring.application.name=oauth-service
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.timeout=2000
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/oauth2?serverTimezone=GMT%2B8&useSSL=false
spring.datasource.username=root
spring.datasource.password=
修改主类文件
![](https://e16s.com/wp-content/uploads/2022/07/frc-a2ac94d23962ecc17479e8af2cdba2c7.png)
image.png
主类文件夹下建立 config, service 两个文件夹
![](https://e16s.com/wp-content/uploads/2022/07/frc-be2cd631256ce37546baf3c201fcdcdf.png)
image.png
config下添加两个配置文件(WebSecurityConfig.java,AuthorizationServerConfig.java),service目录下添加Redis缓存Token实现(RedisTokenStoreService)
![](https://e16s.com/wp-content/uploads/2022/07/frc-e30736febf910b636f500d0e5381e7dd.png)
image.png
如果想用bcrypt编码 则所有的数据库端的密码都保存成
{bcrypt}$2a$10$l4Su6LU.w.HIgpHXn31Hc.1VKbkv7.EY.P7VDzJxyImrZEMDW3Hkq
同时修改AuthorizationServerConfig.java 文件
@Autowired
private PasswordEncoder passwordEncoder;
@Bean
public ClientDetailsService clientDetails() {
JdbcClientDetailsService jdbcClientDetailsService = new JdbcClientDetailsService(dataSource);
jdbcClientDetailsService.setPasswordEncoder(passwordEncoder);
return jdbcClientDetailsService;
}
编译,运行
![](https://e16s.com/wp-content/uploads/2022/07/frc-a1d690abef5823c7c56ec0d1f9664f48.png)
image.png
![](https://e16s.com/wp-content/uploads/2022/07/frc-5db10c97724fb5c21c3df5af41bf1a4e.png)
image.png
测试
可以看到 user_1 是被禁用的
![](https://e16s.com/wp-content/uploads/2022/07/frc-52c47dc29cbf977104a6de7d1f2b568d.png)
image.png
换成 Test用户,则可以获取到Token
![](https://e16s.com/wp-content/uploads/2022/07/frc-6e7e1f2c756dc92c7fc0f4db920fc73f.png)
image.png
查看Redis缓存
![](https://e16s.com/wp-content/uploads/2022/07/frc-b65c0f67363baa9401083afae9fc770a.png)
image.png
文章评论