增加LoginUserContextHolder获取当前登录人对象
This commit is contained in:
+1
-3
@@ -15,9 +15,7 @@ import org.springframework.security.oauth2.provider.token.TokenStore;
|
||||
|
||||
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.*;
|
||||
|
||||
/**
|
||||
* 认证授权相关工具类
|
||||
|
||||
+29
@@ -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();
|
||||
}
|
||||
}
|
||||
+2
-34
@@ -6,6 +6,7 @@ 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 com.central.common.utils.LoginUserUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.security.authentication.AnonymousAuthenticationToken;
|
||||
@@ -64,42 +65,9 @@ public class TokenArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
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);
|
||||
|
||||
SysUser user = null;
|
||||
if (StrUtil.isBlank(username)) {
|
||||
log.warn("resolveArgument error username is empty");
|
||||
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) {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
+69
@@ -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;
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.central.common.filter;
|
||||
|
||||
import com.central.common.context.LoginUserContextHolder;
|
||||
import com.central.common.model.SysUser;
|
||||
import com.central.common.utils.LoginUserUtils;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 获取当前登录人过滤器
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2022/6/26
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@ConditionalOnClass(Filter.class)
|
||||
@Order
|
||||
public class LoginUserFilter extends OncePerRequestFilter {
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
|
||||
FilterChain filterChain) throws IOException, ServletException {
|
||||
try {
|
||||
SysUser user = LoginUserUtils.getCurrentUser(request, false);
|
||||
LoginUserContextHolder.setUser(user);
|
||||
filterChain.doFilter(request, response);
|
||||
} finally {
|
||||
LoginUserContextHolder.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -1,2 +1,3 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.central.common.filter.TenantFilter
|
||||
com.central.common.filter.TenantFilter, \
|
||||
com.central.common.filter.LoginUserFilter
|
||||
@@ -20,10 +20,19 @@ http://localhost:8093/test/notAuth
|
||||
|
||||
|
||||
### 3.2. 测试接口二
|
||||
通过 @LoginUser 获取当前登录人
|
||||
|
||||
http://localhost:8093/test/auth?access_token=xxx
|
||||
> - xxx 需替换为正确的 access_token
|
||||
> - 可以通过修改 `bootstrap.yml` 文件中的 `zlt.security.ignore.httpUrls` 参数添加排除校验的url。
|
||||
|
||||
|
||||
### 3.3. 测试接口三
|
||||
通过 LoginUserContextHolder 获取当前登录人
|
||||
|
||||
http://localhost:8093/test/auth2?access_token=xxx
|
||||
> - xxx 需替换为正确的 access_token
|
||||
|
||||
|
||||
## 四、获取access_token
|
||||
可使用任意授权模式获取;
|
||||
|
||||
@@ -15,6 +15,10 @@
|
||||
<groupId>com.zlt</groupId>
|
||||
<artifactId>zlt-config</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.zlt</groupId>
|
||||
<artifactId>zlt-common-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.zlt</groupId>
|
||||
<artifactId>zlt-auth-client-spring-boot-starter</artifactId>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.zlt.controller;
|
||||
|
||||
import com.central.common.annotation.LoginUser;
|
||||
import com.central.common.context.LoginUserContextHolder;
|
||||
import com.central.common.model.SysUser;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -16,11 +17,22 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
@Slf4j
|
||||
@RestController
|
||||
public class TestController {
|
||||
/**
|
||||
* 通过 @LoginUser 获取当前登录人
|
||||
*/
|
||||
@GetMapping("/test/auth")
|
||||
public String auth(@LoginUser SysUser user) {
|
||||
return "auth:" + user.getUsername();
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过 LoginUserContextHolder 获取当前登录人
|
||||
*/
|
||||
@GetMapping("/test/auth2")
|
||||
public String auth() {
|
||||
return "auth2:" + LoginUserContextHolder.getUser().getUsername();
|
||||
}
|
||||
|
||||
@GetMapping("/test/notAuth")
|
||||
public String notAuth() {
|
||||
return "notAuth:ok";
|
||||
|
||||
Reference in New Issue
Block a user