Coder Social home page Coder Social logo

proclinux / oauth2-server Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jobmission/oauth2-server

0.0 2.0 0.0 3.16 MB

spring boot (springboot 2+) oauth2 server sso 单点登录 认证中心 JWT,独立部署,用户管理 客户端管理

License: MIT License

Java 88.87% CSS 11.13%

oauth2-server's Introduction

SpringBoot 2.x oauth2 server, SSO 单点登录

创建数据库:持久层采用JPA框架,项目启动前必须先创建数据库,启动时数据表会自动创建

#默认用Mysql数据库,如需用其他数据库请修改配置文件以及数据库驱动
#创建数据库SQL:数据库名、数据库用户名、数据库密码需要和application.properties中的一致

CREATE DATABASE IF NOT EXISTS oauth2_server DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
create user 'oauth2_server'@'localhost' identified by 'password_dev';
grant all privileges on oauth2_server.* to 'oauth2_server'@'localhost';

#初始化数据sql在src/main/resources/sql/init.sql,可自行修改client_id等初始化数据

管理员角色登录后,可以对用户和client进行管理

支持的4种授权模式grant_type

authorization_code,implicit,password,client_credentials;
  • authorization_code模式:用于PC端,页面跳转,安全性最高,需要两步获取token
1. Get /oauth/authorize?client_id=SampleClientId&response_type=code&redirect_uri=http://client.sso.com/login/oauth2/code/sso-login
用户同意授权后服务端响应,浏览器重定向到:http://client.sso.com/login?code=1E37Xk,接收code,然后后端调用步骤2获取token
2. Post /oauth/token?client_id=SampleClientId&client_secret=tgb.258&grant_type=authorization_code&redirect_uri=http://client.sso.com/login/oauth2/code/sso-login&code=1E37Xk
响应:
{
    "access_token": "a.b.c",
    "token_type": "bearer",
    "refresh_token": "d.e.f",
    "expires_in": 43199,
    "scope": "read",
    "userId": "1",
    "jti": "823cdd71-4732-4f9d-b949-a37ceb4488a4"
}
  • password模式:用于手机端或者其他无页面跳转场景,应由后台服务端调用,保护client_id和client_secret
Post /oauth/token?client_id=SampleClientId&client_secret=tgb.258&grant_type=password&scope=read&username=zhangsan&password=tgb.258
响应:
{
    "access_token": "a.b.c",
    "token_type": "bearer",
    "refresh_token": "d.e.f",
    "expires_in": 43199,
    "scope": "read",
    "userId": "1",
    "jti": "823cdd71-4732-4f9d-b949-a37ceb4488a4"
}

RSA密钥生成,用于签名token,客户端、资源端本地验证token

使用Java工具包中的keytool制作证书jwt.jks,设置别名为【jwt】,密码为【keypass】,替换位置src/main/resources/jwt.jks
keytool -genkey -alias jwt -keyalg RSA -keysize 1024 -keystore jwt.jks -validity 3650

获取jwt token签名的RSA公钥,用于本地验证token

Get /oauth/token_key

jwk-set-uri:resource server 可以得到jwt token签名公钥并缓存,进行本地验证

Get /.well-known/jwks.json

验证token,用于在资源端调用验证token是否有效

Post /oauth/check_token?token=a.b.c

访问受保护资源,请求时携带token

Get /user/me?access_token=a.b.c
或者http header中加入Authorization,如下
Authorization: Bearer a.b.c

刷新token

Post /oauth/token?client_id=SampleClientId&client_secret=tgb.258&grant_type=refresh_token&refresh_token=d.e.f

注册新用户接口,应由后台服务端调用,保护client_id和client_secret

Post /oauth/signUp?username=lisi&password=yourpass&client_id=SampleClientId&client_secret=tgb.258

启动方法

java -jar oauth2-server-0.0.1-SNAPSHOT.jar
或者指定配置文件覆盖默认配置
java -jar oauth2-server-0.0.1-SNAPSHOT.jar --spring.config.additional-location=/path/to/override.properties

效果图

登录页 用户管理 client管理

OAuth 2 Developers Guide

spring-security-oauth官方文档
Spring Boot and OAuth2 Tutorial

client 前端DEMO
api 资源接口端DEMO

TODO LIST

  • 扩展grant_type待续
  • 分布式支持

注意!!!

当Server和Client在一台机器上时,请配置域名代理,避免cookie相互覆盖

Bcrypt 在线密码生成 Bcrypt 在线密码生成

oauth2-server's People

Contributors

jobmission avatar

Watchers

James Cloos avatar proc avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.