All Projects → zeldan → spring-boot-oauth2-password-flow

zeldan / spring-boot-oauth2-password-flow

Licence: Apache-2.0 License
Spring Boot 2 - OAuth2 password-flow with JWT

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to spring-boot-oauth2-password-flow

Spring Boot Oauth2 Jwt Swagger Ui
Spring Boot , OAuth 2 , JWT (Json Web Token) and Swagger UI
Stars: ✭ 77 (+50.98%)
Mutual labels:  oauth2, jwt-authentication
Zeus Admin
Zeus基于Golang Gin +casbin,致力于做企业统一权限&账号中心管理系统。包含账号管理,数据权限,功能权限,应用管理,多数据库适配,可docker 一键运行。社区活跃,版本迭代快,加群免费技术支持。
Stars: ✭ 404 (+692.16%)
Mutual labels:  oauth2, jwt-authentication
Jose Jwt
Ultimate Javascript Object Signing and Encryption (JOSE) and JSON Web Token (JWT) Implementation for .NET and .NET Core
Stars: ✭ 692 (+1256.86%)
Mutual labels:  oauth2, jwt-authentication
Nestjs Example
NestJS example with GraphQL, Schema-Stitching, Dataloader, GraphQL Upload, RabbitMQ, Redis, Scalable Websocket and JWT authentication
Stars: ✭ 111 (+117.65%)
Mutual labels:  oauth2, jwt-authentication
Jose2go
Golang (GO) implementation of Javascript Object Signing and Encryption specification
Stars: ✭ 150 (+194.12%)
Mutual labels:  oauth2, jwt-authentication
Php Jwt
Ultra lightweight, dependency free and standalone JSON web token (JWT) library for PHP5.6 to PHP8.0. This library makes JWT a cheese.
Stars: ✭ 214 (+319.61%)
Mutual labels:  oauth2, jwt-authentication
Oauthlib
A generic, spec-compliant, thorough implementation of the OAuth request-signing logic
Stars: ✭ 2,323 (+4454.9%)
Mutual labels:  oauth2, jwt-authentication
spring-boot-refresh-token-jwt
Spring Boot Refresh Token using JWT example - Expire and Renew JWT Token
Stars: ✭ 156 (+205.88%)
Mutual labels:  jwt-authentication, spring-boot-2
spring-batch-admin-ui
Spring Batch Admin 是一个后端采用spring boot 2, spring security , oauth2, Spring data jpa 作为基础框架,集成了quartz 提供调度能力,集成了Spring batch 提供批处理能力的管理系统。系统旨在提供更底层数据展示以及常见批处理的配置以及运行能力。
Stars: ✭ 41 (-19.61%)
Mutual labels:  oauth2
spring-boot-jwt-social-auth
Implementing JWT authentication and integrate Facebook login with it using Spring Boot
Stars: ✭ 32 (-37.25%)
Mutual labels:  jwt-authentication
spring-security-jwt-csrf
A demonstration of stateless JWT authentication with Spring Security, Spring Boot and Vue js
Stars: ✭ 62 (+21.57%)
Mutual labels:  jwt-authentication
genkan
🔑 The future of Kitsu's Authentication
Stars: ✭ 13 (-74.51%)
Mutual labels:  oauth2
KeyManager
Android application to manage SSH and GPG keys on GitHub written in Kotlin.
Stars: ✭ 15 (-70.59%)
Mutual labels:  oauth2
ms-identity-javascript-react-tutorial
A chapterwise tutorial that will take you through the fundamentals of modern authentication with Microsoft identity platform in React using MSAL React
Stars: ✭ 100 (+96.08%)
Mutual labels:  oauth2
SimpleOAuth
Simple OAuth 2.0 for Android
Stars: ✭ 15 (-70.59%)
Mutual labels:  oauth2
passport-laravel
Telegram Unofficial OAuth2 Provider for Laravel Socialite
Stars: ✭ 26 (-49.02%)
Mutual labels:  oauth2
azure-functions-auth
Authentication and Authorization for Azure Functions (with OAuth 2.0 and JWT)
Stars: ✭ 20 (-60.78%)
Mutual labels:  oauth2
httpx auth
Authentication classes to be used with httpx
Stars: ✭ 59 (+15.69%)
Mutual labels:  oauth2
react-redux-aspnet-core-webapi
No description or website provided.
Stars: ✭ 34 (-33.33%)
Mutual labels:  jwt-authentication
EasyTokenGenerator
This repo aims to dynamically and simply generate tokens in Token Based systems.
Stars: ✭ 15 (-70.59%)
Mutual labels:  jwt-authentication

