Skip to content

Commit

Permalink
Merge pull request #8 from admin4j/dev
Browse files Browse the repository at this point in the history
v0.9.5
  • Loading branch information
andanyoung authored Dec 20, 2023
2 parents 880b8cd + 98a83a2 commit 75e28a8
Show file tree
Hide file tree
Showing 90 changed files with 3,225 additions and 743 deletions.
22 changes: 20 additions & 2 deletions admin4j-common-spring-web/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?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"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
Expand All @@ -11,6 +11,7 @@

<groupId>com.admin4j.common</groupId>
<artifactId>admin4j-common-spring-web</artifactId>
<version>0.9.5</version>
<description>与业务无关的工具类库</description>

<properties>
Expand All @@ -20,9 +21,16 @@
</properties>

<dependencies>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
Expand Down Expand Up @@ -71,5 +79,15 @@
<artifactId>hibernate-validator</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure-processor</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.admin4j.common.config;

import com.admin4j.common.constant.WebConstant;
import com.admin4j.common.service.IUserContextHolder;
import com.admin4j.common.service.impl.SimpleUserContextHolder;
import com.admin4j.common.util.UserContextUtil;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* @author andanyang
* @since 2023/9/15 9:19
*/
@Configuration
@AutoConfigureOrder(WebConstant.IUserContextHolderOrder + 6)
public class UserContextAutoConfiguration implements InitializingBean, ApplicationContextAware {

private ApplicationContext applicationContext;

@Bean
@ConditionalOnMissingBean(IUserContextHolder.class)
@ConditionalOnClass(name = "com.alibaba.ttl.TransmittableThreadLocal")
public IUserContextHolder userContextHolder() {
return new SimpleUserContextHolder();
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}

@Override
public void afterPropertiesSet() throws Exception {
UserContextUtil.userContextHolder = applicationContext.getBean(IUserContextHolder.class);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.admin4j.common.constant;

/**
* @author andanyang
* @since 2023/12/1 11:18
*/
public final class WebConstant {

/**
* IUserContextHolder 接口再spring中初始化顺序
*/
public static final int IUserContextHolderOrder = -1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import lombok.Data;

import java.io.Serializable;
import java.util.Set;
import java.util.Collection;

/**
* UserContext 用户上下文
Expand Down Expand Up @@ -35,7 +35,7 @@ public class AuthenticationUser implements Serializable {
* 权限列表
*/
@ApiModelProperty("权限code列表")
private Set<String> permissions;
private Collection<String> permissions;

// private String fromService;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.admin4j.framework.web;
package com.admin4j.common.service.impl;

import com.admin4j.common.pojo.AuthenticationUser;
import com.admin4j.common.pojo.ResponseEnum;
Expand All @@ -13,7 +13,6 @@
* @author andanyang
* @since 2021/7/27 10:56
*/

public class SimpleUserContextHolder implements IUserContextHolder {

/**
Expand All @@ -29,6 +28,11 @@ public void loginOut() {
clear();
}

@Override
public AuthenticationUser getAuthenticationUser() {
return THREAD_LOCAL_USER.get();
}

/**
* 设置登录者信息
*
Expand All @@ -39,11 +43,6 @@ public void setAuthenticationUser(AuthenticationUser authenticationUser) {
THREAD_LOCAL_USER.set(authenticationUser);
}

@Override
public AuthenticationUser getAuthenticationUser() {
return THREAD_LOCAL_USER.get();
}

/**
* 获取用户
*
Expand Down Expand Up @@ -90,6 +89,20 @@ public void offTenant() {
setTenantId(0L);
}

/**
* get租户
*/
@Override
public Long getTenantId() {
AuthenticationUser loginUserNoCheck = getLoginUserNoCheck();
// 小心三目表达式,NPE
if (loginUserNoCheck == null) {
return null;
} else {
return loginUserNoCheck.getTenantId();
}
}

/**
* 设置租户
*
Expand All @@ -101,12 +114,11 @@ public void setTenantId(Long tenant) {
}

/**
* get租户
* get用户ID
*/
@Override
public Long getTenantId() {
AuthenticationUser loginUserNoCheck = getLoginUserNoCheck();
return loginUserNoCheck == null ? 0L : loginUserNoCheck.getTenantId();
public Long getUserId() {
return getLoginUser().getUserId();
}

/**
Expand All @@ -118,12 +130,4 @@ public Long getTenantId() {
public void setUserId(Long userId) {
getLoginUser().setUserId(userId);
}

/**
* get用户ID
*/
@Override
public Long getUserId() {
return getLoginUser().getUserId();
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.admin4j.common.config.UserContextConfig
com.admin4j.common.config.UserContextAutoConfiguration
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,34 @@
*/
public class SpelUtil {

private SpelUtil() {
}

/**
* 是一种基于本地变量表的参数解析器,依赖于编译时的选项,可能无法在所有情况下获取参数名称
*/
private static final LocalVariableTableParameterNameDiscoverer U = new LocalVariableTableParameterNameDiscoverer();
//使用SPEL进行key的解析
// 使用SPEL进行key的解析
private static final ExpressionParser EL_PARSER = new SpelExpressionParser();

private SpelUtil() {
}

/**
* el表达式解析
*
* @param spel 解析值
* @param method 方法
* @param args 参数
* @return
*/
public static String parse(String spel, Method method, Object[] args) {
//获取被拦截方法参数名列表(使用Spring支持类库)
if (StringUtils.isBlank(spel)) {
return null;
}
// 获取被拦截方法参数名列表(使用Spring支持类库)
String[] paraNameArr = U.getParameterNames(method);

//SPEL上下文
// SPEL上下文
StandardEvaluationContext context = new StandardEvaluationContext();
//把方法参数放入SPEL上下文中
// 把方法参数放入SPEL上下文中
if (paraNameArr != null) {
for (int i = 0; i < paraNameArr.length; i++) {
context.setVariable(paraNameArr[i], args[i]);
Expand Down Expand Up @@ -63,12 +76,12 @@ public static String parse(Object rootObject, String spel, Method method, Object
} else if (!spel.contains("#")) {
return parse(spel, method, args);
}
//获取被拦截方法参数名列表(使用Spring支持类库)
// 获取被拦截方法参数名列表(使用Spring支持类库)
String[] paraNameArr = U.getParameterNames(method);

//SPEL上下文
// SPEL上下文
StandardEvaluationContext context = new MethodBasedEvaluationContext(rootObject, method, args, U);
//把方法参数放入SPEL上下文中
// 把方法参数放入SPEL上下文中
if (paraNameArr != null) {
for (int i = 0; i < paraNameArr.length; i++) {
context.setVariable(paraNameArr[i], args[i]);
Expand Down
7 changes: 6 additions & 1 deletion admin4j-common/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
Expand All @@ -23,6 +23,11 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>

<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ public class EncryptException extends SystemException {
public EncryptException(String message, Throwable throwable) {
super(message, throwable);
}

public EncryptException(Throwable throwable) {
super(throwable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,11 @@ public enum ResponseEnum implements IResponse, Assert {
*/
FAIL_AUTH(402, "登录失败,账号或者密码错误"),
/**
*
* 没有权限
*/
FAIL_AUTH_FORBIDDEN(403, "FAIL_AUTH_FORBIDDEN"),


/**
* token 认证失败
*/
FAIL_NO_TOKEN(405, "FAIL_NO_TOKEN"),


/**
* 服务未找到
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// package com.admin4j.common.crypto;
//
// import org.junit.Before;
// import org.junit.Test;
//
// import java.security.NoSuchAlgorithmException;
// import java.util.Base64;
//
// /**
// * @author andanyang
// * @since 2023/11/27 16:26
// */
// public class RSAUtilTest {
// private static final String PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqLh8XuAeLvjCly5hesbWtjanpgjVOwNGMPX8lx2937pdP48KlaacrW576lconObZahf60CuNohFTyZrRf9y770tXrC1NF2iDAxZpFjsZChlbftwmk94Okvq/9AkJxyprXZniLZ7fU9gI77sVsKx1wASpaAsLFwSu/y4pgsMa+hESr3r0mRZ1hoVSvKr53EbJfqgtoeeqRYzTZJzbpwKyPhrP2fEIG/9NGgiehp0vIzyFtKET9FcLZZzPi0nA6pVIohZUMS1t/qFdwWuebjBDqfH86ZapqUBYqhWBMr8xBI4vYYH3y02eJwYILpKMlJlEXH55C360+g+Zxg8A9mLZ9QIDAQAB";
// private static final String PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCouHxe4B4u+MKXLmF6xta2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zLs8PbRGirMNp1l/c6BqWhk6YRjYm6xKGQ2klQinu1SUV3zdTIpUniwtWLdxZf29Jw0P4AT8RcJC3dlrbtFhm+WxLHr1ZDA7VtyZrJjTka/fQZqrLR1FbzMBd2jpBPuv2oFtEM/NKwKBgArQTaXxo9ZPTU8Kr22v+7FE8OyOo5T7ThVfLKmnBVq4K6n/5emERWQ/CI25KEJjpDVYCHCGYM7jTr2kPXrElYt9V2NfJl4N4tlROwCYbKzhD+Fdso9JRA8acXl3W9xE7euzOchg1eMRFouoii6kXNbxfNGq+45GTgzjnvVYpPt5AoGAZ1fE7Dj+y5bhyIl0mQq7DV3Zeoi97E2h7a8Z2SAvQjgk9EPnU4oQLbOdSwsxVSC/jivfFY82tckMcirBxQjz8VAzFy/DNgnlWyN76WeCZgzgyTuOPnRjOCPvvPMWDApSzUY9UwxAY4eqvCaziRMFEMRI+M1Nr0+ICYGC0YuSvec=";
// static RSAUtil.RSAKeyPair rsaKeyPair;
//
// @Before
// public void init() throws NoSuchAlgorithmException {
// rsaKeyPair = RSAUtil.generateKeyPair(2048);
// System.out.println("getPublicKey = " + rsaKeyPair.getPublicKey());
// System.out.println("getPrivateKey = " + rsaKeyPair.getPrivateKey());
// }
//
// @Test
// public void testEncrypt() {
//
// // RSAUtil.
// String data = "bec36c76-d315-40bd-af6d-cf32feee49ae-221";
// byte[] keyBytes = PRIVATE_KEY.getBytes();
//
// byte[] encrypt = RSAUtil.encrypt(data.getBytes(), RSAUtil.getPrivateKey(keyBytes));
// System.out.println("encrypt = " + Base64.getEncoder().encodeToString(encrypt));
//
// // // // TODO TEST
// // String decrypt = RSAUtil.decrypt(encrypt, PUBLIC_KEY);
// // System.out.println("decrypt = " + decrypt);
// // assert encrypt.equals(decrypt);
// }
//
// public void testDecrypt() {
// }
//
//
// }
Loading

0 comments on commit 75e28a8

Please sign in to comment.