Skip to content

Commit

Permalink
Merge pull request #43 from zlt2000/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
zlt2000 authored Jul 10, 2022
2 parents f91378d + 4eed927 commit fcf78a4
Show file tree
Hide file tree
Showing 77 changed files with 467 additions and 103 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# zlt-microservices-platform

<p align="center">
<img src="https://img.shields.io/badge/Version-5.3.0-critical" alt="Downloads"/>
<img src="https://img.shields.io/badge/Spring%20Boot-2.5.13-blue" alt="Downloads"/>
<img src="https://img.shields.io/badge/Version-5.4.0-critical" alt="Downloads"/>
<img src="https://img.shields.io/badge/Spring%20Boot-2.5.14-blue" alt="Downloads"/>
<img src="https://img.shields.io/badge/Spring%20Cloud-2020.0.5-blue" alt="Downloads"/>
<img src="https://img.shields.io/badge/Spring%20Cloud%20Alibaba-2021.1-blue" alt="Downloads"/>
<img src="https://img.shields.io/badge/Elasticsearch-7.x-brightgreen" alt="Downloads"/>
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.zlt</groupId>
<artifactId>central-platform</artifactId>
<version>5.3.0</version>
<version>5.4.0</version>
<packaging>pom</packaging>

<properties>
Expand All @@ -14,7 +14,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>8</java.version>
<spring-cloud-alibaba-dependencies.version>2021.1</spring-cloud-alibaba-dependencies.version>
<spring-boot-dependencies.version>2.5.13</spring-boot-dependencies.version>
<spring-boot-dependencies.version>2.5.14</spring-boot-dependencies.version>
<spring-cloud-dependencies.version>2020.0.5</spring-cloud-dependencies.version>
<commons-collections4.version>4.4</commons-collections4.version>
<swagger.butler.version>2.0.1</swagger.butler.version>
Expand Down
2 changes: 1 addition & 1 deletion zlt-business/code-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.zlt</groupId>
<artifactId>zlt-business</artifactId>
<version>5.3.0</version>
<version>5.4.0</version>
</parent>
<artifactId>code-generator</artifactId>

Expand Down
2 changes: 1 addition & 1 deletion zlt-business/file-center/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.zlt</groupId>
<artifactId>zlt-business</artifactId>
<version>5.3.0</version>
<version>5.4.0</version>
</parent>
<artifactId>file-center</artifactId>
<description>文件中心</description>
Expand Down
2 changes: 1 addition & 1 deletion zlt-business/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.zlt</groupId>
<artifactId>central-platform</artifactId>
<version>5.3.0</version>
<version>5.4.0</version>
</parent>
<artifactId>zlt-business</artifactId>
<description>业务中心</description>
Expand Down
2 changes: 1 addition & 1 deletion zlt-business/search-center/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.zlt</groupId>
<artifactId>zlt-business</artifactId>
<version>5.3.0</version>
<version>5.4.0</version>
</parent>
<artifactId>search-center</artifactId>
<description>搜索中心</description>
Expand Down
2 changes: 1 addition & 1 deletion zlt-business/search-center/search-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.zlt</groupId>
<artifactId>search-center</artifactId>
<version>5.3.0</version>
<version>5.4.0</version>
</parent>
<artifactId>search-client</artifactId>
<description>搜索中心客户端</description>
Expand Down
2 changes: 1 addition & 1 deletion zlt-business/search-center/search-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.zlt</groupId>
<artifactId>search-center</artifactId>
<version>5.3.0</version>
<version>5.4.0</version>
</parent>
<artifactId>search-server</artifactId>
<description>搜索中心服务端</description>
Expand Down
2 changes: 1 addition & 1 deletion zlt-business/user-center/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.zlt</groupId>
<artifactId>zlt-business</artifactId>
<version>5.3.0</version>
<version>5.4.0</version>
</parent>
<artifactId>user-center</artifactId>
<description>用户中心</description>
Expand Down
2 changes: 1 addition & 1 deletion zlt-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.zlt</groupId>
<artifactId>central-platform</artifactId>
<version>5.3.0</version>
<version>5.4.0</version>
</parent>
<artifactId>zlt-commons</artifactId>
<description>通用组件</description>
Expand Down
2 changes: 1 addition & 1 deletion zlt-commons/zlt-auth-client-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.zlt</groupId>
<artifactId>zlt-commons</artifactId>
<version>5.3.0</version>
<version>5.4.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import com.central.common.constant.CommonConstant;
import com.central.common.constant.SecurityConstants;
import com.central.common.context.LoginUserContextHolder;
import com.central.common.model.SysUser;
import com.central.common.utils.SpringUtil;
import com.central.oauth2.common.token.CustomWebAuthenticationDetails;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
import org.springframework.security.oauth2.common.exceptions.UnapprovedClientAuthenticationException;
Expand All @@ -15,15 +18,16 @@

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Enumeration;
import java.util.Map;
import java.util.*;

