/spring-rest-oauth2-sample

REST service sample that protected by Spring OAuth 2

Primary LanguageJava

spring-rest-oauth2-sample

中文版

Index

This is REST service sample be supported by:

And use specification-arg-resolver for filter.

NOTE If you need RSA sign check, you can use validateWithSignCheck of ValidateHelper

Build and Run [TOP]

$ cd <spring-rest-oauth2-sample root path>
$ ./gradlew clean build bootRun

Usage [TOP]

Data Format [TOP]

Data format of response:

{
  "code": "200",
  "operationStatus": "SUCCESS",
  "message": "Successfully",
  "data": {}
}

Use the Seed [TOP]

Get access_token [TOP]

Take your token from oauth/token in terminal, if you use ssl remember add -k:

$ curl -X POST -vu ios_app:123456 http://localhost:8080/oauth/token -H "Accept: application/json" -d "password=admin&username=admin&grant_type=password&scope=read&client_secret=123456&client_id=ios_app"

or Advanced REST Client in your Chrome with:

url: http://localhost:8080/oauth/token
POST
headers: Authorization: Basic aW9zX2FwcDoxMjM0NTY= (Encrypt client_id:client_secret by HTTP Basic)
payload: password=admin&username=admin&grant_type=password&scope=read&client_secret=123456&client_id=ios_app

Get New access_token with refresh_token [TOP]

curl -X POST -vu ios_app:123456 http://localhost:8080/oauth/token -H "Accept: application/json" -d "grant_type=refresh_token&refresh_token=<refresh_token_returned>&client_secret=123456&client_id=ios_app"

or use Advanced REST Client:

url: http://localhost:8080/oauth/token
POST
headers: Authorization: Basic <Encrypt client_id:client_secret by HTTP Basic>
payload: grant_type=refresh_token&refresh_token=<refresh_token_returned>

Access to Welcome Resource [TOP]

Use the access_token returned to make the authorized request to the protected endpoint:

$ curl -X GET http://localhost:8080/welcome -H "Authorization: Bearer <access_token_returned>"

If the request is successful, you will see the following JSON response like this:

{
  "code": "200",
  "operationStatus": "SUCCESS",
  "message": "Successfully",
  "data": {
    "id": 2,
    "content": "Hello, admin!"
  }
}

or use Advanced REST Client:

url: http://localhost:8080/welcome
GET
headers: Authorization: bearer <access_token_returned>

Access to User Resource [TOP]

1. Create New User [TOP]

curl -X POST "http://localhost:8080/resources/v1/users" -H "Authorization: bearer <access_token_returned>" -d "usr=tommy&name=tom&pwd=tom12345"

If the request is successful, you will see the following JSON response like this:

{
  "code": "200",
  "operationStatus": "SUCCESS",
  "message": "Successfully",
  "data": {
    "id": 4,
    "name": "tom",
    "usr": "tommy",
    "description": null
  }
}

or use Advanced REST Client:

url: http://localhost:8080/resources/v1/users
POST
headers: Authorization: bearer <access_token_returned>
payload: usr=tommy&name=tom&pwd=tom12345

2. Show All Users [TOP]

$ curl -X GET "http://localhost:8080/resources/v1/users" -H "Authorization: bearer <access_token_returned>"

If the request is successful, you will see the following JSON response like this:

{
  "code": "200",
  "operationStatus": "SUCCESS",
  "message": "Successfully",
  "data": [
      {
        "id": 4,
        "name": "tom",
        "usr": "tommy",
        "description": null
      },
      {
        "id": 2,
        "name": "admin",
        "usr": "admin",
        "description": "admin account"
      },
      {
        "id": 1,
        "name": "root",
        "usr": "root",
        "description": "root account"
      },
      {
        "id": 3,
        "name": "guest",
        "usr": "guest",
        "description": "guest account"
      }
    ]
  }
}

or use Advanced REST Client:

url: http://localhost:8080/resources/v1/users
GET
headers: Authorization: bearer <access_token_returned>

You can add filter params like:

$ curl -X GET "http://localhost:8080/resources/v1/users?name=tom&createdDateAfter=2016-06-01&createdDateBefore=2016-07-30&sortBy=id:desc,name:desc" -H "Authorization: bearer <access_token_returned>"

