项目作者: Esemesek

项目描述 :
Spring oauth2 example
高级语言: Java
项目地址: git://github.com/Esemesek/spring-oauth2-example.git
创建时间: 2017-06-24T08:32:00Z
项目社区:https://github.com/Esemesek/spring-oauth2-example

开源协议:MIT License

下载


What is this?

Sample SpringBoot application with OAuth2 based authorization.

How to use?

Boot run application

  1. ./gradlew clean bootRun

Get access token with password

  1. curl -XPOST -vu app:secret "http://localhost:9000/server/oauth/token?username=admin&password=admin&grant_type=password"

Response:

  1. {
  2. "access_token":"aa1dbe69-b03a-4517-966f-a9bd5e0e4747",
  3. "token_type":"bearer",
  4. "refresh_token":"89472ee5-01b1-400b-8fcf-413cf52df7f4",
  5. "expires_in":1799,
  6. "scope":"read write"
  7. }

Get access token with refresh token

  1. curl -XPOST -vu app:secret "http://localhost:9000/server/oauth/token?refresh_token=89472ee5-01b1-400b-8fcf-413cf52df7f4&grant_type=refresh_token"

Response:

  1. {
  2. "access_token":"262f25ab-943e-4014-b254-542acc808d22",
  3. "token_type":"bearer",
  4. "refresh_token":"89472ee5-01b1-400b-8fcf-413cf52df7f4",
  5. "expires_in":1799,
  6. "scope":"read write"
  7. }

Access unsecured resources

  1. curl -XGET http://localhost:9000/server/unsecured

Response:

  1. Just public stuff

Access secured resources

  1. curl -XGET http://localhost:9000/server/secured

Response:

  1. {
  2. "timestamp":1498298747485,
  3. "status":401,
  4. "error":"Unauthorized",
  5. "message":"Access Denied",
  6. "path":"/server/secured"
  7. }

Get access_token

  1. curl -XPOST -vu app:secret "http://localhost:9000/server/oauth/token?username=admin&password=admin&grant_type=password"

Response:

  1. {
  2. "access_token":"2782cbd7-852c-41f1-9477-7e2a78e6fe38",
  3. "token_type":"bearer",
  4. "refresh_token":"89f7bf5a-720b-42e8-8b47-a80222a4fbdd",
  5. "expires_in":1799,
  6. "scope":"read write"
  7. }

Request resource with Authorization header

  1. curl -XGET -H "Authorization: Bearer 2782cbd7-852c-41f1-9477-7e2a78e6fe38" http://localhost:9000/server/secured

Response:

  1. Secret stuff