Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Spring Boot 2.1.x OAuth2 JWT Authorization server with Database (JPA, Hibernate, PostgreSQL, Liquibase)

License

NotificationsYou must be signed in to change notification settings

odenktools/springboot-oauth2-jwt

Repository files navigation

Sample Spring Boot 2.0.x OAuth2 JWT Authorization Server(JWT, JPA, Hibernate, PostgreSQL, Dockerize).

You can use this project to boostraping Authorization your own Application.

If you feel happyGive me a STAR to thisrepository.

Features

  • OAuth2.0 Authorization Server
  • OAuth2.0 Resource Server
  • We usePostgreSQL
  • We useLiquibase
  • We useMaven
  • We useDocker, to simplify understanding application flow.
  • We useJWT Token RSA, production ready.
  • File upload API (Using Oauth2 Resource Server)
  • How to use Validation.
  • How to integrating QueryDSL into JPA.

Oauth2 user credential

UserPasswordauthorized grant types
server-serverserver-serverclient_credentials
external-serverexternal-serverclient_credentials
android-clientandroid-clientpassword,refresh_token,implicit,authorization_code

Customer credential

UserPassword
customer_onecustomer_one
customer_twocustomer_two
customer_badcustomer_bad

How to Running On Docker

For running on Docker instance, it's really simple.

  • Clone this repository.
  • cd springboot-oauth2-jwt
  • docker network create odenktools-net
  • Rundocker-compose up -d
  • make a cup of coffee...
  • You're done! application ready to test.

Testing Apps

server-server login

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"}