If the request is successful, you will see the following JSON response like this:

{
  "code": "200",
  "operationStatus": "SUCCESS",
  "message": "Successfully",
  "data": [
    {
      "id": 5,
      "name": "tommy",
      "usr": "tom",
      "description": null
    },
    {
      "id": 4,
      "name": "tom",
      "usr": "tommy",
      "description": null
    }
  ]
}

3. Show Users in Page [TOP]

$ curl -X GET "http://localhost:8080/resources/v1/users?pageNo=1&pageSize=20&name=tom&sortBy=id:asc,name:desc" -H "Authorization: Bearer <access_token_returned>"

If the request is successful, you will see the following JSON response like this:

{
  "code": "200",
  "operationStatus": "SUCCESS",
  "message": "Successfully",
  "data": {
    "content": [
      {
        "id": 4,
        "name": "tom",
        "usr": "tommy",
        "description": null
      },
      {
        "id": 5,
        "name": "tommy",
        "usr": "tom",
        "description": null
      }
    ],
    "totalElements": 2,
    "last": true,
    "totalPages": 1,
    "size": 20,
    "number": 0,
    "sort": [
      {
        "direction": "ASC",
        "property": "id",
        "ignoreCase": false,
        "nullHandling": "NATIVE",
        "ascending": true
      },
      {
        "direction": "DESC",
        "property": "name",
        "ignoreCase": false,
        "nullHandling": "NATIVE",
        "ascending": false
      }
    ],
    "first": true,
    "numberOfElements": 2
  }
}

or use Advanced REST Client:

url: http://localhost:8080/resources/v1/users?pageNo=1&pageSize=20&name=tom&sortBy=id:asc,name:desc
GET
headers: Authorization: bearer <access_token_returned>

NOTE:

Param name Type Description
pageNo int Must be equal or greater than 1
pageSize int Must be equal or greater than 1
sortBy string Like paramA:asc,paramB:desc,paramC:asc,...

4. Show User by id [TOP]

$ curl -X GET "http://localhost:8080/resources/v1/users/4" -H "Authorization: Bearer <access_token_returned>"

If the request is successful, you will see the following JSON response like this:

{
  "code": "200",
  "operationStatus": "SUCCESS",
  "message": "Successfully",
  "data": {
    "id": 4,
    "name": "tom",
    "usr": "tommy",
    "description": null
  }
}

or use Advanced REST Client:

url: http://localhost:8080/resources/v1/users/4
GET
headers: Authorization: bearer <access_token_returned>

5. Update User by id [TOP]

curl -X PUT "http://localhost:8080/resources/v1/users/4" -H "Authorization: bearer <access_token_returned>" -d "usr=tommy&name=jerry&pwd=tom54321"

If the request is successful, you will see the following JSON response like this:

{
  "code": "200",
  "operationStatus": "SUCCESS",
  "message": "Successfully",
  "data": {
    "id": 4,
    "name": "jerry",
    "usr": "tommy",
    "description": null
  }
}

or use Advanced REST Client:

url: http://localhost:8080/resources/v1/users/4
PUT
headers: Authorization: bearer <access_token_returned>
payload: usr=tommy&name=jerry&pwd=tom54321

6. Delete User by id [TOP]

curl -X DELETE "http://localhost:8080/resources/v1/users/4" -H "Authorization: bearer <access_token_returned>"

If the request is successful, you will see the following JSON response like this:

{
  "code": "200",
  "operationStatus": "SUCCESS",
  "message": "Delete user successfully.",
  "data": null
}

or use Advanced REST Client:

url: http://localhost:8080/resources/v1/users/4
DELETE
headers: Authorization: bearer <access_token_returned>

Other resources [TOP]

Refer to previous user resource. And you can generate the sign with SignTest

Deploy [TOP]

  1. Build war and use tomcat.
  2. Build jar and run java -jar foo.jar
  3. Use Docker. You can build your docker image by Dockerfile. And run it with docker-compose.yml.

License [TOP]

MIT

Copyright (c) since 2015 saintdan

Version History [TOP]

Version history is here. ;)