feat:jwt授权
This commit is contained in:
11
pom.xml
11
pom.xml
@@ -75,11 +75,16 @@
|
|||||||
<version>${spring.authorization.server.version}</version>
|
<version>${spring.authorization.server.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- JWT (optional: for convenience) -->
|
<!-- JWT (optional: for convenience) -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.nimbusds</groupId>
|
<groupId>org.springframework.security</groupId>
|
||||||
<artifactId>nimbus-jose-jwt</artifactId>
|
<artifactId>spring-security-oauth2-jose</artifactId>
|
||||||
<version>9.24.4</version>
|
<version>5.7.11</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- MySQL -->
|
<!-- MySQL -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|||||||
@@ -6,9 +6,9 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.xiang</groupId>
|
<groupId>com.xiang</groupId>
|
||||||
<artifactId>xservice-basic</artifactId>
|
<artifactId>xservice-basic</artifactId>
|
||||||
<version>1.1</version>
|
<version>2.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<version>1.3</version>
|
<version>2.0-SNAPSHOT</version>
|
||||||
|
|
||||||
<artifactId>xservice-common</artifactId>
|
<artifactId>xservice-common</artifactId>
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,9 +4,12 @@ import com.xiang.xservice.basic.utils.SSHManager;
|
|||||||
import com.xiang.xservice.mysql.config.DynamicDataSourceContext;
|
import com.xiang.xservice.mysql.config.DynamicDataSourceContext;
|
||||||
import com.xiang.xservice.mysql.entity.DataSourceProperty;
|
import com.xiang.xservice.mysql.entity.DataSourceProperty;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
|
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
|
||||||
import com.xiang.xservice.mysql.entity.DynamicDataSourceProperties;
|
import com.xiang.xservice.mysql.entity.DynamicDataSourceProperties;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class DynamicRoutingDataSource extends AbstractRoutingDataSource {
|
public class DynamicRoutingDataSource extends AbstractRoutingDataSource {
|
||||||
|
|
||||||
@@ -15,7 +18,13 @@ public class DynamicRoutingDataSource extends AbstractRoutingDataSource {
|
|||||||
@Override
|
@Override
|
||||||
protected Object determineCurrentLookupKey() {
|
protected Object determineCurrentLookupKey() {
|
||||||
String key = DynamicDataSourceContext.get();
|
String key = DynamicDataSourceContext.get();
|
||||||
|
if (StringUtils.isEmpty(key)) {
|
||||||
|
key = dynamicDataSourceProperties.getPrimary();
|
||||||
|
}
|
||||||
DataSourceProperty dataSourceProperty = dynamicDataSourceProperties.getDatasource().get(key);
|
DataSourceProperty dataSourceProperty = dynamicDataSourceProperties.getDatasource().get(key);
|
||||||
|
if (Objects.isNull(dataSourceProperty.getSshConnect())) {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
if (Boolean.TRUE.equals(dataSourceProperty.getSshConnect())) {
|
if (Boolean.TRUE.equals(dataSourceProperty.getSshConnect())) {
|
||||||
try {
|
try {
|
||||||
SSHManager.createTunnel(
|
SSHManager.createTunnel(
|
||||||
|
|||||||
Reference in New Issue
Block a user