# springboot2-oauth2 经典使用示例 **Repository Path**: luck/oauth2 ## Basic Information - **Project Name**: springboot2-oauth2 经典使用示例 - **Description**: springboot2-oauth2 经典使用示例 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 5 - **Forks**: 0 - **Created**: 2019-08-05 - **Last Updated**: 2023-03-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README #项目说明 本项目是 SpringBoot2 + spring-security-oauth2 使用示例,实现了以下四和授权模式。 (1)授权码模式(Authorization Code) (2)授权码简化模式(Implicit) (3)Pwd模式(Resource Owner Password Credentials) (4)Client模式(Client Credentials) 项目提供的所有用户和client 的密码都为123456 #安装运行 导入oauth2.sql 修改 application.yml的数据源 运行 ``` mvn spring-boot:run ``` ### 授权码模式(Authorization Code)使用说明 1. 尝试直接访问用户信息 ```http http://localhost:8080/user/info ``` 提示需要认证: ```xml Full authentication is required to access this resource unauthorized ``` 2. 尝试获取授权码 ```http http://localhost:8080/oauth/authorize?client_id=client_3&response_type=code&scope=read&redirect_uri=http://localhost:8080/code?client_id=client_3 ``` 因为这里会弹出 HTTP Basic认证,必须登录的用户才能申请 code。 3. 输入用户名和密码 username=user_1 passpord=123456 如上用户名密码是交给 SpringSecurity 的主过滤器用来认证的 4. 登录成功后,真正进行授权码的申请 oauth/authorize 认证成功,会根据 redirect_uri 执行 302 重定向,并且带上生成的 code,注意重定向到的是 8080 端口,这个时候已经是另外一个应用了。 ```http http://localhost:8080/code?client_id=client_3&code=c3FbHM ``` 代码中封装了一个 http 请求, 使用 restTemplate 向 认证服务器发送 token 的申请,当然是使用 code 来申请的,并最终成功获取到 access_token ```json { "access_token":"5db93d64-2252-4349-90a3-e4d6637f90ae", "refresh_token":"5a67faae-38ed-4e5c-a809-c9d07c16abcb", "scope":"read", "token_type":"bearer", "expires_in":42494 } ``` 5. 携带 access_token 访问 用户 信息 ```http http://localhost:8080/user/info?access_token=5db93d64-2252-4349-90a3-e4d6637f90ae ``` 正常返回信息 ```json { "password":null, "username":"user_1", "authorities":[{"authority":"ROLE_USER"}], "accountNonExpired":true, "accountNonLocked":true, "credentialsNonExpired":true, "enabled":true } ``` ### 授权码简化模式(Implicit) 使用说明 Implicit与Authorization_code 区别是 Implicit不需要验证client_secret,请求如果成功会直接返回 token 获取授权码 ```http http://localhost:8080/oauth/authorize?response_type=token&client_id=client_4&scope=read&redirect_uri=http://localhost:8080/param ``` 如果成功会重定向到URL(token在URL里) ```http request http://localhost:8080/param#access_token=85090391-2c33-4a75-a989-116bb06b0c5a&token_type=bearer&expires_in=42962&scope=read ``` ### 密码模式(Resource Owner Password Credentials)使用说明 请求 Access Token: ```http request http://localhost:8080/oauth/token?username=user_1&password=123456&grant_type=client_credentials&scope=read&client_id=client_1&client_secret=123456 ``` 正常返回信息 ```json { "access_token":"fb1a1d03-9658-4d92-822a-d988c9f7a923", "token_type":"bearer", "expires_in":43148, "scope":"read" } ``` ### Client模式(Client Credentials)使用说明 请求 Access Token: ```http request http://localhost:8080/oauth/token?grant_type=client_credentials&scope=read&client_id=client_1&client_secret=123456 ``` 正常返回信息 ```json { "access_token": "fb1a1d03-9658-4d92-822a-d988c9f7a923", "token_type": "bearer", "expires_in": 42811, "scope": "read" } ```