axzo-framework-header做鉴权

This commit is contained in:
zhuguanghong 2022-03-22 12:32:03 +08:00
parent 536fd1c48d
commit 417bf54924
11 changed files with 165 additions and 60 deletions

View File

@ -9,7 +9,8 @@
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>axzo-framework-header</artifactId> <artifactId>axzo-framework-header</artifactId>
<version>1.0.0</version> <version>1.0.0-SNAPSHOT</version>
<properties> <properties>
<maven.compiler.source>8</maven.compiler.source> <maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target> <maven.compiler.target>8</maven.compiler.target>

View File

@ -1,14 +0,0 @@
package cn.axzo.framework.header;
public class GlobalUserContext {
public static final ThreadLocal<GlobalUserInfo> threadLocal = new ThreadLocal<>();
public static GlobalUserInfo get() {
return threadLocal.get();
}
public static void release() {
// TODO
threadLocal.remove();
}
}

View File

@ -1,29 +0,0 @@
package cn.axzo.framework.header;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
public class GlobalUserContextInterceptor {
protected void doBefore() {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if(requestAttributes == null) {
return;
}
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
if(request == null) {
return;
}
String authorization = request.getHeader(HeaderConstant.KEY_AUTHORIZATION);
// TODO
// UserInfo tmp = Base64Decoder.decode(authorization);
//
// GlobalUserContext.get().setUserId(tmp.getUserId);
}
protected void doAfter() {
GlobalUserContext.release();
}
}

View File

@ -1,7 +0,0 @@
package cn.axzo.framework.header;
public class GlobalUserInfo {
// 此处放当前登录用户的基本信息
// TODO
}

View File

@ -1,6 +0,0 @@
package cn.axzo.framework.header;
public class HeaderConstant {
public static final String KEY_AUTHORIZATION = "Authorization";
}

View File

@ -0,0 +1,11 @@
package cn.axzo.framework.header.annotation;
import java.lang.annotation.*;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PreBuildUser {
//暂时还不知道 怎么用
String value();
}

View File

@ -0,0 +1,92 @@
package cn.axzo.framework.header.configuration;
import cn.axzo.framework.header.annotation.PreBuildUser;
import cn.axzo.framework.header.constants.AopConstants;
import cn.axzo.framework.header.domian.UserInfo;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.codec.Base64;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Objects;
/**
* 构建切面在进入controller之前 进行User用户构建,
* 使用方法 直接方法上开启注解入参带上userinfo 或者其<子类?>
*/
@Aspect
@Component
@Slf4j
public class UserInfoAspect {
@Around(value = "@annotation(preBuildUser)")
public Object methodHandler(ProceedingJoinPoint pjp, PreBuildUser preBuildUser) throws Throwable {
HttpServletRequest httpRequest =null ;
String token = null;
try {
httpRequest = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();//获取request
token = httpRequest.getHeader("Authorization");
} catch (NullPointerException e) {
log.error("preAuthorizeAspect get request and response is null--NullPointException");
}catch (Exception exception){
log.error("preAuthorizeAspect has a exception-->{}",exception.getMessage());
}
assert httpRequest != null;
this.fillInUserInfoDetail(httpRequest,pjp);
return pjp.proceed();
}
public void fillInUserInfoDetail(HttpServletRequest request,ProceedingJoinPoint pjp){
String userJsonInfo = request.getHeader("userinfo");
if(StringUtils.isEmpty(userJsonInfo)){
//没有拿到用户信息这个构建也没有意义了
return ;
}
String encode = 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);
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,encode);
pjpArg.setAppVersion(appVersion);
pjpArg.setDeviceKind(deviceKind);
pjpArg.setDeviceNo(deviceNo);
pjpArg.setSystemType(systemType);
pjpArg.setTenantId(tenantId);
pjpArg.setVisitTo(visitTo);
pjpArg.setToken(authorization);
break;
}
}
}
public void buildUserInfoWithAspect(UserInfo userInfo,String userJsonInfo){
Map map = JSONUtil.toBean(userJsonInfo, Map.class);
Map data = (Map)map.get("data");
//对bean进行封装属性
BeanUtil.fillBeanWithMap(data, userInfo, false);
}
}

View File

@ -0,0 +1,14 @@
package cn.axzo.framework.header.constants;
public class AopConstants {
public static final String HEADER_TENANT = "tenantId";
public static final String HEADER_SYSTEM_TYPE = "1";
public static final String HEADER_AUTH = "authorization";
public static final String HAEDER_DEVICE_KIND = "deviceKind";
public static final String HAEDER_DEVICE_NO = "deviceNo";
public static final String HAEDER_APP_VERSION = "appVersion";
public static final String VISIT_TO = "visitTo";
}

View File

@ -12,20 +12,46 @@ import java.util.List;
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
public class UserInfo { public class UserInfo {
//客户端Id
private String clientId; private String clientId;
//身份证
private String idCard; private String idCard;
//身份验证过期时间
private boolean credentialsNonExpired; private boolean credentialsNonExpired;
//角色
private List<String> roles; private List<String> roles;
//性别
private int sex; private int sex;
//终端
private String terminal; private String terminal;
//axzoId
private Long userId; private Long userId;
//权限列表
private List<String> authorities; private List<String> authorities;
//是否有效
private boolean enabled; private boolean enabled;
//账户是否被锁定
private boolean isLock; private boolean isLock;
//真实姓名
private String realName; private String realName;
//密码
private String password; private String password;
//手机号
private String phoneNumber; private String phoneNumber;
//
private Long id; private Long id;
private boolean accountNonLocked; private boolean accountNonLocked;
private String username; private String username;
/**
*从请求头赋值
*/
//租户id
private String tenantId;
private String systemType;
private String deviceKind;
private String deviceNo;
private String appVersion;
private String token;
private String visitTo;
} }

View File

@ -8,7 +8,6 @@ import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.aopalliance.intercept.Joinpoint;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;

22
pom.xml
View File

@ -17,7 +17,7 @@
<module>asyncTool</module> <module>asyncTool</module>
<module>alarm-spring-boot-starter</module> <module>alarm-spring-boot-starter</module>
<module>canal-alarm</module> <module>canal-alarm</module>
<module>axzo-framework-header</module> <module>axzo-framework-header</module>
</modules> </modules>
<build> <build>
@ -27,10 +27,28 @@
<artifactId>maven-deploy-plugin</artifactId> <artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version> <version>2.8.2</version>
<configuration> <configuration>
<skip>true</skip> <skip>false</skip>
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins> </plugins>
</build> </build>
</project> </project>