/**
* 认证授权相关工具类
*
* @author zlt
* @date 2018/5/13
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
@Slf4j
public class AuthUtils {
Expand Down Expand Up @@ -73,12 +77,12 @@ private static String extractHeaderToken(HttpServletRequest request) {
/**
* 校验accessToken
*/
public static void checkAccessToken(HttpServletRequest request) {
public static SysUser checkAccessToken(HttpServletRequest request) {
String accessToken = extractToken(request);
checkAccessToken(accessToken);
return checkAccessToken(accessToken);
}

public static void checkAccessToken(String accessTokenValue) {
public static SysUser checkAccessToken(String accessTokenValue) {
TokenStore tokenStore = SpringUtil.getBean(TokenStore.class);
OAuth2AccessToken accessToken = tokenStore.readAccessToken(accessTokenValue);
if (accessToken == null || accessToken.getValue() == null) {
Expand All @@ -91,6 +95,17 @@ public static void checkAccessToken(String accessTokenValue) {
if (result == null) {
throw new InvalidTokenException("Invalid access token: " + accessTokenValue);
}
return setContext(result);
}

/**
* 用户信息赋值 context 对象
*/
public static SysUser setContext(Authentication authentication) {
SecurityContextHolder.getContext().setAuthentication(authentication);
SysUser user = getUser(authentication);
LoginUserContextHolder.setUser(user);
return user;
}

/**
Expand Down Expand Up @@ -134,6 +149,21 @@ public static String getUsername(Authentication authentication) {
return username;
}

/**
* 获取登陆的用户对象
*/
public static SysUser getUser(Authentication authentication) {
SysUser user = null;
if (authentication != null && !(authentication instanceof AnonymousAuthenticationToken)) {
Object principal = authentication.getPrincipal();
//客户端模式只返回一个clientId
if (principal instanceof SysUser) {
user = (SysUser)principal;
}
}
return user;
}

/**
* 获取登陆的帐户类型
*/
Expand Down
2 changes: 1 addition & 1 deletion zlt-commons/zlt-common-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.zlt</groupId>
<artifactId>zlt-commons</artifactId>
<version>5.3.0</version>
<version>5.4.0</version>
</parent>
<artifactId>zlt-common-core</artifactId>
<description>公共通用组件</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface CommonConstant {
/**
* 项目版本号(banner使用)
*/
String PROJECT_VERSION = "5.3.0";
String PROJECT_VERSION = "5.4.0";

/**
* token请求头名称
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.central.common.context;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.central.common.model.SysUser;

/**
* 登录用户holder
*
* @author zlt
* @date 2022/6/26
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
public class LoginUserContextHolder {
private static final ThreadLocal<SysUser> CONTEXT = new TransmittableThreadLocal<>();

public static void setUser(SysUser user) {
CONTEXT.set(user);
}

public static SysUser getUser() {
return CONTEXT.get();
}

public static void clear() {
CONTEXT.remove();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
import com.central.common.feign.UserService;
import com.central.common.model.SysRole;
import com.central.common.model.SysUser;
import com.central.common.utils.LoginUserUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
Expand Down Expand Up @@ -61,30 +65,9 @@ public Object resolveArgument(MethodParameter methodParameter,
LoginUser loginUser = methodParameter.getParameterAnnotation(LoginUser.class);
boolean isFull = loginUser.isFull();
HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
String userId = request.getHeader(SecurityConstants.USER_ID_HEADER);
String username = request.getHeader(SecurityConstants.USER_HEADER);
String roles = request.getHeader(SecurityConstants.ROLE_HEADER);
//账号类型
String accountType = request.getHeader(SecurityConstants.ACCOUNT_TYPE_HEADER);
if (StrUtil.isBlank(username)) {
log.warn("resolveArgument error username is empty");
return null;
}
SysUser user;
if (isFull) {
user = userService.selectByUsername(username);
} else {
user = new SysUser();
user.setId(Long.valueOf(userId));
user.setUsername(username);
}
List<SysRole> sysRoleList = new ArrayList<>();
Arrays.stream(roles.split(",")).forEach(role -> {
SysRole sysRole = new SysRole();
sysRole.setCode(role);
sysRoleList.add(sysRole);
});
user.setRoles(sysRoleList);
return user;

return LoginUserUtils.getCurrentUser(request, isFull);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.central.common.utils;

import cn.hutool.core.util.StrUtil;
import com.central.common.constant.SecurityConstants;
import com.central.common.feign.UserService;
import com.central.common.model.SysRole;
import com.central.common.model.SysUser;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* 获取当前登录人工具类
*
* @author zlt
* @version 1.0
* @date 2022/6/26
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
public class LoginUserUtils {
/**
* 获取当前登录人
*/
public static SysUser getCurrentUser(HttpServletRequest request, boolean isFull) {
SysUser user = null;

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && !(authentication instanceof AnonymousAuthenticationToken)) {
Object principal = authentication.getPrincipal();
//客户端模式只返回一个clientId
if (principal instanceof SysUser) {
user = (SysUser)principal;
}
}
if (user == null) {
String userId = request.getHeader(SecurityConstants.USER_ID_HEADER);
String username = request.getHeader(SecurityConstants.USER_HEADER);
String roles = request.getHeader(SecurityConstants.ROLE_HEADER);

if (StrUtil.isAllNotBlank(username, userId)) {
if (isFull) {
UserService userService = SpringUtil.getBean(UserService.class);
user = userService.selectByUsername(username);
} else {
user = new SysUser();
user.setId(Long.valueOf(userId));
user.setUsername(username);
}
if (StrUtil.isNotBlank(roles)) {
List<SysRole> sysRoleList = new ArrayList<>();
Arrays.stream(roles.split(",")).forEach(role -> {
SysRole sysRole = new SysRole();
sysRole.setCode(role);
sysRoleList.add(sysRole);
});
user.setRoles(sysRoleList);
}
}
}
return user;
}
}
2 changes: 1 addition & 1 deletion zlt-commons/zlt-common-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.zlt</groupId>
<artifactId>zlt-commons</artifactId>
<version>5.3.0</version>
<version>5.4.0</version>
</parent>
<artifactId>zlt-common-spring-boot-starter</artifactId>
<description>公共通用组件</description>
Expand Down
Loading

0 comments on commit fcf78a4

Please sign in to comment.