Sample Spring Boot 2.0.x OAuth2 JWT Authorization Server (JPA, Hibernate, PostgreSQL, Dockerize).
If you feel happy give a star to this repository.
- OAuth2.0 Authorization Server
- OAuth2.0 Resource Server
- Liquibase
- Gradle 4.10.2
- Docker
- JWT Token RSA
- Sample File Upload (Using Oauth2 Resource Server)
For running on Docker instance, it's really simple.
- Clone this repo
- Run
docker-compose up -d
Login server-server
curl --request POST \ --user server-server:server-server \ --url http://localhost:8090/oauth/token \ --header 'Content-Type: application/x-www-form-urlencoded' \ --data grant_type=client_credentials
the result will be like
{ "access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsicmVzb3VyY2VfaWQiXSwic2NvcGUiOlsicmVhZCIsIndyaXRlIiwidHJ1c3QiXSwiZXhwIjoxNTQ0NjI4MTQ1LCJqdGkiOiI1NDVjMWM3MC0xNDMxLTQxZTItYTg4MS03MWIyZDNhNmNkYTQiLCJjbGllbnRfaWQiOiJzZXJ2ZXItc2VydmVyIn0.EheboSEeO-_bvhYjBrX0bdZ0pDBCzHg02hKTBCVQuqOF6jcz7ZSMonC-i9ErHmmlx9ZDKW2EChzfltxhpcYcV0jZwR7eQSWT9CVUjl8jtkYrWTqf8qOy3R_t9sC7KeJSmLOiYSZ0FyBfiP6Fa65UjDvL5K1cxzYP75AjPBNwIdHr5vP4NGdI0XWOWNHrm4lKqXcBRiD9iS-dMRc5zkMfD4Mtp8clrMbIROlQOH3u9g6XudhB-LkwzjzQdyH2kQrTqbIPOGchgfluhcR0db95KptM0BVT-KFaNY-YxpCKPJRTGIoqa8ogS-8T3vZlZvKWsxj4Rpn1zLiRcblav0dO6w", "token_type": "bearer", "expires_in": 450, "scope": "read write trust", "jti": "545c1c70-1431-41e2-a881-71b2d3a6cda4" }
Login customer (end user for your apps)
curl --request POST \ --user android-client:android-client \ --url http://localhost:8090/oauth/token \ --header 'Content-Type: application/x-www-form-urlencoded' \ --data 'username=customer_one&grant_type=password&password=customer_one'
the result will be like
{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XSwianRpIjoiODdiN2JkY2YtNWFmOC00ZGEwLWJiM2MtNGZlZjkwNzRmYzBhIiwiY2xpZW50X2lkIjoiYW5kcm9pZC1jbGllbnQifQ.JXBdkfD5HTxlMYf1hm8H3QKQ9dhhAMoH1gbl_Lv3SALXOyOVuc3N_OvM5YEBbWMwXfJlHPV07uGnMovFwINV2OPGUkaqzHIC-aCQr-__n8rDYmJRmU-48PiiOdV-nZgIwTUTZEkz1DxI4S4ZvZci59VNOUUyq8oGRUmKmDBTtqW4UGGaElX5xmAecE-qbXpj77VyWv4uOJoToO53_-hPfBO31fRXiEEO0lzFEOq_yeNg1M6NFekqeGqPXQZgOgzbnJcv4EIXueB9phDDD6zLab3iAmgDQdKZI8boVLDHek5PfdJd10A2RjMvjepMq5SD7W1dCXn_3lZRqoGbBR8PVQ", "expires_in": 499, "scope": "read write trust", "jti": "7458f7c7-aca8-4dee-bf00-8b4a2dae5a82" }
Customer Check Oauth2 Token
curl --request POST \ --user android-client:android-client \ --url http://localhost:8090/oauth/check_token \ --header 'Content-Type: application/x-www-form-urlencoded' \ --data token={{YOUR_ACCESS_TOKEN}}
the result will be like
{ "aud": [ "resource_id" ], "user_name": "customer_one", "scope": [ "read", "write", "trust" ], "active": true, "exp": 1544631555, "authorities": [ "ROLE_MOBILE", "UPDATE_COMPANY", "READ_COMPANY", "READ_APIKEY" ], "jti": "7458f7c7-aca8-4dee-bf00-8b4a2dae5a82", "client_id": "android-client" }
Customer Refresh Oauth2 Token
curl --request POST \ --url http://localhost:8090/oauth/token \ --header 'Content-Type: application/x-www-form-urlencoded' \ --data 'grant_type=refresh_token&token={{YOUR_ACCESS_TOKEN}}&refresh_token={{YOUR_REFRESH_TOKEN}}'
Access Customer Profile
curl --request GET \ --url http://localhost:8091/api/v1/customer/me \ --header 'Authorization: Bearer {{YOUR_ACCESS_TOKEN}}' \
the result will be like
{ "messages": "Welcome again ``customer_one``. And Happy nice day!" }
File Upload
curl --request POST \ --url http://localhost:8091/api/v1/file/upload \ --header 'Authorization: Bearer {{YOUR_ACCESS_TOKEN}}' \ --header 'Content-Type: application/x-www-form-urlencoded' \ --header 'content-type: multipart/form-data' \ --form 'file=@/opt/pictures/your-images.jpg'
First clone this repo. To build the sources you need to have Gradle 4x installed.
After the clone, create database
- For Authorization Server
su - postgres createdb auth_server
- For Resource Server
su - postgres createdb file_server
After create database, build the entire project.
For Authorization Server
cd authorization_server gradle build
For Resource Server
cd resource_server gradle build
Generate RSA KeyStore
keytool -genkey -alias jwt -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore jwt.p12 -keypass odenktools123 -validity 3650
keytool -list -rfc --keystore jwt.p12 | openssl x509 -inform pem -pubkey
User : server-server
Password : server-server
User : android-client
Password : android-client
User : external-server
Password : external-server
User : customer_one
Password : customer_one
- [] Admin UI for manage users and customers credentials
MIT License
Copyright (c) 2018 odenktools
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.