Customer Login (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

{"access_token":"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsicmVzb3VyY2VfaWQiXSwidXNlcl9uYW1lIjoiY3VzdG9tZXJfb25lIiwic2NvcGUiOlsicmVhZCIsIndyaXRlIiwidHJ1c3QiXSwiZXhwIjoxNTQ0NjMxNTU1LCJhdXRob3JpdGllcyI6WyJST0xFX01PQklMRSIsIlVQREFURV9DT01QQU5ZIiwiUkVBRF9DT01QQU5ZIiwiUkVBRF9BUElLRVkiXSwianRpIjoiNzQ1OGY3YzctYWNhOC00ZGVlLWJmMDAtOGI0YTJkYWU1YTgyIiwiY2xpZW50X2lkIjoiYW5kcm9pZC1jbGllbnQifQ.NsUtA-VnfVkFFEYnsypv8_fSXT3b0JBWBxXAo_5DHOZ9MEk3PUtNcIkIjsZm1kF6G9j8fCTENSq5-EMKChfywvkhiyvmKxX7j52WKqes8SWU2e2vrDCHfs3xXQTH8x4ISdMTMz88hyt2n8fMIPCn6CtNBdv2FUZsRdfV2JBSLEteV55R4GVs0x7dWc--OyQqvySWsZ0W2YtBh2kSXG1OsIwAXbnPpGRPckBlef7QuG2XEr7CfKQZ5rIy5OqFyI-HvR8q-eA_ZNn70GeAJiVY7Op5FMHMUa70aB5sdiTK2jp_dcoEsLE3WYS4d66maUqnRFbEBNWL6h4T-BjosiVWeQ","token_type":"bearer","refresh_token":"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsicmVzb3VyY2VfaWQiXSwidXNlcl9uYW1lIjoiY3VzdG9tZXJfb25lIiwic2NvcGUiOlsicmVhZCIsIndyaXRlIiwidHJ1c3QiXSwiYXRpIjoiNzQ1OGY3YzctYWNhOC00ZGVlLWJmMDAtOGI0YTJkYWU1YTgyIiwiZXhwIjoxNTQ0NjMxNTU1LCJhdXRob3JpdGllcyI6WyJST0xFX01PQklMRSIsIlVQREFURV9DT01QQU5ZIiwiUkVBRF9DT01QQU5ZIiwiUkVBRF9BUElLRVkiXSwianRpIjoiODdiN2JkY2YtNWFmOC00ZGEwLWJiM2MtNGZlZjkwNzRmYzBhIiwiY2xpZW50X2lkIjoiYW5kcm9pZC1jbGllbnQifQ.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/{{IMAGE_WANT_TO_UPLOAD}}.jpg'

User Group

Get all groups on database

curl --request GET \  --url http://localhost:8090/api/v1/admin/group/list \  --header'Authorization: Bearer {{YOUR_ACCESS_TOKEN}}' \  --header'Content-Type: application/json'

Get all groups on database with filtering data

curl --request GET \  --url'http://localhost:8090/api/v1/admin/group/list?name=Super%20Admin&coded=coded&page=0&size=10' \  --header'Authorization: Bearer {{YOUR_ACCESS_TOKEN}}'

Result

{"content": [        {"id":11,"named":"johndoe","coded":"johndoe","namedDescription":"johndoe group","isActive":1,"updatedAt":null,"created_at":"19-12-2018 04:40:08"        }    ],"number":0,"size":10,"totalElements":1,"pageable": {"sort": {"sorted":false,"unsorted":true        },"offset":0,"pageSize":10,"pageNumber":0,"paged":true,"unpaged":false    },"last":true,"totalPages":1,"sort": {"sorted":false,"unsorted":true    },"first":true,"numberOfElements":1}

Create a new user group

curl --request POST \  --url http://localhost:8090/api/v1/admin/group/create \  --header'Authorization: Bearer {{YOUR_ACCESS_TOKEN}}' \  --header'Content-Type: application/json' \  --data'{\n"named": "odenktools",\n"coded": "odenktoolsIuX",\n"namedDescription": "Super Admin Group",\n"isActive": 1\n}'

Result

{"code":200,"messages":"Group with name ``John Doe`` was successfuly added"}

Get user group by id

curl --request GET \  --url http://localhost:8090/api/v1/admin/group/{{GROUP_ID}} \  --header'Authorization: Bearer {{YOUR_ACCESS_TOKEN}}' \  --header'Content-Type: application/json'

Result

{"code":200,"data": {"named":"Role SuperAdmin","coded":"ROLE_SUPER_ADMIN","namedDescription":"Role For SuperAdmin","isActive":1,"createdAt":"2018-12-12T15:10:14.979122Z","updatedAt":null    }}

Delete user group by id

curl --request DELETE \  --url http://localhost:8090/api/v1/admin/group/delete/{{GROUP_ID}} \  --header'Authorization: Bearer {{YOUR_ACCESS_TOKEN}}' \  --header'Content-Type: application/json'

Result

{"code":200,"messages":"Group was successfuly removed."}

Build Application from source

First clone this repo. To build the sources you need to haveGradle 4x installed.

After the clone, create database

  • Authorization Server
su - postgrescreatedb auth_server
  • Resource Server
su - postgrescreatedb file_server

Aftercreate database, build the entire project.

  • Authorization Server
cd authorization_servermaven package
  • Resource Server
cd resource_servermaven package

Generate RSA KeyStore

keytool -genkey -alias jwt -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore jwt.p12 -keypass odenktools123 -validity 3650

paste tomain/resources/certificate folder

keytool -list -rfc --keystore jwt.p12| openssl x509 -inform pem -pubkey

paste tomain/resources/certificate folder

Todo List

  • Oauth2.0 JWT Token
  • Remote Token
  • Client Registration API
  • Admin UI for manageOauth2 user credential andcustomers
  • Custom authorization scope

References

Official - Oauth2 Sample Code

Official - Spring Oauth2 Docs

Official - Spring Oauth2 ref

Official - Spring Oauth2 Tutorial

Oauth Playground

Dzone - Build a Spring Boot App With Secure Server-to-Server Communication via OAuth 2.0

Okta - Secure Server-to-Server Communication with Spring Boot and OAuth 2.0

LICENSE

MIT License

Copyright (c) 2018 - 2020 odenktools

Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, 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 THESOFTWARE.

About

Spring Boot 2.1.x OAuth2 JWT Authorization server with Database (JPA, Hibernate, PostgreSQL, Liquibase)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp