mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-04-05 17:37:53 +08:00
commit
6675fc5283
20
sa-token-demo/sa-token-demo-grpc/client/pom.xml
Normal file
20
sa-token-demo/sa-token-demo-grpc/client/pom.xml
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>sa-token-demo-grpc</artifactId>
|
||||||
|
<groupId>com.lym</groupId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>client</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.lym;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lym
|
||||||
|
* @description
|
||||||
|
* @date 2022/8/26 10:58
|
||||||
|
**/
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableDiscoveryClient
|
||||||
|
public class Client {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(Client.class);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
package com.lym.controller;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
|
import com.lym.grpc.client.GrpcAuthService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lym
|
||||||
|
* @description
|
||||||
|
* @date 2022/8/26 11:01
|
||||||
|
**/
|
||||||
|
@RestController
|
||||||
|
public class TestController {
|
||||||
|
@Autowired
|
||||||
|
private GrpcAuthService grpcAuthService;
|
||||||
|
|
||||||
|
// 客户端登录,状态带到服务端。
|
||||||
|
@RequestMapping("test")
|
||||||
|
public void test() {
|
||||||
|
System.out.println("登录前:" + grpcAuthService.isLogin());
|
||||||
|
System.out.println("登录前:" + StpUtil.isLogin());
|
||||||
|
|
||||||
|
StpUtil.login(1);
|
||||||
|
|
||||||
|
System.out.println("登录后:" + grpcAuthService.isLogin());
|
||||||
|
System.out.println("登录后:" + StpUtil.getTokenValue());
|
||||||
|
System.out.println("登录后:" + StpUtil.getLoginId());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 服务端登录,登录状态带回客户端
|
||||||
|
@RequestMapping("test2")
|
||||||
|
public String test2() {
|
||||||
|
System.out.println("登录前:" + grpcAuthService.isLogin());
|
||||||
|
System.out.println("登录前:" + StpUtil.isLogin());
|
||||||
|
|
||||||
|
String token = grpcAuthService.login(3);
|
||||||
|
|
||||||
|
System.out.println("登录后:" + grpcAuthService.isLogin());
|
||||||
|
System.out.println("登录后:" + StpUtil.getTokenValue());
|
||||||
|
System.out.println("登录后:" + StpUtil.getLoginId());
|
||||||
|
assert StpUtil.getTokenValue().equals(token);
|
||||||
|
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.lym.grpc.client;
|
||||||
|
|
||||||
|
import com.google.protobuf.Empty;
|
||||||
|
import com.lym.grpc.auth.Auth;
|
||||||
|
import com.lym.grpc.auth.AuthServiceGrpc;
|
||||||
|
import net.devh.boot.grpc.client.inject.GrpcClient;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lym
|
||||||
|
* @description
|
||||||
|
* @date 2022/8/26 11:02
|
||||||
|
**/
|
||||||
|
@Service
|
||||||
|
public class GrpcAuthService {
|
||||||
|
@GrpcClient("test-server")
|
||||||
|
private AuthServiceGrpc.AuthServiceBlockingStub grpcAuthService;
|
||||||
|
|
||||||
|
public boolean isLogin() {
|
||||||
|
Auth.GrpcBool resp = grpcAuthService.isLogin(Empty.getDefaultInstance());
|
||||||
|
return resp.getVal();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String login(Integer id) {
|
||||||
|
Auth.GrpcString resp = grpcAuthService.login(Auth.GrpcInt.newBuilder().setVal(id).build());
|
||||||
|
return resp.getVal();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
package auth;
|
||||||
|
|
||||||
|
option java_package = "com.lym.grpc.auth";
|
||||||
|
import "google/protobuf/empty.proto";
|
||||||
|
|
||||||
|
message GrpcBool{
|
||||||
|
bool val = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message GrpcInt{
|
||||||
|
int32 val = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message GrpcString{
|
||||||
|
string val = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
service AuthService{
|
||||||
|
rpc isLogin(google.protobuf.Empty) returns (GrpcBool);
|
||||||
|
rpc login(GrpcInt) returns (GrpcString);
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
server:
|
||||||
|
port: 2222
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: test-client
|
||||||
|
redis:
|
||||||
|
host: localhost
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
server-addr: localhost:8848
|
||||||
|
sa-token:
|
||||||
|
is-read-cookie: false
|
||||||
|
grpc:
|
||||||
|
server:
|
||||||
|
port: 2223
|
||||||
|
client:
|
||||||
|
test-server:
|
||||||
|
negotiation-type: PLAINTEXT
|
122
sa-token-demo/sa-token-demo-grpc/pom.xml
Normal file
122
sa-token-demo/sa-token-demo-grpc/pom.xml
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
<modules>
|
||||||
|
<module>client</module>
|
||||||
|
<module>server</module>
|
||||||
|
</modules>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.6.3</version>
|
||||||
|
<relativePath/>
|
||||||
|
</parent>
|
||||||
|
<groupId>com.lym</groupId>
|
||||||
|
<artifactId>sa-token-demo-grpc</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>sa-token-demo-grpc</name>
|
||||||
|
<description>sa-token-demo-grpc</description>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<lombok.version>1.18.10</lombok.version>
|
||||||
|
<sa-token-version>1.30.0</sa-token-version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||||
|
<version>2.7.2</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.dev33</groupId>
|
||||||
|
<artifactId>sa-token-spring-boot-starter</artifactId>
|
||||||
|
<version>${sa-token-version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.dev33</groupId>
|
||||||
|
<artifactId>sa-token-dao-redis-jackson</artifactId>
|
||||||
|
<version>${sa-token-version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.dev33</groupId>
|
||||||
|
<artifactId>sa-token-context-grpc</artifactId>
|
||||||
|
<version>${sa-token-version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||||
|
<version>2021.0.1.0</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-dependencies</artifactId>
|
||||||
|
<version>2021.0.1</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<extensions>
|
||||||
|
<extension>
|
||||||
|
<groupId>kr.motd.maven</groupId>
|
||||||
|
<artifactId>os-maven-plugin</artifactId>
|
||||||
|
</extension>
|
||||||
|
</extensions>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.xolstice.maven.plugins</groupId>
|
||||||
|
<artifactId>protobuf-maven-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<protocArtifact>com.google.protobuf:protoc:3.1.0:exe:${os.detected.classifier}</protocArtifact>
|
||||||
|
<pluginId>grpc-java</pluginId>
|
||||||
|
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.12.0:exe:${os.detected.classifier}</pluginArtifact>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>compile</goal>
|
||||||
|
<goal>compile-custom</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
20
sa-token-demo/sa-token-demo-grpc/server/pom.xml
Normal file
20
sa-token-demo/sa-token-demo-grpc/server/pom.xml
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>sa-token-demo-grpc</artifactId>
|
||||||
|
<groupId>com.lym</groupId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>server</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.lym;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lym
|
||||||
|
* @description
|
||||||
|
* @date 2022/8/26 10:58
|
||||||
|
**/
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableDiscoveryClient
|
||||||
|
public class Server {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(Server.class);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.lym.grpc.server;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
|
import com.google.protobuf.Empty;
|
||||||
|
import com.lym.grpc.auth.Auth;
|
||||||
|
import com.lym.grpc.auth.AuthServiceGrpc;
|
||||||
|
import com.lym.service.AuthService;
|
||||||
|
import io.grpc.stub.StreamObserver;
|
||||||
|
import net.devh.boot.grpc.server.service.GrpcService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lym
|
||||||
|
* @description
|
||||||
|
* @date 2022/8/26 11:29
|
||||||
|
**/
|
||||||
|
@GrpcService
|
||||||
|
public class GrpcAuthService extends AuthServiceGrpc.AuthServiceImplBase {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private AuthService authService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void isLogin(Empty request, StreamObserver<Auth.GrpcBool> responseObserver) {
|
||||||
|
boolean isLogin = authService.isLogin();
|
||||||
|
responseObserver.onNext(Auth.GrpcBool.newBuilder().setVal(isLogin).build());
|
||||||
|
responseObserver.onCompleted();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void login(Auth.GrpcInt request, StreamObserver<Auth.GrpcString> responseObserver) {
|
||||||
|
StpUtil.login(request.getVal());
|
||||||
|
Auth.GrpcString resp = Auth.GrpcString.newBuilder().setVal(StpUtil.getTokenValue()).build();
|
||||||
|
responseObserver.onNext(resp);
|
||||||
|
responseObserver.onCompleted();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.lym.service;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lym
|
||||||
|
* @description
|
||||||
|
* @date 2022/8/26 11:30
|
||||||
|
**/
|
||||||
|
@Service
|
||||||
|
public class AuthService {
|
||||||
|
public boolean isLogin() {
|
||||||
|
if (StpUtil.isLogin()) {
|
||||||
|
System.out.println("id:" + StpUtil.getLoginIdAsInt());
|
||||||
|
System.out.println("token:" + StpUtil.getTokenValue());
|
||||||
|
} else {
|
||||||
|
System.out.println("未登录");
|
||||||
|
}
|
||||||
|
|
||||||
|
return StpUtil.isLogin();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
package auth;
|
||||||
|
|
||||||
|
option java_package = "com.lym.grpc.auth";
|
||||||
|
import "google/protobuf/empty.proto";
|
||||||
|
|
||||||
|
message GrpcBool{
|
||||||
|
bool val = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message GrpcInt{
|
||||||
|
int32 val = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message GrpcString{
|
||||||
|
string val = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
service AuthService{
|
||||||
|
rpc isLogin(google.protobuf.Empty) returns (GrpcBool);
|
||||||
|
rpc login(GrpcInt) returns (GrpcString);
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
server:
|
||||||
|
port: 5555
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: test-server
|
||||||
|
redis:
|
||||||
|
host: localhost
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
server-addr: localhost:8848
|
||||||
|
sa-token:
|
||||||
|
is-read-cookie: false
|
||||||
|
|
||||||
|
grpc:
|
||||||
|
server:
|
||||||
|
port: 5556
|
39
sa-token-doc/doc/plugin/grpc-extend.md
Normal file
39
sa-token-doc/doc/plugin/grpc-extend.md
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
# 和 grpc 集成
|
||||||
|
|
||||||
|
本插件的作用是让 Sa-Token 和 grpc 做一个整合。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
和dubbo插件一样,解决了以下问题
|
||||||
|
|
||||||
|
1. 在 [ 被调用端 ] 安全的调用 Sa-Token 相关 API。
|
||||||
|
2. 在 [ 调用端 ] 登录的会话,其登录状态可以自动传递到 [ 被调用端 ] ;在 [ 被调用端 ] 登录的会话,其登录状态可以自动回传到 [ 调用端 ]
|
||||||
|
3. id-token 安全校验
|
||||||
|
|
||||||
|
---
|
||||||
|
和dubbo插件一样,具有以下限制:
|
||||||
|
|
||||||
|
1. [ 调用端 ] 与 [ 被调用端 ] 的 `SaStorage` 数据无法互通。
|
||||||
|
2. [ 被调用端 ] 执行的 `SaResponse.setHeader()`、`setStatus()` 等代码无效。
|
||||||
|
|
||||||
|
### 引入插件
|
||||||
|
需要springboot环境,添加依赖(调用端和被调用端都需要引入):
|
||||||
|
|
||||||
|
``` xml
|
||||||
|
<!-- Sa-Token 整合 grpc -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.dev33</groupId>
|
||||||
|
<artifactId>sa-token-context-grpc</artifactId>
|
||||||
|
<version>${sa.top.version}</version>
|
||||||
|
</dependency>
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
### 开启id-token校验:
|
||||||
|
直接在 `application.yml` 配置即可:
|
||||||
|
|
||||||
|
``` yml
|
||||||
|
sa-token:
|
||||||
|
# 打开 RPC 调用鉴权
|
||||||
|
check-id-token: true
|
||||||
|
```
|
@ -31,6 +31,7 @@
|
|||||||
<module>sa-token-temp-jwt</module>
|
<module>sa-token-temp-jwt</module>
|
||||||
<module>sa-token-jwt</module>
|
<module>sa-token-jwt</module>
|
||||||
<module>sa-token-context-dubbo</module>
|
<module>sa-token-context-dubbo</module>
|
||||||
|
<module>sa-token-context-grpc</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
35
sa-token-plugin/sa-token-context-grpc/pom.xml
Normal file
35
sa-token-plugin/sa-token-context-grpc/pom.xml
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>sa-token-plugin</artifactId>
|
||||||
|
<groupId>cn.dev33</groupId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<artifactId>sa-token-context-grpc</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.devh</groupId>
|
||||||
|
<artifactId>grpc-spring-boot-starter</artifactId>
|
||||||
|
<version>2.10.1.RELEASE</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.dev33</groupId>
|
||||||
|
<artifactId>sa-token-core</artifactId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
@ -0,0 +1,20 @@
|
|||||||
|
package cn.dev33.satoken.context.grpc;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.context.second.SaTokenSecondContext;
|
||||||
|
import cn.dev33.satoken.context.second.SaTokenSecondContextCreator;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sa-Token 二级Context - 创建器 [Grpc版]
|
||||||
|
*
|
||||||
|
* @author lym
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class SaTokenSecondContextCreatorForGrpc implements SaTokenSecondContextCreator {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SaTokenSecondContext create() {
|
||||||
|
return new SaTokenSecondContextForGrpc();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
package cn.dev33.satoken.context.grpc;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.context.grpc.context.SaTokenGrpcContext;
|
||||||
|
import cn.dev33.satoken.context.grpc.model.SaRequestForGrpc;
|
||||||
|
import cn.dev33.satoken.context.grpc.model.SaResponseForGrpc;
|
||||||
|
import cn.dev33.satoken.context.grpc.model.SaStorageForGrpc;
|
||||||
|
import cn.dev33.satoken.context.model.SaRequest;
|
||||||
|
import cn.dev33.satoken.context.model.SaResponse;
|
||||||
|
import cn.dev33.satoken.context.model.SaStorage;
|
||||||
|
import cn.dev33.satoken.context.second.SaTokenSecondContext;
|
||||||
|
import cn.dev33.satoken.exception.ApiDisabledException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sa-Token 上下文 [grpc版本]
|
||||||
|
*
|
||||||
|
* @author lym
|
||||||
|
*/
|
||||||
|
public class SaTokenSecondContextForGrpc implements SaTokenSecondContext {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SaRequest getRequest() {
|
||||||
|
return new SaRequestForGrpc();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SaResponse getResponse() {
|
||||||
|
return new SaResponseForGrpc();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SaStorage getStorage() {
|
||||||
|
return new SaStorageForGrpc();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean matchPath(String pattern, String path) {
|
||||||
|
throw new ApiDisabledException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isValid() {
|
||||||
|
return SaTokenGrpcContext.isNotNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package cn.dev33.satoken.context.grpc.constants;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.id.SaIdUtil;
|
||||||
|
import cn.dev33.satoken.util.SaTokenConsts;
|
||||||
|
import io.grpc.Metadata;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lym
|
||||||
|
* @description
|
||||||
|
* @date 2022/8/26 14:27
|
||||||
|
**/
|
||||||
|
public class GrpcContextConstants {
|
||||||
|
public static final Metadata.Key<String> SA_ID_TOKEN =
|
||||||
|
Metadata.Key.of(SaIdUtil.ID_TOKEN, Metadata.ASCII_STRING_MARSHALLER);
|
||||||
|
|
||||||
|
public static final Metadata.Key<String> SA_JUST_CREATED_NOT_PREFIX =
|
||||||
|
Metadata.Key.of(SaTokenConsts.JUST_CREATED_NOT_PREFIX, Metadata.ASCII_STRING_MARSHALLER);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package cn.dev33.satoken.context.grpc.context;
|
||||||
|
|
||||||
|
import io.grpc.*;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lym
|
||||||
|
* @date 2022/8/25 11:02
|
||||||
|
**/
|
||||||
|
public class SaTokenGrpcContext {
|
||||||
|
/**
|
||||||
|
* grpc请求上下文。请求完成后会由grpc自动清空
|
||||||
|
*
|
||||||
|
* @see Contexts#interceptCall(Context, ServerCall, Metadata, ServerCallHandler)
|
||||||
|
*/
|
||||||
|
private static final Context.Key<Map<String, Object>> SA_TOKEN_CONTEXT_KEY =
|
||||||
|
Context.key("sa-token-context");
|
||||||
|
|
||||||
|
public static Object get(String key) {
|
||||||
|
return SA_TOKEN_CONTEXT_KEY.get().get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void set(String key, Object value) {
|
||||||
|
SA_TOKEN_CONTEXT_KEY.get().put(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void removeKey(String key) {
|
||||||
|
SA_TOKEN_CONTEXT_KEY.get().remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, Object> getContext() {
|
||||||
|
return SA_TOKEN_CONTEXT_KEY.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isNotNull() {
|
||||||
|
return SA_TOKEN_CONTEXT_KEY.get() != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Context create() {
|
||||||
|
return Context.current().withValue(SaTokenGrpcContext.SA_TOKEN_CONTEXT_KEY, new HashMap<>());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package cn.dev33.satoken.context.grpc.interceptor;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.context.grpc.context.SaTokenGrpcContext;
|
||||||
|
import io.grpc.*;
|
||||||
|
import net.devh.boot.grpc.server.interceptor.GrpcGlobalServerInterceptor;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lym
|
||||||
|
* @date 2022/8/24 10:09
|
||||||
|
* @description 处理请求前,创建上下文
|
||||||
|
*/
|
||||||
|
@GrpcGlobalServerInterceptor
|
||||||
|
public class SaTokenContextGrpcServerInterceptor implements ServerInterceptor, Ordered {
|
||||||
|
@Override
|
||||||
|
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
|
||||||
|
Context ctx = SaTokenGrpcContext.create();
|
||||||
|
return Contexts.interceptCall(ctx, call, headers, next);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 必须最先创建上下文,后面的拦截器才能获取到上下文
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return HIGHEST_PRECEDENCE;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,59 @@
|
|||||||
|
package cn.dev33.satoken.context.grpc.interceptor;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.SaManager;
|
||||||
|
import cn.dev33.satoken.context.SaTokenContextDefaultImpl;
|
||||||
|
import cn.dev33.satoken.context.grpc.constants.GrpcContextConstants;
|
||||||
|
import cn.dev33.satoken.id.SaIdUtil;
|
||||||
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
|
import cn.dev33.satoken.util.SaFoxUtil;
|
||||||
|
import io.grpc.*;
|
||||||
|
import net.devh.boot.grpc.client.interceptor.GrpcGlobalClientInterceptor;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lym
|
||||||
|
* @date 2022/8/24 15:45
|
||||||
|
* @description 客户端请求的时候,带上token
|
||||||
|
*/
|
||||||
|
@GrpcGlobalClientInterceptor
|
||||||
|
public class SaTokenGrpcClientInterceptor implements ClientInterceptor, Ordered {
|
||||||
|
@Override
|
||||||
|
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method,
|
||||||
|
CallOptions callOptions, Channel next) {
|
||||||
|
return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
|
||||||
|
@Override
|
||||||
|
public void start(Listener<RespT> responseListener, Metadata headers) {
|
||||||
|
|
||||||
|
// 追加 Id-Token 参数
|
||||||
|
if (SaManager.getConfig().getCheckIdToken()) {
|
||||||
|
headers.put(GrpcContextConstants.SA_ID_TOKEN, SaIdUtil.getToken());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 调用前,传递会话Token
|
||||||
|
String tokenValue = StpUtil.getTokenValue();
|
||||||
|
if (SaFoxUtil.isNotEmpty(tokenValue)
|
||||||
|
&& SaManager.getSaTokenContextOrSecond() != SaTokenContextDefaultImpl.defaultContext) {
|
||||||
|
headers.put(GrpcContextConstants.SA_JUST_CREATED_NOT_PREFIX, tokenValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
super.start(new ForwardingClientCallListener.SimpleForwardingClientCallListener<RespT>(responseListener) {
|
||||||
|
/**
|
||||||
|
* 服务端结束响应后,解析回传的Token值
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void onClose(Status status, Metadata responseHeader) {
|
||||||
|
StpUtil.setTokenValue(responseHeader.get(GrpcContextConstants.SA_JUST_CREATED_NOT_PREFIX));
|
||||||
|
super.onClose(status, responseHeader);
|
||||||
|
}
|
||||||
|
}, headers);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return HIGHEST_PRECEDENCE;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
package cn.dev33.satoken.context.grpc.interceptor;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.SaManager;
|
||||||
|
import cn.dev33.satoken.context.grpc.constants.GrpcContextConstants;
|
||||||
|
import cn.dev33.satoken.id.SaIdUtil;
|
||||||
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
|
import cn.dev33.satoken.util.SaFoxUtil;
|
||||||
|
import io.grpc.*;
|
||||||
|
import net.devh.boot.grpc.server.interceptor.GrpcGlobalServerInterceptor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lym
|
||||||
|
* @description 鉴权,设置token
|
||||||
|
* @date 2022/8/25 11:33
|
||||||
|
**/
|
||||||
|
@GrpcGlobalServerInterceptor
|
||||||
|
public class SaTokenGrpcServerInterceptor implements ServerInterceptor {
|
||||||
|
@Override
|
||||||
|
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
|
||||||
|
// RPC 调用鉴权
|
||||||
|
if (SaManager.getConfig().getCheckIdToken()) {
|
||||||
|
String idToken = headers.get(GrpcContextConstants.SA_ID_TOKEN);
|
||||||
|
SaIdUtil.checkToken(idToken);
|
||||||
|
}
|
||||||
|
String tokenFromClient = headers.get(GrpcContextConstants.SA_JUST_CREATED_NOT_PREFIX);
|
||||||
|
StpUtil.setTokenValue(tokenFromClient);
|
||||||
|
|
||||||
|
return next.startCall(new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {
|
||||||
|
/**
|
||||||
|
* 结束响应时,若本服务生成了新token,将其传回客户端
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void close(Status status, Metadata responseHeaders) {
|
||||||
|
String justCreateToken = StpUtil.getTokenValue();
|
||||||
|
if (!SaFoxUtil.equals(justCreateToken, tokenFromClient) && SaFoxUtil.isNotEmpty(justCreateToken)) {
|
||||||
|
responseHeaders.put(GrpcContextConstants.SA_JUST_CREATED_NOT_PREFIX, justCreateToken);
|
||||||
|
}
|
||||||
|
super.close(status, responseHeaders);
|
||||||
|
}
|
||||||
|
}, headers);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,85 @@
|
|||||||
|
package cn.dev33.satoken.context.grpc.model;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.context.grpc.context.SaTokenGrpcContext;
|
||||||
|
import cn.dev33.satoken.context.model.SaRequest;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Request for grpc
|
||||||
|
*
|
||||||
|
* @author lym
|
||||||
|
*/
|
||||||
|
public class SaRequestForGrpc implements SaRequest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取底层源对象
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Object getSource() {
|
||||||
|
return SaTokenGrpcContext.getContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在 [请求体] 里获取一个值
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String getParam(String name) {
|
||||||
|
// 不传播 url 参数
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在 [请求头] 里获取一个值
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String getHeader(String name) {
|
||||||
|
// 不传播 header 参数
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在 [Cookie作用域] 里获取一个值
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String getCookieValue(String name) {
|
||||||
|
// 不传播 cookie 参数
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回当前请求path (不包括上下文名称)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String getRequestPath() {
|
||||||
|
// 不传播 requestPath
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回当前请求的url,例:http://xxx.com/test
|
||||||
|
*
|
||||||
|
* @return see note
|
||||||
|
*/
|
||||||
|
public String getUrl() {
|
||||||
|
// 不传播 url
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回当前请求的类型
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String getMethod() {
|
||||||
|
// 不传播 method
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 转发请求
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Object forward(String path) {
|
||||||
|
// 不传播 forward 动作
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
package cn.dev33.satoken.context.grpc.model;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.context.grpc.context.SaTokenGrpcContext;
|
||||||
|
import cn.dev33.satoken.context.model.SaResponse;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Response for grpc
|
||||||
|
*
|
||||||
|
* @author lym
|
||||||
|
*/
|
||||||
|
public class SaResponseForGrpc implements SaResponse {
|
||||||
|
/**
|
||||||
|
* 获取底层源对象
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Object getSource() {
|
||||||
|
return SaTokenGrpcContext.getContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置响应状态码
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public SaResponse setStatus(int sc) {
|
||||||
|
// 不回传 status 状态
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在响应头里写入一个值
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public SaResponse setHeader(String name, String value) {
|
||||||
|
// 不回传 header响应头
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在响应头里添加一个值
|
||||||
|
*
|
||||||
|
* @param name 名字
|
||||||
|
* @param value 值
|
||||||
|
* @return 对象自身
|
||||||
|
*/
|
||||||
|
public SaResponse addHeader(String name, String value) {
|
||||||
|
// 不回传 header响应头
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重定向
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Object redirect(String url) {
|
||||||
|
// 不回传 重定向 动作
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
package cn.dev33.satoken.context.grpc.model;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.context.grpc.context.SaTokenGrpcContext;
|
||||||
|
import cn.dev33.satoken.context.model.SaStorage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Storage for grpc
|
||||||
|
*
|
||||||
|
* @author lym
|
||||||
|
*/
|
||||||
|
public class SaStorageForGrpc implements SaStorage {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取底层源对象
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Object getSource() {
|
||||||
|
return SaTokenGrpcContext.getContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在 [Request作用域] 里写入一个值
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public SaStorage set(String key, Object value) {
|
||||||
|
SaTokenGrpcContext.set(key, value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在 [Request作用域] 里获取一个值
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Object get(String key) {
|
||||||
|
return SaTokenGrpcContext.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在 [Request作用域] 里删除一个值
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public SaStorage delete(String key) {
|
||||||
|
SaTokenGrpcContext.removeKey(key);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||||
|
cn.dev33.satoken.context.grpc.interceptor.SaTokenGrpcClientInterceptor,\
|
||||||
|
cn.dev33.satoken.context.grpc.interceptor.SaTokenContextGrpcServerInterceptor,\
|
||||||
|
cn.dev33.satoken.context.grpc.interceptor.SaTokenGrpcServerInterceptor,\
|
||||||
|
cn.dev33.satoken.context.grpc.SaTokenSecondContextCreatorForGrpc
|
Loading…
Reference in New Issue
Block a user