spring-boot-oauth2-password-flow

It is a Spring Boot application, that contains all necessary configurations to be able to try oauth2 authorization (password flow). It uses JWT token key for the authorization.

There is a hsql embedded database in the application by default, and it contains two default users (they are uploaded by resources/data.sql)

admin / admin

  • role: ROLE_ADMIN
  • privilege: PRIVILEGE_ADMIN_READ

user / user

  • role: ROLE_USER
  • privilege: PRIVILEGE_USER_READ

You can choose postgres or mysql instead of hsql, you have to change active spring profile to postgres or mysql.

Try it

  1. Init database (postgresql / mysql sql shell)
CREATE DATABASE zeldan;
  1. start the spring-boot app

mvnw spring-boot:run

OR

mvnw spring-boot:run -Dspring.profiles.active=postgres

OR

mvnw spring-boot:run -Dspring.profiles.active=mysql

  1. get access_token for

admin

curl -X POST -vu client:secret http://localhost:8080/oauth/token -H "Accept: application/json" -d "password=admin&username=admin&grant_type=password&scope=read%20write&client_secret=secret&client_id=client"

user

curl -X POST -vu client:secret http://localhost:8080/oauth/token -H "Accept: application/json" -d "password=user&username=user&grant_type=password&scope=read%20write&client_secret=secret&client_id=client"

It will return with the bearer access_token. An example:

{
"access_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0OTE0NjYxMTYsInVzZXJfbmFtZSI6InVzZXIiLCJhdXRob3JpdGllcyI6WyJQUklWSUxFR0VfVVNFUl9SRUFEIl0sImp0aSI6IjQ4MDVhZGQ3LWMzNTgtNDkzMC05ODkwLTEzNjNkNjJiZmQ0ZiIsImNsaWVudF9pZCI6ImNsaWVudCIsInNjb3BlIjpbInJlYWQiLCJ3cml0ZSJdfQ.7nMeIVuskhkmHXxX6CC6RZf9A_aXxsaoTXev6av4h64",
"token_type":"bearer",
"refresh_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJ1c2VyIiwic2NvcGUiOlsicmVhZCIsIndyaXRlIl0sImF0aSI6IjQ4MDVhZGQ3LWMzNTgtNDkzMC05ODkwLTEzNjNkNjJiZmQ0ZiIsImV4cCI6MTQ5NDAxNDkxNiwiYXV0aG9yaXRpZXMiOlsiUFJJVklMRUdFX1VTRVJfUkVBRCJdLCJqdGkiOiI2MmU0MTU3Yy1hOWNiLTRlYjMtODg1Ni0wMmJhOWI1ZjQ3OWQiLCJjbGllbnRfaWQiOiJjbGllbnQifQ.1fexTQcFC80VkqbDo5zJfCzq0vbPPvJVPp8Nr3CwH68",
"expires_in":43199,
"scope":"read write",
"jti":"4805add7-c358-4930-9890-1363d62bfd4f"}

From this, you need "access_token", you can check what it contains exactly via jwt.io.

  1. add Authorization header, with Bearer
curl -H "Authorization: bearer <token>" http://localhost:8080/user

OR

curl -H "Authorization: bearer <token>" http://localhost:8080/admin

Of course the http://localhost:8080/admin endpoint is accessible only by admin, and the http://localhost:8080/user is accessible only by user. If you try to access the wrong endpoint with your user, then you will get an error: {"error":"unauthorized","error_description":"Full authentication is required to access this resource"}

Recommendation: Use Postman instead of curl commands.

FAQ

  1. How to add new user

If you want to add a new user, then you have to add a new line in data.sql:

INSERT INTO account (id, enabled, username, password) VALUES (3, true, <username>, <encryptedPassword>);

To generate encryptedPassword, you can use online bcrypt hash generator (e.g.: https://www.dailycred.com/article/bcrypt-calculator) or you can generate it with Spring Boot BCryptPasswordEncoder (https://docs.spring.io/spring-security/site/docs/current/apidocs/org/springframework/security/crypto/bcrypt/BCryptPasswordEncoder.html).

After that you have to insert new user into account_roles, based on what role you want to add to the user (role 1 = admin, role 2 = user).

INSERT INTO account_roles (account_id, roles_role_id) VALUES (3, 1);

Technology Stack

  • Java 8
  • Spring boot 2.1.7
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].