feat:jwt授权

This commit is contained in:
xiang
2025-08-24 01:04:56 +08:00
parent 346bd89c72
commit c3ef7a4fe6
4 changed files with 54 additions and 5 deletions

11
pom.xml
View File

@@ -75,11 +75,16 @@
<version>${spring.authorization.server.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
<!-- JWT (optional: for convenience) -->
<dependency>
<groupId>com.nimbusds</groupId>
<artifactId>nimbus-jose-jwt</artifactId>
<version>9.24.4</version>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-jose</artifactId>
<version>5.7.11</version>
</dependency>
<!-- MySQL -->
<dependency>

View File

@@ -6,9 +6,9 @@
<parent>
<groupId>com.xiang</groupId>
<artifactId>xservice-basic</artifactId>
<version>1.1</version>
<version>2.0-SNAPSHOT</version>
</parent>
<version>1.3</version>
<version>2.0-SNAPSHOT</version>
<artifactId>xservice-common</artifactId>

View File

@@ -0,0 +1,35 @@
package com.xiang.xservice.basic.utils;
import com.nimbusds.jose.jwk.RSAKey;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.UUID;
public class JwkUtils {
public static RSAKey generateRsa() {
KeyPair keyPair = generateRsaKey();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
return new RSAKey.Builder(publicKey)
.privateKey(privateKey)
.keyID(UUID.randomUUID().toString())
.build();
}
static KeyPair generateRsaKey() {
KeyPair keyPair;
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
keyPair = keyPairGenerator.generateKeyPair();
} catch (Exception ex) {
throw new IllegalStateException(ex);
}
return keyPair;
}
}

View File

@@ -4,9 +4,12 @@ import com.xiang.xservice.basic.utils.SSHManager;
import com.xiang.xservice.mysql.config.DynamicDataSourceContext;
import com.xiang.xservice.mysql.entity.DataSourceProperty;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import com.xiang.xservice.mysql.entity.DynamicDataSourceProperties;
import java.util.Objects;
@RequiredArgsConstructor
public class DynamicRoutingDataSource extends AbstractRoutingDataSource {
@@ -15,7 +18,13 @@ public class DynamicRoutingDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
String key = DynamicDataSourceContext.get();
if (StringUtils.isEmpty(key)) {
key = dynamicDataSourceProperties.getPrimary();
}
DataSourceProperty dataSourceProperty = dynamicDataSourceProperties.getDatasource().get(key);
if (Objects.isNull(dataSourceProperty.getSshConnect())) {
return key;
}
if (Boolean.TRUE.equals(dataSourceProperty.getSshConnect())) {
try {
SSHManager.createTunnel(