搭建基于OAuth2的开放平台、为APP端提供统一接口管控平台、为第三方合作伙伴的业务对接提供授信可控的技术对接平台.
- 统一API网关、访问鉴权、参数验签、外部调用更安全.
- 分布式架构,基于服务发现,Fegin(伪RPC)方式内部调用,更便捷.
- 深度整合SpringCloud+SpringSecurity+Oauth2,更细粒度、灵活的ABAC权限控制.
- 前后端分离方式开发应用,分工合作更高效!
- 代码合理封装、简单易懂、
默认登录账号:admin 123456 测试登录账号:test 123456
v-2.0.0 2019-05-01
1. 升级SpringCloud版本至Greenwich.SR1,SpringBoot 2.1.4.RELEASE
2. 重构项目结构
3. 增加SpringCloudGateway
4. 迁移gateway功能到base服务中
5. 增加MybatisPlus
6. 使用.yml代替.properties
7. 移除generator模块,将提供单独代码生成项目(计划中...)
v-1.0.0 2019-03-18
1. 重构项目结构
2. 重构表结构
3. 重构授权逻辑
4. 提取公共配置,并迁移到Nacos配置中心
5. 优化功能
open-cloud
├── docs
├── bin -- 执行脚本
├── config -- 公共配置,用于导入到nacos配置中心
├── generator -- mapper生成器
├── sql -- sql文件
├── opencloud-app -- 应用服务模块
├── app-admin -- 运营后台服务(port = 8301)
├── app-auth-demo -- 移动应用用户认证中心(多认证中心演示)(port = 7211)
├── opencloud-common -- 公共类和jar包依赖
├── opencloud-common-core -- 提供微服务相关依赖包、工具类、全局异常解析等...
├── opencloud-common-starter -- SpringBoot自动扫描
├── opencloud-gateway -- 开放API服务模块
├── opencloud-api-gateway -- API开放网关-基于SpringCloudGateway-(port = 8888)
├── opencloud-api-gateway-zuul --(较为稳定推荐使用)API开放网关-基于Zuul-(port = 8888)
├── opencloud-platform -- 平台服务模块
├── opencloud-base-client -- 平台基础服务接口
├── opencloud-base-provider -- 平台基础服务(port = 8233)
├── opencloud-auth-client -- 平台认证服务接口
├── opencloud-auth-provider -- 平台认证服务(port = 8211)
├── opencloud-msg-client -- 消息服务接口
├── opencloud-msg-provider -- 消息服务(port = 8266)
├── opencloud-scheduler-client -- 任务调度接口
├── opencloud-scheduler-provider -- 任务调度服务(port = 8501)
├── opencloud-bpm-client -- 工作流接口
├── opencloud-bpm-provider -- 工作流服务(port = 8255)
上手难度:★★★
本项目基于springCloud打造的分布式快速开发框架. 需要了解SpringCloud,SpringBoot开发,分布式原理。
-
准备环境
-
导入sql脚本
- docs/sql/oauth2.sql
- docs/sql/base.sql
- docs/sql/gateway.sql
- docs/sql/quartz.sql && scheduler.sql
-
导入配置中心,Nacos公共配置
-
修改主pom.xml
初始化
maven clean install
本地启动,默认不用修改
<!--Nacos配置中心地址--> <config.server-addr>127.0.0.1:8848</config.server-addr> <!--Nacos配置中心命名空间,用于支持多环境.这里必须使用ID,不能使用名称,默认为空--> <config.namespace></config.namespace> <!--Nacos服务发现地址--> <discovery.server-addr>127.0.0.1:8848</discovery.server-addr>
-
本地启动(顺序启动)
- BaseApplication
- AuthApplication
- ZuulGatewayApplication(推荐) 或 ApiGatewayApplication
- AdminApplication
4个服务启动成功后。就可以依赖这些服务进行微服务开发了。
访问 http://localhost:8888 -
前端启动
npm install npm run dev
-
项目打包部署
maven多环境打包
mvn clean install package -P {dev|test|online}
项目启动
1. ./docs/bin/startup.sh {start|stop|restart|status} open-base-provider.jar 2. ./docs/bin/startup.sh {start|stop|restart|status} open-auth-provider.jar 3. ./docs/bin/startup.sh {start|stop|restart|status} open-admin-provider.jar 4. ./docs/bin/startup.sh {start|stop|restart|status} open-api-gateway.jar
资源服务器Oauth2校验解析token共总结了3种存放和读取方式:
- 远程校验解析token. 缺点:需创建客户端信息,http方式,性能较差. 构建方法:OpenHelper.buildRemoteTokenServices(OpenCommonProperties properties)
- jwt校验解析token. 缺点:需提供jwt签名或密钥,生存token过长(cookie有时存放不下),且base64加密无法存放敏感数据,不方便拓展. 构建方法:OpenHelper.buildJwtTokenServices(OpenCommonProperties properties)
- (建议使用)redis校验解析token. 无需创建任何客户端和密钥,读取性能优,可存放复杂的认证信息. 缺点:必须使用同一个redis DB. 构建方法: OpenHelper.buildRedisTokenServices(RedisConnectionFactory redisConnectionFactory)
1.创建新maven项目
<!-- 引入公共包 -->
<dependency>
<artifactId>opencloud-common-starter</artifactId>
<groupId>com.opencloud</groupId>
<version>${opencloud.common.version}</version>
</dependency>
2.配置 bootstrap.yml
server:
port: 8266
spring:
application:
name: ${artifactId}
cloud:
nacos:
config:
namespace: ${config.namespace}
refreshable-dataids: common.properties
server-addr: ${config.server-addr}
shared-dataids: common.properties,db.properties,redis.properties,rabbitmq.properties
discovery:
server-addr: ${discovery.server-addr}
main:
allow-bean-definition-overriding: true
mvc:
throw-exception-if-no-handler-found: true
resources:
add-mappings: false
profiles:
active: ${profile.name}
management:
endpoints:
web:
exposure:
include: refresh,health
opencloud:
swagger2:
description: 平台消息服务
enabled: true
title: 平台消息服务
- 创建MyServiceApplication.java
//开启feign RPC远程调用 @EnableFeignClients // 开启服务发现 @EnableDiscoveryClient @SpringBootApplication public class MyServiceApplication { public static void main(String[] args) { SpringApplication.run(MyServiceApplication.class, args); } }
4.创建ResourceServerConfiguration.java 资源服务配置
@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
// 构建redis获取token,这里是为了支持自定义用户信息转换器
resources.tokenServices(OpenHelper.buildRedisTokenServices(redisConnectionFactory));
}
@Override
public void configure(HttpSecurity http) throws Exception {
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
.and()
.authorizeRequests()
// 内部访问直接放行
.antMatchers("/v1/**").permitAll()
// 只有拥有actuator权限可执行远程端点
.requestMatchers(EndpointRequest.toAnyEndpoint()).hasAnyAuthority(CommonConstants.AUTHORITY_ACTUATOR)
.anyRequest().authenticated()
.and()
//认证鉴权错误处理,为了统一异常处理。每个资源服务器都应该加上。
.exceptionHandling()
.accessDeniedHandler(new OpenAccessDeniedHandler())
.authenticationEntryPoint(new OpenAuthenticationEntryPoint())
.and()
.csrf().disable();
}
}
5.启动项目
- 平台用户认证中心(opencloud-auth-provider)
- 应用用户认证中心(app-auth-demo)
- 用户认证中心2 - 用户认证中心2
- 用户认证中心N - 根据实际应用可创建多个用户中心
针对不同应用的用户数据是单独存储,所以需要建立不同的认证中心提供用户认证。
- 采用oauth2统一协议,每个APP拥有独立的认证授权中心.
- 区分用户数据源
- 共享客户端oauth_client_details信息.
- 统一方法获取OpenHelper.getUser().认证中心标识-authCenterId.
- 个性定制,可单独提供手机验证码等方式登陆.
例: 应用信息生成的 AppId: 1553588629729 AppSecret: 1a616ba3f91141efa1c4f4a1ce725e2c
- 多认证中心,密码模式
移动应用用户认证中心:localhost:7211/oauth/token
- 授权码模式(authorization_code) 需要用户认证
- 获取code 浏览器访问
http://localhost:8211/oauth/authorize?response_type=code&client_id=1553588629729&redirect_uri=http://www.baidu.com
未登录将进入登录页,输入系统用户登录信息 用户确认授权信息 重定向到回调地址,获得code
- 客户端模式(client_credentials)
http://localhost:8211/oauth/token?grant_type=client_credentials&client_id=1553588629729&client_secret=1a616ba3f91141efa1c4f4a1ce725e2c