# Spring-security-oauth2
**Repository Path**: odiyq/Spring-security-oauth2
## Basic Information
- **Project Name**: Spring-security-oauth2
- **Description**: Spring security整合oauth2实现token认证
- **Primary Language**: Java
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 21
- **Created**: 2021-08-18
- **Last Updated**: 2021-08-18
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# Spring-security整合OAuth2授权认证协议
1. 使用@EnableAuthorizationServer配置授权服务器,开启授权认证服务的端点/oauth/authorize、/oauth/token
2. 使用@EnableResourceServer配置资源服务器,配置需要传token才能调用的接口路径
3. 使用@EnableGlobalMethodSecurity配置spring security放行所有认证服务端点/oauth/**
4. token写入redis缓存,过期会自动失效.运行前请先开启redis并导入SQL脚本
5. 由于使用了password认证,前端发请求时需要先申请token,代码在auth.js中:
```
function fetchToken(){
var name = token_storage.getItem('username');
var pwd = token_storage.getItem('password');
var data={
'scope':'server',
'grant_type':'password',
'username':"java",
"password":"1234"
};
$.ajax({
url: "/oauth/token",
type:"post",
data:data,
async: false,
contentType: 'application/x-www-form-urlencoded',
beforeSend:function(xhr){
xhr.setRequestHeader("Authorization", 'Basic ' + BASE64.encode(client_id+':'+client_secret));
},
success: function (sResponse) {
saveAuth(sResponse);
console.log('fetch_token ok: ' + sResponse.access_token+' expires_in:'+sResponse.expires_in);
},
error:function(a,b,c){
console.log(a, b, c);
}
});
}
```
如果要刷新token,则:
```
function refreshToken(){
var auth = getAuth();
var data={
'client_id': client_id,
'client_secret': client_secret,
'grant_type':'refresh_token',
'refresh_token':auth.refresh_token
};
$.ajax({
url: "/oauth/token",
type:"post",
data:data,
async: false,
contentType: 'application/x-www-form-urlencoded',
success: function (sResponse) {
saveAuth(sResponse);
console.log('refresh_token ok: ' + sResponse.access_token+' expires_in:'+sResponse.expires_in);
},
error:function(a,b,c){
if (a.status==400 && a.responseJSON.error=='invalid_grant'){
console.log('refresh token invalid');
clearAuth();
}
}
});
}
```
发送请求前,在登录页面使用ajaxSetup()对ajax请求的token初始化并写入localStorage。登录用户名java,密码1234

如此,则所有请求头都自带token:

### 测试password认证,前端需要先发送请求获取token:


然后拿着token去拉接口:

如果token过期就刷新token:

### oauth2的认证流程

OAauth2.0包括以下角色:
- 客户端 本身不存储资源,需要通过资源拥有者的授权去请求资源服务器的资源,比如:Android客户端、Web客户端(浏览器端)、微信客户端等。
- 资源拥有者 通常为用户,也可以是应用程序,即该资源的拥有者。
- 授权服务器(也称认证服务器)用于服务提供商对资源拥有的身份进行认证、对访问资源进行授权,认证成功后会给客户端发放令牌 (access_token),作为客户端访问资源服务器的凭据。
- 资源服务器 存储资源的服务器,服务提供商会给准入的接入方一个身份,用于接入时的凭据:
client_id:客户端标识
client_secret:客户端秘钥
因此,准确来说,授权服务器对两种OAuth2.0中的两个角色进行认证授权,分别是资源拥有者、客户端。
总之,对于授权码模式,先申请授权码,然后用授权码申请token令牌,然后把token令牌放在请求头拉取其他接口。如果是密码模式,则可以跳过授权码,直接走后面两步。
### 测试授权码模式
需要先申请授权code:http://localhost:8080/oauth/authorize?response_type=code&client_id=web

- 同意授权后,页面地址会返回一个code:

- 再拿着code请求token:

- 再拿着token去拉接口:

### 测试客户端模式
直接取申请token:

- 再用token拉接口

### 附录:中央技术储备仓库(Central Technique Reserve Repository)
#### 基础篇:职业化,从做好OA系统开始
1. [Spring boot整合Mybatis实现增删改查(支持多数据源)](https://gitee.com/shenzhanwang/SSM)
2. [Struts2,Hibernate,Spring三大框架的整合实现增删改查](https://gitee.com/shenzhanwang/S2SH)
3. [Spring,SpringMVC和Hibernate的整合实现增删改查](https://gitee.com/shenzhanwang/SSH)
4. [Spring boot整合activiti工作流引擎实现OA开发](https://gitee.com/shenzhanwang/Spring-activiti)
5. [Spring发布与调用REST风格的WebService](https://gitee.com/shenzhanwang/Spring-REST)
6. [Spring boot整合Axis调用SOAP风格的web服务](https://gitee.com/shenzhanwang/Spring-axis)
7. [Spring boot整合Apache Shiro实现RBAC权限控制](https://gitee.com/shenzhanwang/Spring-shiro)
8. [使用Spring security实现RBAC权限控制](https://gitee.com/shenzhanwang/spring-security-demo)
9. [Spring整合Jasig CAS框架实现单点登录](https://gitee.com/shenzhanwang/Spring-cas-sso)
#### 中级篇:中间件的各种姿势
10. [Spring boot整合mongoDB文档数据库实现增删改查](https://gitee.com/shenzhanwang/Spring-mongoDB)
11. [Spring连接Redis实现缓存](https://gitee.com/shenzhanwang/Spring-redis)
12. [Spring连接图存数据库Neo4j实现增删改查](https://gitee.com/shenzhanwang/Spring-neo4j)
13. Spring boot整合列存数据库hbase实现增删改查
14. [Spring平台整合消息队列ActiveMQ实现发布订阅、生产者消费者模型(JMS)](https://gitee.com/shenzhanwang/Spring-activeMQ)
15. [Spring boot整合消息队列RabbitMQ实现四种消息模式(AMQP)](https://gitee.com/shenzhanwang/Spring-rabbitMQ)
16. Spring boot整合kafka 2.1.0实现大数据消息管道
17. [Spring boot整合websocket实现即时通讯](https://gitee.com/shenzhanwang/Spring-websocket)
18. [Spring security整合oauth2实现token认证](https://gitee.com/shenzhanwang/Spring-security-oauth2)
19. [Spring MVC整合FastDFS客户端实现文件上传](https://gitee.com/shenzhanwang/Spring-fastdfs)
20. 23种设计模式,源码、注释、使用场景
21. [使用ETL工具Kettle的实例](https://gitee.com/shenzhanwang/Kettle-demo)
22. Git指南和分支管理策略
23. 使用数据仓库进行OLAP数据分析(Mysql+Kettle+Zeppelin)
#### 高级篇:架构之美
24. [zookeeper原理、架构、使用场景和可视化](https://gitee.com/shenzhanwang/zookeeper-practice)
25. Spring boot整合Apache dubbo v2.7.5实现分布式服务治理(SOA架构) 
> 包含组件Spring boot v2.2.2+Dubbo v2.7.5+Nacos v1.1.1
效果图
26. 使用Spring Cloud Alibaba v2.1.0实现微服务架构(MSA架构)
> 包含组件Nacos+Feign+Gateway+Ribbon+Sentinel+Zipkin
效果图
27. 使用jenkins+centos+git+maven搭建持续集成环境自动化部署分布式服务
28. 使用docker+compose+jenkins+gitlab+spring cloud实现微服务的编排、持续集成和动态扩容
29. 使用FastDFS搭建分布式文件系统(高可用、负载均衡)
30. 搭建高可用nginx集群和Tomcat负载均衡
31. 使用mycat实现Mysql数据库的主从复制、读写分离、分表分库、负载均衡和高可用
32. [Spring boot整合Elastic search实现全文检索和大数据分析](https://gitee.com/shenzhanwang/Spring-elastic_search) 
#### 特别篇:分布式事务和并发控制
33. 基于可靠消息最终一致性实现分布式事务(activeMQ)
34. Spring boot dubbo整合seata实现分布式事务
> 包含组件nacos v1.1.0 + seata v0.7.1 +spring boot dubbo v2.7.5
效果图
35. Spring cloud alibaba v2.1.0整合seata实现分布式事务 
> 包含组件nacos v1.1.0 + seata v0.7.1 +spring cloud alibaba v2.1.0
效果图
36. 并发控制:数据库锁机制和事务隔离级别的实现
37. 并发控制:使用redis实现分布式锁 
38. 并发控制:使用zookeeper实现分布式锁
39. 并发控制:Java多线程编程实例
40. 并发控制:使用netty实现高性能NIO通信
### 视频教程

在线观看(含源码和PPT)