# springSecurityOAthu2 **Repository Path**: xmnzp/springSecurityOAthu2 ## Basic Information - **Project Name**: springSecurityOAthu2 - **Description**: oAthu2 安全认证 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2020-10-21 - **Last Updated**: 2024-05-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Spring Security OAuth2 ### 1. 概念 `OAuth`是一种用来规范令牌(`Token`)发放的授权机制,主要包含了四种授权模式:授权码模式、简化模式、密码模式和客户端模式。`Spring Security OAuth2`对这四种授权模式进行了实现。 ### 2. 授权模式 #### 2.1 认证模式 实际应用中的解释,比如我们在微信中使用携程小程序.那么在这个过程中,携程小程序要维修授权登录 这个过程就包括了`oauth2` 认证的过程 1. `Third-part-application` 第三方引用程序 这里比如 携程小程序 2. `Http server` `HTTP`服务提供商 比如这里的微信 3. `Resource owner` 资源所有者 微信的用户(自己) 4. `User agent` 用户代理 这里值手机 5 `Authorization server` 认证服务器,这里指微信提供的第三方登录服务 6. `Resource server` 资源服务 携程提供的服务,(订单查询,旅行查询,酒店住宿等 需要认证才可以使用) `http://localhost:8001/oauth/authorize?response_type=code&client_id=kiki&redirect_uri=http://localhost:8001/getCode&state=state` #### 2.2 密码模式 密码模式比起授权码模式来说,相对简单些。我们在`postman`中请求:`localhost:8001/oauth/token?grant_type=password&username=garnett&password=123456` #### 2.3 客户端模式 这种模式直接根据`client`的`id`和密钥即可获取`token`,无需用户参与。这种模式比较合适消费`api`的后端服务,不支持`refresh token` `http://localhost:8001/oauth/token?grant_type=client_credentials&client_id=kiki&state=state` ### 测试最好直接结合postman工具测试 1. 找到权限认证工具栏模块 选择项目所属的安全认证方式 ![](./docs/Snipaste_2020-10-29_16-20-00.png) 2. 点击 `GET NEW ACCESS TOKEN` ![](./docs/Snipaste_2020-10-29_16-20-57.png) 3. 常用的授权模式选择 并完成对应的配置 ![](./docs/Snipaste_2020-10-29_16-21-40.png) 4. 授权模式 ![](./docs/Snipaste_2020-10-29_16-21-52.png) 5. 客户端模式 ![](./docs/Snipaste_2020-10-29_16-22-12.png) 6. 密码模式 ![](./docs/Snipaste_2020-10-29_16-22-02.png) 7. 获取到的token示例 ![](./docs/Snipaste_2020-10-29_16-22-18.png) 8. 自动复制token所在位置 ![](./docs/Snipaste_2020-10-29_16-22-29.png) 9. 复制token测试获取用户信息 ![](./docs/Snipaste_2020-10-29_16-22-48.png)