Commit 52ba6026 by xlx

账号登录

接口认证
parent 2f8a5b1d
Showing with 637 additions and 1144 deletions
...@@ -3,6 +3,7 @@ package com.matchtech.system.api; ...@@ -3,6 +3,7 @@ package com.matchtech.system.api;
import com.matchtech.common.core.constants.SecurityConstants; import com.matchtech.common.core.constants.SecurityConstants;
import com.matchtech.common.core.constants.ServiceNameConstants; import com.matchtech.common.core.constants.ServiceNameConstants;
import com.matchtech.common.core.domain.R; import com.matchtech.common.core.domain.R;
import com.matchtech.common.core.page.resp.ApiResponseEntity;
import com.matchtech.system.api.domain.SysUser; import com.matchtech.system.api.domain.SysUser;
import com.matchtech.system.api.factory.RemoteUserFallbackFactory; import com.matchtech.system.api.factory.RemoteUserFallbackFactory;
import com.matchtech.system.api.model.LoginUser; import com.matchtech.system.api.model.LoginUser;
...@@ -28,27 +29,6 @@ public interface RemoteUserService { ...@@ -28,27 +29,6 @@ public interface RemoteUserService {
* @param source 请求来源 * @param source 请求来源
* @return 结果 * @return 结果
*/ */
@GetMapping("/user/info/{username}") @GetMapping("/a/sys/user/info/{userId}")
public R<LoginUser> getUserInfo(@PathVariable("username") String username, @RequestHeader(SecurityConstants.FROM_SOURCE) String source); public ApiResponseEntity<LoginUser> getUserInfo(@PathVariable("userId") String userId, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
/**
* 注册用户信息
*
* @param sysUser 用户信息
* @param source 请求来源
* @return 结果
*/
@PostMapping("/user/register")
public R<Boolean> registerUserInfo(@RequestBody SysUser sysUser, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
/**
* 记录用户登录IP地址和登录时间
*
* @param sysUser 用户信息
* @param source 请求来源
* @return 结果
*/
@PutMapping("/user/recordlogin")
public R<Boolean> recordUserLogin(@RequestBody SysUser sysUser, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
} }
package com.matchtech.system.api.domain; package com.matchtech.system.api.domain;
import java.util.Date;
import java.util.List;
import com.matchtech.common.core.domain.BaseEntity;
import jakarta.validation.constraints.*;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.matchtech.common.core.annotation.Excel; import com.matchtech.common.core.annotation.Excel;
import com.matchtech.common.core.annotation.Excel.ColumnType; import com.matchtech.common.core.annotation.Excel.ColumnType;
import com.matchtech.common.core.annotation.Excel.Type; import com.matchtech.common.core.annotation.Excel.Type;
import com.matchtech.common.core.constants.UserConstants;
import com.matchtech.common.core.annotation.Excels; import com.matchtech.common.core.annotation.Excels;
import com.matchtech.common.core.xss.Xss; import com.matchtech.common.core.constants.UserConstants;
import com.matchtech.common.core.domain.BaseEntity;
import lombok.Data;
import java.util.Date;
import java.util.List;
/** /**
* 用户对象 sys_user * 用户对象 sys_user
* *
* @author matchtech * @author matchtech
*/ */
@Data
public class SysUser extends BaseEntity public class SysUser extends BaseEntity
{ {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/** 用户ID */ /** 用户ID */
@Excel(name = "用户序号", type = Type.EXPORT, cellType = ColumnType.NUMERIC, prompt = "用户编号") private String userId;
private Long userId;
/** 部门ID */ /** 部门ID */
@Excel(name = "部门编号", type = Type.IMPORT) private String deptId;
private Long deptId;
/** 用户账号 */ /** 用户账号 */
@Excel(name = "登录名称") @Excel(name = "登录名称")
...@@ -94,222 +90,15 @@ public class SysUser extends BaseEntity ...@@ -94,222 +90,15 @@ public class SysUser extends BaseEntity
/** 角色ID */ /** 角色ID */
private Long roleId; private Long roleId;
public SysUser()
{
}
public SysUser(Long userId)
{
this.userId = userId;
}
public Long getUserId()
{
return userId;
}
public void setUserId(Long userId)
{
this.userId = userId;
}
public boolean isAdmin() public boolean isAdmin()
{ {
return isAdmin(this.userId); return isAdmin(this.userId);
} }
public static boolean isAdmin(Long userId) public static boolean isAdmin(String userId)
{ {
return UserConstants.isAdmin(userId); return UserConstants.isAdmin(userId);
} }
public Long getDeptId()
{
return deptId;
}
public void setDeptId(Long deptId)
{
this.deptId = deptId;
}
@Xss(message = "用户昵称不能包含脚本字符")
@Size(min = 0, max = 30, message = "用户昵称长度不能超过30个字符")
public String getNickName()
{
return nickName;
}
public void setNickName(String nickName)
{
this.nickName = nickName;
}
@Xss(message = "用户账号不能包含脚本字符")
@NotBlank(message = "用户账号不能为空")
@Size(min = 0, max = 30, message = "用户账号长度不能超过30个字符")
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
@Email(message = "邮箱格式不正确")
@Size(min = 0, max = 50, message = "邮箱长度不能超过50个字符")
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
@Size(min = 0, max = 11, message = "手机号码长度不能超过11个字符")
public String getPhonenumber()
{
return phonenumber;
}
public void setPhonenumber(String phonenumber)
{
this.phonenumber = phonenumber;
}
public String getSex()
{
return sex;
}
public void setSex(String sex)
{
this.sex = sex;
}
public String getAvatar()
{
return avatar;
}
public void setAvatar(String avatar)
{
this.avatar = avatar;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getStatus()
{
return status;
}
public void setStatus(String status)
{
this.status = status;
}
public String getDelFlag()
{
return delFlag;
}
public void setDelFlag(String delFlag)
{
this.delFlag = delFlag;
}
public String getLoginIp()
{
return loginIp;
}
public void setLoginIp(String loginIp)
{
this.loginIp = loginIp;
}
public Date getLoginDate()
{
return loginDate;
}
public void setLoginDate(Date loginDate)
{
this.loginDate = loginDate;
}
public Date getPwdUpdateDate()
{
return pwdUpdateDate;
}
public void setPwdUpdateDate(Date pwdUpdateDate)
{
this.pwdUpdateDate = pwdUpdateDate;
}
public SysDept getDept()
{
return dept;
}
public void setDept(SysDept dept)
{
this.dept = dept;
}
public List<SysRole> getRoles()
{
return roles;
}
public void setRoles(List<SysRole> roles)
{
this.roles = roles;
}
public Long[] getRoleIds()
{
return roleIds;
}
public void setRoleIds(Long[] roleIds)
{
this.roleIds = roleIds;
}
public Long[] getPostIds()
{
return postIds;
}
public void setPostIds(Long[] postIds)
{
this.postIds = postIds;
}
public Long getRoleId()
{
return roleId;
}
public void setRoleId(Long roleId)
{
this.roleId = roleId;
}
} }
package com.matchtech.system.api.factory; package com.matchtech.system.api.factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import com.matchtech.common.core.domain.R; import com.matchtech.common.core.domain.R;
import com.matchtech.common.core.page.resp.ApiResponseEntity;
import com.matchtech.system.api.RemoteUserService; import com.matchtech.system.api.RemoteUserService;
import com.matchtech.system.api.domain.SysUser; import com.matchtech.system.api.domain.SysUser;
import com.matchtech.system.api.model.LoginUser; import com.matchtech.system.api.model.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
/** /**
* 用户服务降级处理 * 用户服务降级处理
* *
* @author matchtech * @author matchtech
*/ */
@Component @Component
public class RemoteUserFallbackFactory implements FallbackFactory<RemoteUserService> public class RemoteUserFallbackFactory implements FallbackFactory<RemoteUserService> {
{
private static final Logger log = LoggerFactory.getLogger(RemoteUserFallbackFactory.class); private static final Logger log = LoggerFactory.getLogger(RemoteUserFallbackFactory.class);
@Override @Override
public RemoteUserService create(Throwable throwable) public RemoteUserService create(Throwable throwable) {
{
log.error("用户服务调用失败:{}", throwable.getMessage()); log.error("用户服务调用失败:{}", throwable.getMessage());
return new RemoteUserService() return new RemoteUserService() {
{
@Override
public R<LoginUser> getUserInfo(String username, String source)
{
return R.fail("获取用户失败:" + throwable.getMessage());
}
@Override
public R<Boolean> registerUserInfo(SysUser sysUser, String source)
{
return R.fail("注册用户失败:" + throwable.getMessage());
}
@Override @Override
public R<Boolean> recordUserLogin(SysUser sysUser, String source) public ApiResponseEntity<LoginUser> getUserInfo(String username, String source) {
{ return ApiResponseEntity.fail(500, "获取用户失败", throwable.getMessage());
return R.fail("记录用户登录信息失败:" + throwable.getMessage());
} }
}; };
} }
......
...@@ -3,12 +3,14 @@ package com.matchtech.system.api.model; ...@@ -3,12 +3,14 @@ package com.matchtech.system.api.model;
import java.io.Serializable; import java.io.Serializable;
import java.util.Set; import java.util.Set;
import com.matchtech.system.api.domain.SysUser; import com.matchtech.system.api.domain.SysUser;
import lombok.Data;
/** /**
* 用户信息 * 用户信息
* *
* @author matchtech * @author matchtech
*/ */
@Data
public class LoginUser implements Serializable public class LoginUser implements Serializable
{ {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
...@@ -21,7 +23,7 @@ public class LoginUser implements Serializable ...@@ -21,7 +23,7 @@ public class LoginUser implements Serializable
/** /**
* 用户名id * 用户名id
*/ */
private Long userid; private String userid;
/** /**
* 用户名 * 用户名
...@@ -58,93 +60,4 @@ public class LoginUser implements Serializable ...@@ -58,93 +60,4 @@ public class LoginUser implements Serializable
*/ */
private SysUser sysUser; private SysUser sysUser;
public String getToken()
{
return token;
}
public void setToken(String token)
{
this.token = token;
}
public Long getUserid()
{
return userid;
}
public void setUserid(Long userid)
{
this.userid = userid;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public Long getLoginTime()
{
return loginTime;
}
public void setLoginTime(Long loginTime)
{
this.loginTime = loginTime;
}
public Long getExpireTime()
{
return expireTime;
}
public void setExpireTime(Long expireTime)
{
this.expireTime = expireTime;
}
public String getIpaddr()
{
return ipaddr;
}
public void setIpaddr(String ipaddr)
{
this.ipaddr = ipaddr;
}
public Set<String> getPermissions()
{
return permissions;
}
public void setPermissions(Set<String> permissions)
{
this.permissions = permissions;
}
public Set<String> getRoles()
{
return roles;
}
public void setRoles(Set<String> roles)
{
this.roles = roles;
}
public SysUser getSysUser()
{
return sysUser;
}
public void setSysUser(SysUser sysUser)
{
this.sysUser = sysUser;
}
} }
...@@ -59,6 +59,12 @@ ...@@ -59,6 +59,12 @@
<artifactId>matchtech-common-dto</artifactId> <artifactId>matchtech-common-dto</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.matchtech</groupId>
<artifactId>matchtech-common-core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package com.matchtech.system.controller; package com.matchtech.matchtechauthlocal.controller;
import com.matchtech.common.core.constants.SecurityConstants;
import com.matchtech.common.core.page.resp.ApiResponseEntity; import com.matchtech.common.core.page.resp.ApiResponseEntity;
import com.matchtech.common.core.page.resp.ApiResponseUtils; import com.matchtech.common.core.page.resp.ApiResponseUtils;
import com.matchtech.common.core.web.controller.BaseController; import com.matchtech.common.core.web.controller.BaseController;
import com.matchtech.common.dto.login.SysLoginDto;
import com.matchtech.common.dto.login.SysLoginVo; import com.matchtech.common.dto.login.SysLoginVo;
import com.matchtech.common.dto.login.TokenDto;
import com.matchtech.common.redis.constant.RedisConstants; import com.matchtech.common.redis.constant.RedisConstants;
import com.matchtech.common.redis.service.RedisUtils; import com.matchtech.common.redis.service.RedisUtils;
import com.matchtech.system.domain.dto.SysLoginDto; import com.matchtech.common.security.service.TokenService;
import com.matchtech.system.service.SysUserService; import com.matchtech.system.api.RemoteUserService;
import com.matchtech.system.api.model.LoginUser;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
...@@ -18,24 +22,28 @@ import org.springframework.web.bind.annotation.RequestBody; ...@@ -18,24 +22,28 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@Tag(name = "系统登录") @Tag(name = "系统登录")
@RestController @RestController
@RequestMapping(value = "${matchtech.adminPath}/sys/") @RequestMapping(value = "a/sys/")
public class SysLoginController extends BaseController { public class LocalLoginController extends BaseController {
@Autowired @Autowired
private SysUserService sysUserService; private RemoteUserService remoteUserService;
@Autowired
private TokenService tokenService;
@Operation(summary = "用户登录") @Operation(summary = "用户登录")
@PostMapping("/login") @PostMapping("/login")
public ApiResponseEntity<SysLoginVo> login(@Validated @RequestBody SysLoginDto loginDto) { public ApiResponseEntity login(@Validated @RequestBody SysLoginDto loginDto) {
ApiResponseEntity<LoginUser> userInfo = remoteUserService.getUserInfo(loginDto.getUsername(), SecurityConstants.INNER);
SysLoginVo sysLoginVo = sysUserService.login(loginDto); if(userInfo.getCode() != 200){
// String token = sysLoginVo.getToken(); return userInfo;
// CompletableFuture.runAsync(() -> { }
// GeneralUtils.setUserPermissionCache(token); LoginUser data = userInfo.getData();
// }); TokenDto token = tokenService.createToken(data);
return ApiResponseUtils.success(sysLoginVo); return ApiResponseUtils.success(token);
} }
@Operation(summary = "退出登录") @Operation(summary = "退出登录")
...@@ -45,17 +53,4 @@ public class SysLoginController extends BaseController { ...@@ -45,17 +53,4 @@ public class SysLoginController extends BaseController {
RedisUtils.delete(RedisConstants.SYS_USER_TOKEN_PREFIX + token); RedisUtils.delete(RedisConstants.SYS_USER_TOKEN_PREFIX + token);
return ApiResponseUtils.success(); return ApiResponseUtils.success();
} }
// @Operation(summary = "获取登录用户权限菜单")
// @PostMapping("/getLoginUserMenu")
// public ApiResponseEntity<List<SysMenuVo>> getLoginUserMenu(HttpServletRequest request) {
// SysUserEntity sysUser = RedisUtils.get(RedisConstants.SYS_USER_TOKEN_PREFIX + request.getHeader("token"));
// if (Constants.SUPER_ADMIN_ID.equals(sysUser.getId())) {
// return ApiResponseUtils.success(sysMenuService.tree(new SysMenuQueryDto()));
// }
// // TODO: 非超级管理员获取有权限菜单
// return ApiResponseUtils.success(sysRoleMenuService.getMenuByUserId(sysUser.getId()));
// }
} }
#matchtech-auth-local
# Tomcat
server:
port: 28080
# Spring
spring:
application:
# 应用名称
name: matchtech-auth-local
profiles:
# 环境配置
active: dev
cloud:
nacos:
discovery:
# 服务注册地址
server-addr: 127.0.0.1:8848
config:
# 配置中心地址
server-addr: 127.0.0.1:8848
# 配置文件格式
file-extension: yml
# 共享配置
shared-configs:
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
# 扩展配置
extension-configs:
- data-id: ${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
refresh: true
...@@ -16,8 +16,8 @@ public class MatchtechProperties { ...@@ -16,8 +16,8 @@ public class MatchtechProperties {
private static final AntPathMatcher matcher = new AntPathMatcher(); private static final AntPathMatcher matcher = new AntPathMatcher();
private Map<String, List<String>> white; private Map<String, List<String>> white;
private String tokenTimeout; private String tokenTimeout;
private String adminPath; private String adminPath = "a";
private String frontPath; private String frontPath = "f";
private String validateFlag; private String validateFlag;
private String signFlag; private String signFlag;
private String frontUrl; private String frontUrl;
......
...@@ -20,7 +20,8 @@ public class SecurityConstants ...@@ -20,7 +20,8 @@ public class SecurityConstants
/** /**
* 授权信息字段 * 授权信息字段
*/ */
public static final String AUTHORIZATION_HEADER = "Authorization"; //public static final String AUTHORIZATION_HEADER = "Authorization";
public static final String AUTHORIZATION_HEADER = "token";
/** /**
* 请求来源 * 请求来源
......
...@@ -15,10 +15,10 @@ public class ServiceNameConstants ...@@ -15,10 +15,10 @@ public class ServiceNameConstants
/** /**
* 系统模块的serviceid * 系统模块的serviceid
*/ */
public static final String SYSTEM_SERVICE = "matchtech-modules-system"; public static final String SYSTEM_SERVICE = "matchtech-system";
/** /**
* 文件服务的serviceid * 文件服务的serviceid
*/ */
public static final String FILE_SERVICE = "matchtech-modules-file"; public static final String FILE_SERVICE = "matchtech-file";
} }
package com.matchtech.common.core.constants; package com.matchtech.common.core.constants;
import cn.hutool.core.util.StrUtil;
/** /**
* 用户常量信息 * 用户常量信息
* *
...@@ -81,8 +83,8 @@ public class UserConstants ...@@ -81,8 +83,8 @@ public class UserConstants
public static final int PASSWORD_MAX_LENGTH = 20; public static final int PASSWORD_MAX_LENGTH = 20;
public static boolean isAdmin(Long userId) public static boolean isAdmin(String userId)
{ {
return userId != null && 1L == userId; return StrUtil.equals(userId,"1");
} }
} }
...@@ -51,9 +51,9 @@ public class SecurityContextHolder ...@@ -51,9 +51,9 @@ public class SecurityContextHolder
THREAD_LOCAL.set(threadLocalMap); THREAD_LOCAL.set(threadLocalMap);
} }
public static Long getUserId() public static String getUserId()
{ {
return Convert.toLong(get(SecurityConstants.DETAILS_USER_ID), 0L); return Convert.toStr(get(SecurityConstants.DETAILS_USER_ID), "0");
} }
public static void setUserId(String account) public static void setUserId(String account)
......
package com.matchtech.common.core.servlet;
import jakarta.servlet.ReadListener;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class MyHttpServletRequestWrapper extends HttpServletRequestWrapper {
private Logger logger = LoggerFactory.getLogger(getClass());
/** 复制请求body */
private byte[] body;
public MyHttpServletRequestWrapper(HttpServletRequest request) {
super(request);
try {
// 设置编码格式, 防止中文乱码
//request.setCharacterEncoding("UTF-8");
// 将请求中的流取出来放到body里,后面都只操作body就行
this.body = RequestReadUtils.readByte(request);
} catch (Exception e) {
logger.error("MyHttpServletRequestWrapper 拦截器异常", e);
}
}
@Override
public ServletInputStream getInputStream() {
// 返回body的流信息即可
try (final ByteArrayInputStream bais = new ByteArrayInputStream(body)) {
return getServletInputStream(bais);
} catch (IOException e) {
logger.error("MyHttpServletRequestWrapper.getInputStream() exception", e);
throw new RuntimeException("MyHttpServletRequestWrapper 获取input流异常");
}
}
@Override
public BufferedReader getReader() {
return new BufferedReader(new InputStreamReader(this.getInputStream()));
}
/**
* 重写getInputStream流
*
* @param bais
* @return
*/
private static ServletInputStream getServletInputStream(ByteArrayInputStream bais) {
return new ServletInputStream() {
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener readListener) {
}
@Override
public int read() {
return bais.read();
}
};
}
public byte[] getBody() {
return body;
}
}
\ No newline at end of file
package com.matchtech.common.core.servlet;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.WriteListener;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServletResponseWrapper;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
public class MyHttpServletResponseWrapper extends HttpServletResponseWrapper {
private ByteArrayOutputStream buffer = null;
private ServletOutputStream out = null;
private PrintWriter writer = null;
public MyHttpServletResponseWrapper(HttpServletResponse resp) throws IOException {
super(resp);
// 真正存储数据的流
buffer = new ByteArrayOutputStream();
out = new WapperedOutputStream(buffer);
writer = new PrintWriter(new OutputStreamWriter(buffer, this.getCharacterEncoding()));
}
/**
* 重载父类获取outputstream的方法
*/
@Override
public ServletOutputStream getOutputStream() throws IOException {
return out;
}
/**
* 重载父类获取writer的方法
*/
@Override
public PrintWriter getWriter() throws UnsupportedEncodingException {
return writer;
}
/**
* 重载父类获取flushBuffer的方法
*/
@Override
public void flushBuffer() throws IOException {
if (out != null) {
out.flush();
}
if (writer != null) {
writer.flush();
}
}
@Override
public void reset() {
buffer.reset();
}
/**
* 将out、writer中的数据强制输出到WapperedResponse的buffer里面,否则取不到数据
*/
public byte[] getResponseData() throws IOException {
flushBuffer();
return buffer.toByteArray();
}
/**
* 内部类,对ServletOutputStream进行包装
*/
private class WapperedOutputStream extends ServletOutputStream {
private ByteArrayOutputStream bos = null;
public WapperedOutputStream(ByteArrayOutputStream stream) throws IOException {
bos = stream;
}
@Override
public void write(int b) throws IOException {
bos.write(b);
}
@Override
public void write(byte[] b) throws IOException {
bos.write(b, 0, b.length);
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setWriteListener(WriteListener writeListener) {
}
}
}
package com.matchtech.common.core.servlet;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
public class RequestReadUtils {
public static Logger logger = LoggerFactory.getLogger(RequestReadUtils.class);
/**
* 读取请求流
*
* @param request
* @return
* @throws IOException
* @throws UnsupportedEncodingException
*/
public static String read(HttpServletRequest request) throws IOException {
// StringBuilder sb = new StringBuilder();
// BufferedReader reader = request.getReader();
// String line;
// while ((line = reader.readLine()) != null) {
// sb.append(line);
// }
// return sb.toString();
if (request instanceof MyHttpServletRequestWrapper cachedRequest) {
// 使用Java 17的模式匹配
return new String(cachedRequest.getBody(), request.getCharacterEncoding());
} else {
// 对于普通请求,使用原来的方式
StringBuilder sb = new StringBuilder();
try (BufferedReader reader = request.getReader()) {
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
}
return sb.toString();
}
}
public static byte[] readByte(HttpServletRequest request) throws IOException {
InputStream inputStream = request.getInputStream();
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
byte[] data = result.toByteArray();
return data;
}
}
package com.matchtech.common.core.async; package com.matchtech.common.core.thread;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
......
package com.matchtech.common.dto.login; package com.matchtech.common.core.thread;
import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonFormat;
......
package com.matchtech.common.core.async; package com.matchtech.common.core.thread;
import org.springframework.core.task.TaskDecorator; import org.springframework.core.task.TaskDecorator;
......
package com.matchtech.common.core.async; package com.matchtech.common.core.thread;
public class MyThreadLocal { public class MyThreadLocal {
private static ThreadLocal<String> tokenThreadLocal = new ThreadLocal<String>(); private static ThreadLocal<String> tokenThreadLocal = new ThreadLocal<String>();
private static ThreadLocal<String> tenantIdThreadLocal = new ThreadLocal<String>(); private static ThreadLocal<String> tenantIdThreadLocal = new ThreadLocal<String>();
private static ThreadLocal<CurrentUserDTO> userThreadLocal = new ThreadLocal<CurrentUserDTO>();
public static CurrentUserDTO getUserThreadLocal() {
return userThreadLocal.get();
}
public static void setUserThreadLocal(CurrentUserDTO currentUserDTO) {
userThreadLocal.set(currentUserDTO);
}
/** /**
* 设置登录token * 设置登录token
* *
......
com.matchtech.common.core.utils.SpringUtils com.matchtech.common.core.utils.SpringUtils
com.matchtech.common.core.config.MatchtechProperties
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/
<# : batch portion
@REM ----------------------------------------------------------------------------
@REM Licensed to the Apache Software Foundation (ASF) under one
@REM or more contributor license agreements. See the NOTICE file
@REM distributed with this work for additional information
@REM regarding copyright ownership. The ASF licenses this file
@REM to you under the Apache License, Version 2.0 (the
@REM "License"); you may not use this file except in compliance
@REM with the License. You may obtain a copy of the License at
@REM
@REM http://www.apache.org/licenses/LICENSE-2.0
@REM
@REM Unless required by applicable law or agreed to in writing,
@REM software distributed under the License is distributed on an
@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@REM KIND, either express or implied. See the License for the
@REM specific language governing permissions and limitations
@REM under the License.
@REM ----------------------------------------------------------------------------
@REM ----------------------------------------------------------------------------
@REM Apache Maven Wrapper startup batch script, version 3.3.4
@REM
@REM Optional ENV vars
@REM MVNW_REPOURL - repo url base for downloading maven distribution
@REM MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven
@REM MVNW_VERBOSE - true: enable verbose log; others: silence the output
@REM ----------------------------------------------------------------------------
@IF "%__MVNW_ARG0_NAME__%"=="" (SET __MVNW_ARG0_NAME__=%~nx0)
@SET __MVNW_CMD__=
@SET __MVNW_ERROR__=
@SET __MVNW_PSMODULEP_SAVE=%PSModulePath%
@SET PSModulePath=
@FOR /F "usebackq tokens=1* delims==" %%A IN (`powershell -noprofile "& {$scriptDir='%~dp0'; $script='%__MVNW_ARG0_NAME__%'; icm -ScriptBlock ([Scriptblock]::Create((Get-Content -Raw '%~f0'))) -NoNewScope}"`) DO @(
IF "%%A"=="MVN_CMD" (set __MVNW_CMD__=%%B) ELSE IF "%%B"=="" (echo %%A) ELSE (echo %%A=%%B)
)
@SET PSModulePath=%__MVNW_PSMODULEP_SAVE%
@SET __MVNW_PSMODULEP_SAVE=
@SET __MVNW_ARG0_NAME__=
@SET MVNW_USERNAME=
@SET MVNW_PASSWORD=
@IF NOT "%__MVNW_CMD__%"=="" ("%__MVNW_CMD__%" %*)
@echo Cannot start maven from wrapper >&2 && exit /b 1
@GOTO :EOF
: end batch / begin powershell #>
$ErrorActionPreference = "Stop"
if ($env:MVNW_VERBOSE -eq "true") {
$VerbosePreference = "Continue"
}
# calculate distributionUrl, requires .mvn/wrapper/maven-wrapper.properties
$distributionUrl = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionUrl
if (!$distributionUrl) {
Write-Error "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties"
}
switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) {
"maven-mvnd-*" {
$USE_MVND = $true
$distributionUrl = $distributionUrl -replace '-bin\.[^.]*$',"-windows-amd64.zip"
$MVN_CMD = "mvnd.cmd"
break
}
default {
$USE_MVND = $false
$MVN_CMD = $script -replace '^mvnw','mvn'
break
}
}
# apply MVNW_REPOURL and calculate MAVEN_HOME
# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-<version>,maven-mvnd-<version>-<platform>}/<hash>
if ($env:MVNW_REPOURL) {
$MVNW_REPO_PATTERN = if ($USE_MVND -eq $False) { "/org/apache/maven/" } else { "/maven/mvnd/" }
$distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace "^.*$MVNW_REPO_PATTERN",'')"
}
$distributionUrlName = $distributionUrl -replace '^.*/',''
$distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$',''
$MAVEN_M2_PATH = "$HOME/.m2"
if ($env:MAVEN_USER_HOME) {
$MAVEN_M2_PATH = "$env:MAVEN_USER_HOME"
}
if (-not (Test-Path -Path $MAVEN_M2_PATH)) {
New-Item -Path $MAVEN_M2_PATH -ItemType Directory | Out-Null
}
$MAVEN_WRAPPER_DISTS = $null
if ((Get-Item $MAVEN_M2_PATH).Target[0] -eq $null) {
$MAVEN_WRAPPER_DISTS = "$MAVEN_M2_PATH/wrapper/dists"
} else {
$MAVEN_WRAPPER_DISTS = (Get-Item $MAVEN_M2_PATH).Target[0] + "/wrapper/dists"
}
$MAVEN_HOME_PARENT = "$MAVEN_WRAPPER_DISTS/$distributionUrlNameMain"
$MAVEN_HOME_NAME = ([System.Security.Cryptography.SHA256]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join ''
$MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME"
if (Test-Path -Path "$MAVEN_HOME" -PathType Container) {
Write-Verbose "found existing MAVEN_HOME at $MAVEN_HOME"
Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD"
exit $?
}
if (! $distributionUrlNameMain -or ($distributionUrlName -eq $distributionUrlNameMain)) {
Write-Error "distributionUrl is not valid, must end with *-bin.zip, but found $distributionUrl"
}
# prepare tmp dir
$TMP_DOWNLOAD_DIR_HOLDER = New-TemporaryFile
$TMP_DOWNLOAD_DIR = New-Item -Itemtype Directory -Path "$TMP_DOWNLOAD_DIR_HOLDER.dir"
$TMP_DOWNLOAD_DIR_HOLDER.Delete() | Out-Null
trap {
if ($TMP_DOWNLOAD_DIR.Exists) {
try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null }
catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" }
}
}
New-Item -Itemtype Directory -Path "$MAVEN_HOME_PARENT" -Force | Out-Null
# Download and Install Apache Maven
Write-Verbose "Couldn't find MAVEN_HOME, downloading and installing it ..."
Write-Verbose "Downloading from: $distributionUrl"
Write-Verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName"
$webclient = New-Object System.Net.WebClient
if ($env:MVNW_USERNAME -and $env:MVNW_PASSWORD) {
$webclient.Credentials = New-Object System.Net.NetworkCredential($env:MVNW_USERNAME, $env:MVNW_PASSWORD)
}
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$webclient.DownloadFile($distributionUrl, "$TMP_DOWNLOAD_DIR/$distributionUrlName") | Out-Null
# If specified, validate the SHA-256 sum of the Maven distribution zip file
$distributionSha256Sum = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionSha256Sum
if ($distributionSha256Sum) {
if ($USE_MVND) {
Write-Error "Checksum validation is not supported for maven-mvnd. `nPlease disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties."
}
Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash
if ((Get-FileHash "$TMP_DOWNLOAD_DIR/$distributionUrlName" -Algorithm SHA256).Hash.ToLower() -ne $distributionSha256Sum) {
Write-Error "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised. If you updated your Maven version, you need to update the specified distributionSha256Sum property."
}
}
# unzip and move
Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null
# Find the actual extracted directory name (handles snapshots where filename != directory name)
$actualDistributionDir = ""
# First try the expected directory name (for regular distributions)
$expectedPath = Join-Path "$TMP_DOWNLOAD_DIR" "$distributionUrlNameMain"
$expectedMvnPath = Join-Path "$expectedPath" "bin/$MVN_CMD"
if ((Test-Path -Path $expectedPath -PathType Container) -and (Test-Path -Path $expectedMvnPath -PathType Leaf)) {
$actualDistributionDir = $distributionUrlNameMain
}
# If not found, search for any directory with the Maven executable (for snapshots)
if (!$actualDistributionDir) {
Get-ChildItem -Path "$TMP_DOWNLOAD_DIR" -Directory | ForEach-Object {
$testPath = Join-Path $_.FullName "bin/$MVN_CMD"
if (Test-Path -Path $testPath -PathType Leaf) {
$actualDistributionDir = $_.Name
}
}
}
if (!$actualDistributionDir) {
Write-Error "Could not find Maven distribution directory in extracted archive"
}
Write-Verbose "Found extracted Maven distribution directory: $actualDistributionDir"
Rename-Item -Path "$TMP_DOWNLOAD_DIR/$actualDistributionDir" -NewName $MAVEN_HOME_NAME | Out-Null
try {
Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null
} catch {
if (! (Test-Path -Path "$MAVEN_HOME" -PathType Container)) {
Write-Error "fail to move MAVEN_HOME"
}
} finally {
try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null }
catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" }
}
Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD"
...@@ -19,12 +19,11 @@ ...@@ -19,12 +19,11 @@
<dependency> <dependency>
<groupId>com.matchtech</groupId> <groupId>com.matchtech</groupId>
<artifactId>matchtech-common-swagger</artifactId> <artifactId>matchtech-common-swagger</artifactId>
<version>3.1.1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.matchtech</groupId> <groupId>cn.hutool</groupId>
<artifactId>matchtech-common-core</artifactId> <artifactId>hutool-all</artifactId>
</dependency> </dependency>
</dependencies> </dependencies>
......
package com.matchtech.common.dto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import io.swagger.v3.oas.annotations.media.Schema;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class BaseIdDto {
@Schema(description = "id", requiredMode = Schema.RequiredMode.REQUIRED)
private String id;
}
package com.matchtech.system.domain.dto; package com.matchtech.common.dto.login;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;
......
...@@ -9,6 +9,7 @@ public class SysLoginVo { ...@@ -9,6 +9,7 @@ public class SysLoginVo {
@Schema(description = "token") @Schema(description = "token")
private String token; private String token;
@Schema(description = "用户详情") @Schema(description = "用户详情")
private SysUserDetailVo sysUserDetailVo; private SysUserDetailVo sysUserDetailVo;
......
package com.matchtech.common.dto.login;
import lombok.Data;
@Data
public class TokenDto {
private String token;
private Long expire;
}
...@@ -34,6 +34,11 @@ ...@@ -34,6 +34,11 @@
<artifactId>matchtech-common-redis</artifactId> <artifactId>matchtech-common-redis</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.matchtech</groupId>
<artifactId>matchtech-common-dto</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>
...@@ -3,6 +3,8 @@ package com.matchtech.common.security.service; ...@@ -3,6 +3,8 @@ package com.matchtech.common.security.service;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import com.matchtech.common.dto.login.TokenDto;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -45,12 +47,12 @@ public class TokenService ...@@ -45,12 +47,12 @@ public class TokenService
/** /**
* 创建令牌 * 创建令牌
*/ */
public Map<String, Object> createToken(LoginUser loginUser) public TokenDto createToken(LoginUser loginUser)
{ {
String token = IdUtils.fastUUID(); String userKey = IdUtils.fastUUID();
Long userId = loginUser.getSysUser().getUserId(); String userId = loginUser.getUserid();
String userName = loginUser.getSysUser().getUserName(); String userName = loginUser.getUsername();
loginUser.setToken(token); loginUser.setToken(userKey);
loginUser.setUserid(userId); loginUser.setUserid(userId);
loginUser.setUsername(userName); loginUser.setUsername(userName);
loginUser.setIpaddr(IpUtils.getIpAddr()); loginUser.setIpaddr(IpUtils.getIpAddr());
...@@ -58,15 +60,15 @@ public class TokenService ...@@ -58,15 +60,15 @@ public class TokenService
// Jwt存储信息 // Jwt存储信息
Map<String, Object> claimsMap = new HashMap<String, Object>(); Map<String, Object> claimsMap = new HashMap<String, Object>();
claimsMap.put(SecurityConstants.USER_KEY, token); claimsMap.put(SecurityConstants.USER_KEY, userKey);
claimsMap.put(SecurityConstants.DETAILS_USER_ID, userId); claimsMap.put(SecurityConstants.DETAILS_USER_ID, userId);
claimsMap.put(SecurityConstants.DETAILS_USERNAME, userName); claimsMap.put(SecurityConstants.DETAILS_USERNAME, userName);
// 接口返回信息 // 接口返回信息
Map<String, Object> rspMap = new HashMap<String, Object>(); TokenDto tokenDto = new TokenDto();
rspMap.put("access_token", JwtUtils.createToken(claimsMap)); tokenDto.setExpire(TOKEN_EXPIRE_TIME);
rspMap.put("expires_in", TOKEN_EXPIRE_TIME); tokenDto.setToken(JwtUtils.createToken(claimsMap));
return rspMap; return tokenDto;
} }
/** /**
......
...@@ -19,7 +19,7 @@ public class SecurityUtils ...@@ -19,7 +19,7 @@ public class SecurityUtils
/** /**
* 获取用户ID * 获取用户ID
*/ */
public static Long getUserId() public static String getUserId()
{ {
return SecurityContextHolder.getUserId(); return SecurityContextHolder.getUserId();
} }
......
...@@ -55,7 +55,7 @@ public class SensitiveJsonSerializer extends JsonSerializer<String> implements C ...@@ -55,7 +55,7 @@ public class SensitiveJsonSerializer extends JsonSerializer<String> implements C
{ {
try try
{ {
Long userId = SecurityContextHolder.getUserId(); String userId = SecurityContextHolder.getUserId();
// 管理员不脱敏 // 管理员不脱敏
return !UserConstants.isAdmin(userId); return !UserConstants.isAdmin(userId);
} }
......
...@@ -33,7 +33,11 @@ ...@@ -33,7 +33,11 @@
<dependency> <dependency>
<groupId>io.springboot</groupId> <groupId>io.springboot</groupId>
<artifactId>knife4j-openapi3-ui</artifactId> <artifactId>knife4j-openapi3-ui</artifactId>
<version>4.5.0</version> </dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
</dependency> </dependency>
......
...@@ -12,6 +12,11 @@ ...@@ -12,6 +12,11 @@
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
</dependencies> </dependencies>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
......
...@@ -87,13 +87,6 @@ ...@@ -87,13 +87,6 @@
</dependency> </dependency>
<!-- Springdoc --> <!-- Springdoc -->
<!-- <dependency>-->
<!-- <groupId>org.springdoc</groupId>-->
<!-- <artifactId>springdoc-openapi-starter-webflux-ui</artifactId>-->
<!-- <version>${springdoc.version}</version>-->
<!-- </dependency>-->
<!-- Springdoc -->
<dependency> <dependency>
<groupId>org.springdoc</groupId> <groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-webflux-core</artifactId> <artifactId>springdoc-openapi-webflux-core</artifactId>
......
package com.matchtech.gateway.filter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
@Configuration
public class FilterConfig {
/**重新HttpServletRequest,让输入流body支持多次读取。
* @return
*/
@Bean
public FilterRegistrationBean<HttpServletRequestRewriteFilter> httpServletRequestRewriteFilterFilter() {
FilterRegistrationBean<HttpServletRequestRewriteFilter> registration = new FilterRegistrationBean<HttpServletRequestRewriteFilter>(
new HttpServletRequestRewriteFilter());
registration.addUrlPatterns("/a/*","/f/*");
registration.setName("httpServletRequestRewriteFilter");
registration.setOrder(1);
return registration;
}
/**登录拦截检验,并把把token和租户ID放进
* @return
*/
@Bean
public FilterRegistrationBean<LoginAndThreadLocalFilter> setThreadLocalFilter() {
FilterRegistrationBean<LoginAndThreadLocalFilter> registration = new FilterRegistrationBean<LoginAndThreadLocalFilter>(
new LoginAndThreadLocalFilter());
registration.addUrlPatterns("/a/*","/f/*");
registration.setName("loginAndThreadLocalFilter");
registration.setOrder(2);
return registration;
}
/**权限验证过滤器。
* @return
*/
@Bean
public FilterRegistrationBean<PermissionAuthenticationFilter> permissionAuthenticationFilter() {
FilterRegistrationBean<PermissionAuthenticationFilter> registration = new FilterRegistrationBean<PermissionAuthenticationFilter>(
new PermissionAuthenticationFilter());
registration.addUrlPatterns("/a/*","/f/*");
registration.setName("permissionAuthenticationFilter");
registration.setOrder(Ordered.LOWEST_PRECEDENCE);
return registration;
}
}
package com.matchtech.gateway.filter;
import com.matchtech.utils.servlet.MyHttpServletRequestWrapper;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
public class HttpServletRequestRewriteFilter implements Filter {
protected Logger logger = LoggerFactory.getLogger(getClass());
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
// 排除文件上传接口
if (null != httpServletRequest.getHeader("Content-Type")
&& httpServletRequest.getHeader("Content-Type").contains("multipart/form-data")) {
chain.doFilter(request, response);
return;
}
MyHttpServletRequestWrapper myHttpServletRequestWrapper = new MyHttpServletRequestWrapper(httpServletRequest);
chain.doFilter(myHttpServletRequestWrapper, response);
}
}
package com.matchtech.gateway.filter;
import com.matchtech.common.core.async.MyThreadLocal;
import com.matchtech.common.core.config.MatchtechProperties;
import com.matchtech.common.core.constants.GlobalCodeMessageEnum;
import com.matchtech.common.core.page.resp.ApiResponseUtils;
import com.matchtech.common.redis.constant.RedisConstants;
import com.matchtech.common.redis.service.RedisUtils;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import java.io.IOException;
import java.io.PrintWriter;
public class LoginAndThreadLocalFilter implements Filter {
protected Logger logger = LoggerFactory.getLogger(getClass());
private SysUserService sysUserService;
private MatchtechProperties matchtechProperties;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
ServletContext sc = filterConfig.getServletContext();
WebApplicationContext cxt = WebApplicationContextUtils.getWebApplicationContext(sc);
if (cxt != null) {
if (sysUserService == null) {
sysUserService = cxt.getBean(SysUserService.class);
}
if (matchtechProperties == null) {
matchtechProperties = cxt.getBean(MatchtechProperties.class);
}
}
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
String tenantId = httpServletRequest.getHeader("tenantId");
String token = httpServletRequest.getHeader("token");
// 登录校验拦截
if (!matchtechProperties.containPath(httpServletRequest.getRequestURI())) {
if (StringUtils.isEmpty(token) || "null".equals(token)) {
this.writer(httpServletResponse, ApiResponseUtils.fail(GlobalCodeMessageEnum.SYSTEM_TOKEN_NOT_FOUND));
return;
}
CurrentUserDTO currentUserDTO = RedisUtils.get(RedisConstants.SYS_USER_TOKEN_PREFIX + token);
if (null == currentUserDTO) {
this.writer(httpServletResponse, ApiResponseUtils.fail(GlobalCodeMessageEnum.SYSTEM_TOKEN_OVERDUE));
return;
}
// 重置token时间
RedisUtils.setExpire(RedisConstants.SYS_USER_TOKEN_PREFIX +token, Long.parseLong(matchtechProperties.getTokenTimeout()));
MyThreadLocal.setUserThreadLocal(currentUserDTO);
}
// 设置token和tenantId到ThreadLocal
MyThreadLocal.setToken(httpServletRequest.getHeader("token"));
MyThreadLocal.setTenantId(httpServletRequest.getHeader("tenantId"));
chain.doFilter(request, response);
//清除ThreadLocal
MyThreadLocal.remove();
}
private void writer(HttpServletResponse httpServletResponse, Object value) throws IOException {
httpServletResponse.setContentType("application/json");
httpServletResponse.setCharacterEncoding("UTF-8");
PrintWriter printWriter = httpServletResponse.getWriter();
printWriter.print(value);
printWriter.flush();
printWriter.close();
}
}
package com.matchtech.gateway.filter;
import com.matchtech.config.MatchtechProperties;
import com.matchtech.service.SysRoleMenuService;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import java.io.IOException;
import java.io.PrintWriter;
public class PermissionAuthenticationFilter implements Filter {
protected Logger logger = LoggerFactory.getLogger(getClass());
private SysRoleMenuService sysRoleMenuService;
private MatchtechProperties matchtechProperties;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
ServletContext sc = filterConfig.getServletContext();
WebApplicationContext cxt = WebApplicationContextUtils.getWebApplicationContext(sc);
if (cxt != null) {
if (sysRoleMenuService == null) {
sysRoleMenuService = cxt.getBean(SysRoleMenuService.class);
}
if (matchtechProperties == null) {
matchtechProperties = cxt.getBean(MatchtechProperties.class);
}
}
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
// List<String> list = matchtechProperties.getWhite().get("authorityurls");
// list.addAll(matchtechProperties.getWhite().get("loginurls"));
// CurrentUserDTO sysUser = RedisUtils.get(RedisConstants.SYS_USER_TOKEN_PREFIX + MyThreadLocal.getToken());
// // 登录校验拦截
// if (null != sysUser && !Constants.SUPER_ADMIN_ID.equals(sysUser.getId()) && !list.contains(httpServletRequest.getRequestURI())) {
// Map<String, SysMenuVo> map = sysRoleMenuService.getMenuByToken(MyThreadLocal.getToken());
//权限注解 判断
//获取处理器方法
// HttpServletRequest httpRequest = (HttpServletRequest) request;
// Object handler = httpRequest.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE);
// if (handler instanceof HandlerMethod) {
// HandlerMethod handlerMethod = (HandlerMethod) handler;
// Method method = handlerMethod.getMethod();
// // 获取方法上的注解
// Permission annotation = method.getAnnotation(Permission.class);
// if (annotation != null) {
// String code = annotation.code();
// if(StringUtils.isNotBlank(code)){
// if (null == map.get(code)) {
// this.writer(httpServletResponse, ApiResponseUtils.fail(GlobalCodeMessageEnum.SYSTEM_ERROR_NOT_AUTH));
// return;
// }
// }
// }
// }
// }
chain.doFilter(request, response);
}
private void writer(HttpServletResponse httpServletResponse, Object value) throws IOException {
httpServletResponse.setContentType("application/json");
httpServletResponse.setCharacterEncoding("UTF-8");
PrintWriter printWriter = httpServletResponse.getWriter();
printWriter.print(value);
printWriter.close();
}
}
# Tomcat # Tomcat
server: server:
port: 8080 port: 9999
# Spring # Spring
spring: spring:
...@@ -23,6 +23,9 @@ spring: ...@@ -23,6 +23,9 @@ spring:
# 共享配置 # 共享配置
shared-configs: shared-configs:
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
extension-configs:
- data-id: matchtech-gateway-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
refresh: true
sentinel: sentinel:
# 取消控制台懒加载 # 取消控制台懒加载
eager: true eager: true
......
package com.matchtech.system.controller; package com.matchtech.system.controller;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.matchtech.common.core.constants.GlobalCodeMessageEnum;
import com.matchtech.common.core.domain.R;
import com.matchtech.common.core.page.resp.ApiResponseEntity;
import com.matchtech.common.core.page.resp.ApiResponseUtils;
import com.matchtech.common.core.utils.StringUtils;
import com.matchtech.common.core.web.controller.BaseController; import com.matchtech.common.core.web.controller.BaseController;
import com.matchtech.common.dto.BaseIdDto;
import com.matchtech.common.dto.login.SysUserDetailVo;
import com.matchtech.system.api.domain.SysUser;
import com.matchtech.system.api.model.LoginUser;
import com.matchtech.system.entity.SysUserEntity;
import com.matchtech.system.service.SysUserService; import com.matchtech.system.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.Set;
@Tag(name = "系统用户管理") @Tag(name = "系统用户管理")
@RestController @RestController
@RequestMapping(value = "${matchtech.adminPath}/sys/user") @RequestMapping(value = "${matchtech.adminPath}/sys/user")
...@@ -16,4 +34,31 @@ public class SysUserController extends BaseController { ...@@ -16,4 +34,31 @@ public class SysUserController extends BaseController {
@Autowired @Autowired
private SysUserService sysUserService; private SysUserService sysUserService;
@Operation(summary = "用户详情")
@PostMapping("/detail")
public ApiResponseEntity<SysUserEntity> detail(@RequestBody BaseIdDto baseIdDto) {
return ApiResponseUtils.success(sysUserService.getById(baseIdDto.getId()));
}
@GetMapping("/info/{userId}")
public ApiResponseEntity<LoginUser> info(@PathVariable("userId") String userId)
{
SysUserEntity sysUser = sysUserService.getOne(Wrappers.lambdaQuery(SysUserEntity.class).eq(SysUserEntity::getLoginName,userId),Boolean.FALSE);
if (StringUtils.isNull(sysUser))
{
return ApiResponseEntity.fail(GlobalCodeMessageEnum.SYSTEM_USER_NOT_FOUND);
}
// // 角色集合
// Set<String> roles = permissionService.getRolePermission(sysUser);
// // 权限集合
// Set<String> permissions = permissionService.getMenuPermission(sysUser);
LoginUser sysUserVo = new LoginUser();
sysUserVo.setUserid(sysUser.getId());
sysUserVo.setUsername(sysUser.getName());
//sysUserVo.setSysUser(sysUser);
//sysUserVo.setRoles(roles);
//sysUserVo.setPermissions(permissions);
return ApiResponseEntity.success(sysUserVo);
}
} }
...@@ -4,7 +4,7 @@ import cn.hutool.core.util.HexUtil; ...@@ -4,7 +4,7 @@ import cn.hutool.core.util.HexUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.matchtech.common.core.async.MyThreadLocal; import com.matchtech.common.core.thread.MyThreadLocal;
import com.matchtech.common.core.config.MatchtechProperties; import com.matchtech.common.core.config.MatchtechProperties;
import com.matchtech.common.core.constants.Constants; import com.matchtech.common.core.constants.Constants;
import com.matchtech.common.core.constants.GlobalCodeMessageEnum; import com.matchtech.common.core.constants.GlobalCodeMessageEnum;
...@@ -15,12 +15,12 @@ import com.matchtech.common.core.utils.sign.RsaUtil; ...@@ -15,12 +15,12 @@ import com.matchtech.common.core.utils.sign.RsaUtil;
import com.matchtech.common.dto.login.SysLoginVo; import com.matchtech.common.dto.login.SysLoginVo;
import com.matchtech.common.redis.constant.RedisConstants; import com.matchtech.common.redis.constant.RedisConstants;
import com.matchtech.common.redis.service.RedisUtils; import com.matchtech.common.redis.service.RedisUtils;
import com.matchtech.common.security.service.TokenService;
import com.matchtech.system.convertors.SysUserConvertor; import com.matchtech.system.convertors.SysUserConvertor;
import com.matchtech.system.domain.dto.SysLoginDto; import com.matchtech.common.dto.login.SysLoginDto;
import com.matchtech.system.entity.SysUserEntity; import com.matchtech.system.entity.SysUserEntity;
import com.matchtech.system.mapper.SysUserMapper; import com.matchtech.system.mapper.SysUserMapper;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
...@@ -47,6 +47,8 @@ public class SysUserService extends ServiceImpl<SysUserMapper, SysUserEntity> { ...@@ -47,6 +47,8 @@ public class SysUserService extends ServiceImpl<SysUserMapper, SysUserEntity> {
private long maxLoginFailedAttempts; private long maxLoginFailedAttempts;
@Value("${matchtech.user.lock-duration}") @Value("${matchtech.user.lock-duration}")
private int lockDuration; private int lockDuration;
@Autowired
private TokenService tokenService;
@Transactional @Transactional
public SysLoginVo login(SysLoginDto loginDto) { public SysLoginVo login(SysLoginDto loginDto) {
...@@ -178,28 +180,6 @@ public class SysUserService extends ServiceImpl<SysUserMapper, SysUserEntity> { ...@@ -178,28 +180,6 @@ public class SysUserService extends ServiceImpl<SysUserMapper, SysUserEntity> {
return RedisUtils.get(RedisConstants.USER_LOGIN_LOCKED + userId) != null; return RedisUtils.get(RedisConstants.USER_LOGIN_LOCKED + userId) != null;
} }
// @Transactional
// public void updatePassword(SysUpdatePasswordDto updatePasswordDto) {
// SysUserEntity currentUser = getCurrentUser();
// if (!validatePassword(updatePasswordDto.getOldPassword(), currentUser.getPassword())) {
// throw new GlobalException(GlobalCodeMessageEnum.SYS_USER_OLD_PASSWORD_ERROR);
// }
// LambdaUpdateWrapper<SysUserEntity> updateWrapper = new LambdaUpdateWrapper<>();
// updateWrapper.set(SysUserEntity::getPassword, RsaUtil.encrypt(updatePasswordDto.getNewPassword()))
// .eq(SysUserEntity::getId, currentUser.getId());
// sysUserMapper.update(null, updateWrapper);
// }
// @Transactional
// public void resetPassword(SysResetPasswordDto sysResetPasswordDto) {
//
// LambdaUpdateWrapper<SysUserEntity> updateWrapper = new LambdaUpdateWrapper<>();
// updateWrapper.set(SysUserEntity::getPassword, RsaUtil.encrypt(sysResetPasswordDto.getNewPassword()))
// .eq(SysUserEntity::getId, sysResetPasswordDto.getUserId());
// sysUserMapper.update(null, updateWrapper);
// }
/** /**
* 获取当前登录用户 * 获取当前登录用户
*/ */
...@@ -262,76 +242,6 @@ public class SysUserService extends ServiceImpl<SysUserMapper, SysUserEntity> { ...@@ -262,76 +242,6 @@ public class SysUserService extends ServiceImpl<SysUserMapper, SysUserEntity> {
// return password.equals(HexUtil.encodeHexStr(salt) + HexUtil.encodeHexStr(hashPassword)); // return password.equals(HexUtil.encodeHexStr(salt) + HexUtil.encodeHexStr(hashPassword));
return RsaUtil.decrypt(plainPassword).equals(RsaUtil.decrypt(password)); return RsaUtil.decrypt(plainPassword).equals(RsaUtil.decrypt(password));
} }
// /**
// * 批量启用/禁用
// * @param req 参数
// */
// @Transactional(rollbackFor = Exception.class)
// public void changeStatus(SysUserReq.ChangeStatus req) {
// super.update(null, Wrappers.lambdaUpdate(SysUserEntity.class)
// .set(SysUserEntity::getLoginFlag, req.getStatus())
// .in(SysUserEntity::getId, req.getIds()));
// //清除登录信息
// if (Constants.NO.equals(req.getStatus())) {
// for (String id : req.getIds()) {
// String token = RedisUtils.get(RedisConstants.SYS_USER_ID_TOKEN_PREFIX + id);
// if (StrUtil.isNotBlank(token)) {
// RedisUtils.delete(RedisConstants.SYS_USER_TOKEN_PREFIX + token);
// }
// RedisUtils.delete(RedisConstants.SYS_USER_ID_TOKEN_PREFIX + id);
// }
// }
// }
// /**
// * 通过工号查询员工信息
// * @param managerId 工号
// * @return 员工信息
// */
// public EmployeeInfoVo getEmployeeInfoByManagerId(String managerId) {
// return sysUserMapper.selectEmployeeInfoById(managerId);
// }
// /**
// * 根据机构ID查询员工信息
// * @param officeId 机构ID
// * @return 员工信息列表
// */
// public List<EmployeeInfoVo> getEmployeeInfoByOfficeId(String officeId) {
// return sysUserMapper.selectEmployeeInfoByOfficeId(officeId);
// }
// /**
// * 根据机构ID和筛选条件查询员工信息
// * @param req 筛选条件
// * @return 员工信息列表
// */
// public PageVO<EmployeeInfoVo> getEmployeeInfoByOfficeIdWithFilter(PageReqDto<EmployeeInfoVo> req) {
// EmployeeInfoVo employeeInfoVo = req.getSearch();
// Page page = sysUserMapper.selectEmployeeInfoByOfficeIdWithFilter(MyBatisUtils.buildPage(req),employeeInfoVo);
// return new PageVO(page, page.getRecords());
// }
// /**
// * 根据角色ID获取用户ID列表
// */
// public List<String> listUserIdByRoleId(String roleId) {
// if (roleId == null){
// return new ArrayList<>();
// }
// return sysUserRoleService.getUserListByRoleId(roleId);
// }
// /**
// * 根据角色ID列表获取用户ID列表
// */
// public List<String> listUserIdByRoleIds(List<String> roleIds) {
// if (roleIds == null || roleIds.isEmpty()){
// return new ArrayList<>();
// }
// return sysUserRoleService.getUserListByRoleIds(roleIds);
// }
} }
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
<!-- Spring生态核心版本 --> <!-- Spring生态核心版本 -->
<spring-boot.version>3.1.1</spring-boot.version> <spring-boot.version>3.1.1</spring-boot.version>
<spring-cloud.version>2022.0.4</spring-cloud.version> <spring-cloud.version>2022.0.4</spring-cloud.version>
<spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version> <spring-cloud-alibaba.version>2022.0.0.2</spring-cloud-alibaba.version>
<spring-boot-admin.version>3.1.1</spring-boot-admin.version> <spring-boot-admin.version>3.1.1</spring-boot-admin.version>
<!-- 数据访问层版本 --> <!-- 数据访问层版本 -->
...@@ -265,6 +265,24 @@ ...@@ -265,6 +265,24 @@
<version>${matchtech.version}</version> <version>${matchtech.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.matchtech</groupId>
<artifactId>matchtech-common-dto</artifactId>
<version>${matchtech.version}</version>
</dependency>
<dependency>
<groupId>io.springboot</groupId>
<artifactId>knife4j-openapi3-ui</artifactId>
<version>4.5.0</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>2.2.20</version>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
......
2025年12月30日 2025年12月29日
2025年12月30日 2025年12月29日
springboot 降版本到3.1.1,其他也降版本 springboot 降版本到3.1.1,其他也降版本
授权 2025年12月30日
账号密码登录认证
swagger
单点登录 单点登录
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment