GithubHelp home page GithubHelp logo

wainermora / oauthserver Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jeesun/oauthserver

0.0 2.0 0.0 1.19 MB

快速实现Spring Boot Oauth2 Server服务,保护你的后台应用。

License: Apache License 2.0

Java 94.06% Dockerfile 0.06% Batchfile 0.16% FreeMarker 2.97% HTML 2.75%

oauthserver's Introduction

oauthserver

简介

oauthserver是一个基于Spring Boot Oauth2的完整的独立的Oauth2 Server微服务。仅仅需要创建相关数据表,修改数据库的连接信息,你就可以得到一个Oauth2 Server微服务。

Oauth2 Client通常是要被保护的资源,例如app接口。配套的Oauth2 Client示例可以在这里找到https://github.com/jeesun/qbankserver

支持的关系型数据库:

  • PostgreSQL
  • MySQL
  • Oracle

已实现的功能:

  1. token保存到关系型数据库;
  2. 获取token时,username允许传用户名、手机号或者邮箱;
  3. 自定义登录页面和授权页面,token获取支持密码模式和授权码模式;
  4. 集成druid,并开启druid监控,访问http://localhost:8182/druid,用户名simon,密码19961120;
  5. 集成Mybatis,以及Mybatis三大插件:通用Mapper,Mybatis-Plus,PageHelper;
  6. 集成swagger2,并配置非全局、无需重复输入的header参数(token),访问http://localhost:8182/swagger-ui.html,全局Authorize的值为"Bearer "+"access_token",注意"Bearer"和"access_token"之间有一个空格;
  7. 集成Redis缓存,默认使用Ehcache缓存,若要切换成Redis缓存,请查看application.yml缓存配置注释;
  8. 集成阿里大鱼(需要安装阿里大鱼jar,安装方法:运行src/main/resources/jars/install.bat);
  9. 代码生成器,运行com/simon/common/code/CodeGenerator.java的main方法进行代码生成,代码生成器的配置在code-gen.properties。可生成Model、Mapper、Repository、Service、ServiceImpl、Controller。需要注意的是,生成的Model需要手动添加@Entity注解,生成的Mapper需要手动添加@Repository注解。

请下载与Spring Boot对应版本的oauthserver:

Spring Boot version oauthserver version
1.x.x 1.x.x
2.x.x 2.x.x

升级指南

  1. 使用v2.x版本,需要修改数据表oauth_client_details的clicent_secret列的值,从明文secret改为经过Scrypt加密的字符串$2a$11$uBcjOC6qWFpxkQJtPyMhPOweH.8gP3Ig1mt27mGDpBncR7gErOuF6;
  2. oauthserver v1.2.0添加了阿里大鱼的发送验证码功能,需要阿里大鱼的jar,安装方法:运行src/main/resources/jars/install.bat。

更新日志

v1.2.4.beta(2018-08-18)

  • 代码生成器bug修复;
  • 重新生成实体类。

v1.2.4.alpha(2018-08-11)

  • 代码生成器配置文件code-gen.properties
  • 修复mysql建表语句bug;
  • 添加对Oracle的支持。

v1.2.3(2018-08-05)

  • 内置代码生成器;
  • 移动config目录到common目录下;
  • 缓存UserDetails到Ehcache,若spring.cache.type=redis,则自动缓存UserDetails到Redis;
  • clients信息存入内存,提高查询效率;
  • 开启druid监控,访问http://localhost:8182/druid,用户名simon,密码19961120。

v1.2.2(2018-08-01)下载

  • 合并users和user_info表,authorities表使用user_id代替username字段;
  • 集成阿里大鱼,需要安装阿里大鱼jar,安装方法:运行src/main/resources/jars/install.bat;
  • 集成Mybatis,以及Mybatis三大插件:通用Mapper,Mybatis-Plus,PageHelper;
  • 集成swagger2,并配置非全局、无需重复输入的header参数(token),访问http://localhost:8182/swagger-ui.html
  • 集成Redis缓存,默认不开启;
  • 自定义登录页面和授权页面,并修复授权码模式与密码模式共存问题;
  • 更棒的接口示例HelloWorldController,强烈建议您阅读该Controller代码。

v2.0.0.alpha(2018-07-16)下载

  • 升级Spring Boot版本从1.5.14.RELEASE到2.0.3.RELEASE。

使用流程

1. 建表

  • PostgreSQL
    请执行src/main/resources/schema-pg.sql,完成数据表的创建和测试数据的导入。
  • MySQL
    请执行src/main/resources/schema-mysql.sql,完成数据表的创建和测试数据的导入。
  • Oracle
    请执行src/main/resources/schema-oracle.sql,完成数据表的创建和测试数据的导入。

