userinfo自动装配
This commit is contained in:
parent
e5ccba7383
commit
49017dbb99
33
auth-starter/.gitignore
vendored
Normal file
33
auth-starter/.gitignore
vendored
Normal file
@ -0,0 +1,33 @@
|
||||
HELP.md
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
BIN
auth-starter/.mvn/wrapper/maven-wrapper.jar
vendored
Normal file
BIN
auth-starter/.mvn/wrapper/maven-wrapper.jar
vendored
Normal file
Binary file not shown.
2
auth-starter/.mvn/wrapper/maven-wrapper.properties
vendored
Normal file
2
auth-starter/.mvn/wrapper/maven-wrapper.properties
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.4/apache-maven-3.8.4-bin.zip
|
||||
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar
|
||||
109
auth-starter/pom.xml
Normal file
109
auth-starter/pom.xml
Normal file
@ -0,0 +1,109 @@
|
||||
<?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>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.6.6</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>cn.axzo.framework</groupId>
|
||||
<artifactId>auth-starter</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<name>auth-starter</name>
|
||||
<description>auth-starter</description>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<javax.servlet.version>4.0.1</javax.servlet.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>5.7.22</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!--lombok-->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.18</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- 获取request和response-->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>5.1.5.RELEASE</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>${javax.servlet.version}</version>
|
||||
<scope>provided</scope>
|
||||
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>5.0.5.RELEASE</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- aop的一个依赖-->
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjrt</artifactId>
|
||||
<version>1.7.3</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>2.0.0-alpha1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-sources</id>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<distributionManagement>
|
||||
<!-- 两个ID必须与 setting.xml中的<server><id>Releases</id></server>保持一致 -->
|
||||
<repository>
|
||||
<id>rdc-releases</id>
|
||||
<name>Nexus Release Repository</name>
|
||||
<url>https://packages.aliyun.com/maven/repository/2005773-release-XI7cl5/</url>
|
||||
</repository>
|
||||
<snapshotRepository>
|
||||
<id>rdc-snapshots</id>
|
||||
<name>Nexus Snapshot Repository</name>
|
||||
<url>https://packages.aliyun.com/maven/repository/2005773-snapshot-V5Gjdf/</url>
|
||||
</snapshotRepository>
|
||||
</distributionManagement>
|
||||
|
||||
</project>
|
||||
@ -0,0 +1,13 @@
|
||||
package cn.axzo.framework.auth;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class AuthStarterApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AuthStarterApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
package cn.axzo.framework.header.annotation;
|
||||
package cn.axzo.framework.auth.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@ -6,6 +6,6 @@ import java.lang.annotation.*;
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface PreBuildUser {
|
||||
//暂时还不知道 怎么用
|
||||
//todo default
|
||||
String value();
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
package cn.axzo.framework.auth.config;
|
||||
import cn.axzo.framework.auth.service.BuilderUserInfoAspect;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class UserInfoConfiguration {
|
||||
|
||||
@Bean
|
||||
public BuilderUserInfoAspect builderUserInfoAspect(){
|
||||
return new BuilderUserInfoAspect();
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
package cn.axzo.framework.header.constants;
|
||||
package cn.axzo.framework.auth.constants;
|
||||
|
||||
public class AopConstants {
|
||||
public static final String HEADER_TENANT = "tenantId";
|
||||
@ -1,9 +1,10 @@
|
||||
package cn.axzo.framework.header.domain;
|
||||
package cn.axzo.framework.auth.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
package cn.axzo.framework.header.configuration;
|
||||
package cn.axzo.framework.auth.service;
|
||||
|
||||
import cn.axzo.framework.header.annotation.PreBuildUser;
|
||||
import cn.axzo.framework.header.constants.AopConstants;
|
||||
import cn.axzo.framework.header.domain.UserInfo;
|
||||
import cn.axzo.framework.auth.annotation.PreBuildUser;
|
||||
import cn.axzo.framework.auth.constants.AopConstants;
|
||||
import cn.axzo.framework.auth.domain.UserInfo;
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
@ -20,58 +20,53 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* 构建切面,在进入controller之前 进行User用户构建,
|
||||
* 使用方法 直接方法上开启注解,入参带上userinfo 或者其<子类?>
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@Aspect
|
||||
@Component
|
||||
@Slf4j
|
||||
public class UserInfoAspect {
|
||||
|
||||
public class BuilderUserInfoAspect {
|
||||
|
||||
@Around(value = "@annotation(preBuildUser)")
|
||||
public Object methodHandler(ProceedingJoinPoint pjp, PreBuildUser preBuildUser) throws Throwable {
|
||||
HttpServletRequest httpRequest =null ;
|
||||
HttpServletRequest httpRequest = null;
|
||||
try {
|
||||
//获取request
|
||||
httpRequest = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
|
||||
} catch (Exception exception){
|
||||
log.error("can not get request,there is a error occurrence");
|
||||
} catch (Exception exception) {
|
||||
// log.error("can not get request,there is a error occurrence"+ exception.printStackTrace(););
|
||||
}
|
||||
//todo request 为null
|
||||
assert httpRequest != null;
|
||||
this.fillInUserInfoDetail(httpRequest,pjp);
|
||||
this.fillInUserInfoDetail(httpRequest, pjp);
|
||||
return pjp.proceed();
|
||||
}
|
||||
|
||||
public void fillInUserInfoDetail(HttpServletRequest request,ProceedingJoinPoint pjp){
|
||||
public void fillInUserInfoDetail(HttpServletRequest request, ProceedingJoinPoint pjp) {
|
||||
String userJsonInfo = request.getHeader(AopConstants.USER_INFO);
|
||||
if(StringUtils.isEmpty(userJsonInfo)){
|
||||
//没有拿到用户信息这个构建也没有意义了
|
||||
return ;
|
||||
if (StringUtils.isEmpty(userJsonInfo)) {
|
||||
//没有拿到用户信息这个构建也没有意义了 //todo 做个异常处理
|
||||
return;
|
||||
}
|
||||
//转编码
|
||||
String userInfo = Base64.decodeStr(userJsonInfo);
|
||||
|
||||
//请求头获取数据
|
||||
String tenantId =request.getHeader(AopConstants.HEADER_TENANT);
|
||||
String systemType =request.getHeader(AopConstants.HEADER_SYSTEM_TYPE);
|
||||
String authorization =request.getHeader(AopConstants.HEADER_AUTH);
|
||||
String deviceKind =request.getHeader(AopConstants.HAEDER_DEVICE_KIND);
|
||||
String deviceNo =request.getHeader(AopConstants.HAEDER_DEVICE_NO);
|
||||
String appVersion =request.getHeader(AopConstants.HAEDER_APP_VERSION);
|
||||
String visitTo =request.getHeader(AopConstants.VISIT_TO);
|
||||
String tenantId = request.getHeader(AopConstants.HEADER_TENANT);
|
||||
String systemType = request.getHeader(AopConstants.HEADER_SYSTEM_TYPE);
|
||||
String authorization = request.getHeader(AopConstants.HEADER_AUTH);
|
||||
String deviceKind = request.getHeader(AopConstants.HAEDER_DEVICE_KIND);
|
||||
String deviceNo = request.getHeader(AopConstants.HAEDER_DEVICE_NO);
|
||||
String appVersion = request.getHeader(AopConstants.HAEDER_APP_VERSION);
|
||||
String visitTo = request.getHeader(AopConstants.VISIT_TO);
|
||||
|
||||
MethodSignature methodSignature = (MethodSignature)pjp.getSignature();
|
||||
MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
|
||||
Method method = methodSignature.getMethod();
|
||||
Class<?>[] parameterTypes = method.getParameterTypes();
|
||||
Object[] pjpArgs = pjp.getArgs();
|
||||
for(int i = 0 ; i< parameterTypes.length;i++){
|
||||
if (parameterTypes[i].isAssignableFrom(UserInfo.class)){
|
||||
UserInfo pjpArg = (UserInfo)pjpArgs[i];
|
||||
buildUserInfoWithAspect(pjpArg,userInfo);
|
||||
for (int i = 0; i < parameterTypes.length; i++) {
|
||||
if (parameterTypes[i].isAssignableFrom(UserInfo.class)) {
|
||||
UserInfo pjpArg = (UserInfo) pjpArgs[i];
|
||||
buildUserInfoWithAspect(pjpArg, userInfo);
|
||||
//塞一些系统参数
|
||||
pjpArg.setAppVersion(appVersion);
|
||||
pjpArg.setDeviceKind(deviceKind);
|
||||
@ -85,10 +80,11 @@ public class UserInfoAspect {
|
||||
}
|
||||
}
|
||||
|
||||
public void buildUserInfoWithAspect(UserInfo userInfo,String userJsonInfo){
|
||||
public void buildUserInfoWithAspect(UserInfo userInfo, String userJsonInfo) {
|
||||
//todo 直接获取用户信息。
|
||||
Map map = JSONUtil.toBean(userJsonInfo, Map.class);
|
||||
Map data = (Map)map.get("data");
|
||||
// Map data = (Map) map.get("data");
|
||||
//对bean进行封装属性
|
||||
BeanUtil.fillBeanWithMap(data, userInfo, false);
|
||||
BeanUtil.fillBeanWithMap(map, userInfo, false);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.axzo.framework.auth.config.UserInfoConfiguration
|
||||
1
auth-starter/src/main/resources/application.properties
Normal file
1
auth-starter/src/main/resources/application.properties
Normal file
@ -0,0 +1 @@
|
||||
|
||||
@ -1,102 +0,0 @@
|
||||
<?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>axzo-framework</artifactId>
|
||||
<groupId>cn.axzo.framework</groupId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>axzo-framework-auth</artifactId>
|
||||
<version>2.0.0</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<javax.servlet.version>4.0.1</javax.servlet.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<!-- 工具类 -->
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>5.7.22</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!--lombok-->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.18</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- 获取request和response-->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>5.1.5.RELEASE</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>${javax.servlet.version}</version>
|
||||
<scope>provided</scope>
|
||||
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>5.0.5.RELEASE</version>
|
||||
</dependency>
|
||||
<!-- aop的一个依赖-->
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjrt</artifactId>
|
||||
<version>1.7.3</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>2.0.0-alpha1</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-sources</id>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<distributionManagement>
|
||||
<!-- 两个ID必须与 setting.xml中的<server><id>Releases</id></server>保持一致 -->
|
||||
<repository>
|
||||
<id>rdc-releases</id>
|
||||
<name>Nexus Release Repository</name>
|
||||
<url>https://packages.aliyun.com/maven/repository/2005773-release-XI7cl5/</url>
|
||||
</repository>
|
||||
<snapshotRepository>
|
||||
<id>rdc-snapshots</id>
|
||||
<name>Nexus Snapshot Repository</name>
|
||||
<url>https://packages.aliyun.com/maven/repository/2005773-snapshot-V5Gjdf/</url>
|
||||
</snapshotRepository>
|
||||
</distributionManagement>
|
||||
|
||||
</project>
|
||||
@ -1,14 +0,0 @@
|
||||
package cn.axzo.framework.header;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
|
||||
public class Base64Decoder {
|
||||
/**
|
||||
* 进行解码
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public String decoder(String str){
|
||||
return Base64.decodeStr(str);
|
||||
}
|
||||
}
|
||||
@ -1,19 +0,0 @@
|
||||
package cn.axzo.framework.header.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class GlobalUserContext {
|
||||
protected List<String> authorities;
|
||||
protected Long UserId;
|
||||
protected String realName;
|
||||
protected String userName;
|
||||
protected String clientId;
|
||||
protected Long id;
|
||||
protected boolean enabled;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user