2. 修改数据库连接信息

  • PostgreSQL
    连接信息在application-pg.yml里。修改完数据库连接信息后,还需要设置application.ymlspring.profiles.active=pg
  • MySQL
    连接信息在application-mysql.yml里。修改完数据库连接信息后,还需要设置application.ymlspring.profiles.active=mysql
  • Oracle
    连接信息在application-oracle.yml里。修改完数据库连接信息后,还需要设置application.ymlspring.profiles.active=oracle
    数据库连接信息的配置项username和password可以经过jasypt加密,也可以直接填明文。若要使用jasypt加密,加密密钥由jasypt.encryptor.password配置,你可以使用test目录下的UtilTests工具得到加密字符串。

3. 运行

现在,一切已准备就绪。运行项目,当程序成功启动时,即表明你已配置成功。

4. 测试

com.simon.common.config.OAuthSecurityConfig.javaclients配置如下:

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.inMemory()
            .withClient("clientIdPassword")
            .secret("$2a$11$uBcjOC6qWFpxkQJtPyMhPOweH.8gP3Ig1mt27mGDpBncR7gErOuF6") //明文secret
            .scopes("read,write,trust")
            .authorizedGrantTypes("authorization_code", "refresh_token", "password", "client_credentials")
            .authorities("ROLE_ADMIN", "ROLE_USER")
            .accessTokenValiditySeconds(7200)//access_token有效期为2小时
            .refreshTokenValiditySeconds(5184000)//refresh_token有效期为2个月60天
            .autoApprove(false);
    //clients.jdbc(dataSource);
}

token相关的接口,都需要进行Basic Oauth认证。
如下图所示:
截图

1、根据用户名和密码获取access_token

POST http://localhost:8182/oauth/token?grant_type=password&username=jeesun&password=1234567890c

成功示例
status=200,返回的json数据:

{
    "access_token": "ca582cd1-be6c-4a5a-82ec-10af7a8e06eb",
    "token_type": "bearer",
    "refresh_token": "c24a6143-97c8-4642-88b9-d5c5b902b487",
    "expires_in": 3824,
    "scope": "read write trust"
}

失败示例

  1. 用户名错误
    status=400,返回的json数据:
{
    "code": 400,
    "message": "用户名不存在",
    "data": null
}
  1. 密码错误
    status=400,返回的json数据:
{
    "code": 400,
    "message": "密码错误",
    "data": null
}
  1. 账号被封enabled=false
    status=400,返回的json数据:
{
    "code": 400,
    "message": "您已被封号",
    "data": null
}

2、检查access_token

GET http://localhost:8182/oauth/check_token?token=ca582cd1-be6c-4a5a-82ec-10af7a8e06eb

成功示例
即使用户被封enabled=false,access_token未过期仍然可用。
status=200,返回的json数据:

{
    "aud": [
        "oauth2-resource"
    ],
    "exp": 1524507296,
    "user_name": "jeesun",
    "authorities": [
        "ROLE_ADMIN",
        "ROLE_USER"
    ],
    "client_id": "clientIdPassword",
    "scope": [
        "read",
        "write",
        "trust"
    ]
}

失败示例
access_token已过期
status=400,返回的json数据:

{
    "code": 400,
    "message": "Token无法识别",
    "data": null
}

3、根据refresh_token获取新的access_token

POST http://localhost:8182/oauth/token?grant_type=refresh_token&refresh_token=c24a6143-97c8-4642-88b9-d5c5b902b487

成功示例
status=200,返回的json数据:

{
    "access_token": "690ecd7d-f2b7-4faa-ac45-5b7a319478e8",
    "token_type": "bearer",
    "refresh_token": "c24a6143-97c8-4642-88b9-d5c5b902b487",
    "expires_in": 7199,
    "scope": "read write trust"
}

失败示例
用户被封enabled=false
status=401,返回的json数据:

{
    "code": 401,
    "message": "用户已失效",
    "data": null
}

4、根据授权码获取token

POST http://localhost:8182/oauth/authorize?response_type=code&client_id=clientIdPassword&scope=read&redirect_uri=http://www.baidu.com 登录 授权
同意授权,跳转到https://www.baidu.com/?code=jgA1h3jgA1h3就是授权码。
使用授权码获取token:
POST http://localhost:8182/oauth/token?grant_type=authorization_code&code=jgA1h3&redirect_uri=http://www.baidu.com

成功示例
status=200,返回的json数据:

{
    "access_token": "ca582cd1-be6c-4a5a-82ec-10af7a8e06eb",
    "token_type": "bearer",
    "refresh_token": "c24a6143-97c8-4642-88b9-d5c5b902b487",
    "expires_in": 3824,
    "scope": "read write trust"
}

5. 关于国际化

接口参数添加locale,值为语言区域,例如zh_CN,zh_CH,en_US。

app实践指南

app获取到token信息后,需要保存token信息和请求时间。在传access_token之前,需要检查access_token是否过期。为了减少后台压力,检查access_token是否过期应该是在app本地完成。通过token的keyexpires_in(剩余有效期)的值,以及本地记录的请求时间,和当前时间做对比,可以很方便地判断出access_token是否过期。如果过期了,需要通过refresh_token获取新的access_token。因为access_token的有效期只有2个小时,这个验证是必须的。
refresh_token同理。

oauthserver's People

Contributors

jeesun avatar

Watchers

 avatar  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.