♻️ Refactoring code. 扩展 PigxUser 增加扩展属性 ,注意清空 Redis
This commit is contained in:
1 parent
e64ce41167
commit
e356e02f31
6 files changed
+28
-27
No files matched your search
@@ -59,8 +59,8 @@
|
||||
<artifactId>spring-security-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security.oauth</groupId>
|
||||
<artifactId>spring-security-oauth2</artifactId>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-oauth2-core</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
+8
-17
@@ -19,10 +19,12 @@
|
||||
|
||||
package com.pig4cloud.pigx.common.log.util;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import cn.hutool.extra.servlet.ServletUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.pig4cloud.pigx.admin.api.dto.SysLogDTO;
|
||||
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
|
||||
import org.springframework.expression.EvaluationContext;
|
||||
@@ -31,7 +33,7 @@ import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||
import org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
@@ -62,28 +64,17 @@ public class SysLogUtils {
|
||||
return sysLog;
|
||||
}
|
||||
|
||||
public SysLogDTO getSysLog(HttpServletRequest request, String username) {
|
||||
SysLogDTO sysLog = new SysLogDTO();
|
||||
sysLog.setCreateBy(username);
|
||||
sysLog.setLogType(LogTypeEnum.NORMAL.getType());
|
||||
sysLog.setRemoteAddr(ServletUtil.getClientIP(request));
|
||||
sysLog.setRequestUri(URLUtil.getPath(request.getRequestURI()));
|
||||
sysLog.setMethod(request.getMethod());
|
||||
sysLog.setUserAgent(request.getHeader("user-agent"));
|
||||
sysLog.setParams(HttpUtil.toParams(request.getParameterMap()));
|
||||
sysLog.setServiceId(getClientId());
|
||||
return sysLog;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取客户端
|
||||
* @return clientId
|
||||
*/
|
||||
private String getClientId() {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication instanceof OAuth2Authentication) {
|
||||
OAuth2Authentication auth2Authentication = (OAuth2Authentication) authentication;
|
||||
return auth2Authentication.getOAuth2Request().getClientId();
|
||||
|
||||
Object principal = authentication.getPrincipal();
|
||||
if (principal instanceof OAuth2AuthenticatedPrincipal) {
|
||||
OAuth2AuthenticatedPrincipal auth2Authentication = (OAuth2AuthenticatedPrincipal) principal;
|
||||
return MapUtil.getStr(auth2Authentication.getAttributes(), SecurityConstants.CLIENT_ID);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
+7
-1
@@ -1,5 +1,6 @@
|
||||
package com.pig4cloud.pigx.common.security.component;
|
||||
|
||||
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
|
||||
import com.pig4cloud.pigx.common.core.util.SpringContextHolder;
|
||||
import com.pig4cloud.pigx.common.security.service.PigxUser;
|
||||
import com.pig4cloud.pigx.common.security.service.PigxUserDetailsService;
|
||||
@@ -69,7 +70,12 @@ public class PigxCustomOpaqueTokenIntrospector implements OpaqueTokenIntrospecto
|
||||
catch (Exception ex) {
|
||||
log.error("资源服务器 introspect Token error {}", ex.getLocalizedMessage());
|
||||
}
|
||||
return (PigxUser) userDetails;
|
||||
|
||||
// 注入客户端信息,方便上下文中获取
|
||||
PigxUser pigxUser = (PigxUser) userDetails;
|
||||
Objects.requireNonNull(pigxUser).getAttributes().put(SecurityConstants.CLIENT_ID,
|
||||
oldAuthorization.getRegisteredClientId());
|
||||
return pigxUser;
|
||||
}
|
||||
|
||||
}
|
||||
+6
-1
@@ -38,6 +38,11 @@ public class PigxUser extends User implements OAuth2AuthenticatedPrincipal {
|
||||
|
||||
private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
|
||||
|
||||
/**
|
||||
* 扩展属性,方便存放oauth 上下文相关信息
|
||||
*/
|
||||
private final Map<String, Object> attributes = new HashMap<>();
|
||||
|
||||
/**
|
||||
* 用户ID
|
||||
*/
|
||||
@@ -136,7 +141,7 @@ public class PigxUser extends User implements OAuth2AuthenticatedPrincipal {
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getAttributes() {
|
||||
return new HashMap<>();
|
||||
return this.attributes;
|
||||
}
|
||||
|
||||
}
|
||||
+1
-6
@@ -23,7 +23,6 @@ import com.pig4cloud.pigx.common.core.util.R;
|
||||
import feign.FeignException;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.validation.BindException;
|
||||
@@ -33,17 +32,12 @@ import org.springframework.web.bind.annotation.*;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 全局异常处理器结合sentinel 全局异常处理器不能作用在 oauth server https://gitee.com/log4j/pig/issues/I1M2TJ
|
||||
* </p>
|
||||
*
|
||||
* @author lengleng
|
||||
* @date 2020-06-29
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RestControllerAdvice
|
||||
@ConditionalOnExpression("!'${security.oauth2.client.clientId}'.isEmpty()")
|
||||
public class GlobalBizExceptionHandler {
|
||||
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
@@ -56,6 +50,7 @@ public class GlobalBizExceptionHandler {
|
||||
@ExceptionHandler(Exception.class)
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
public R handleGlobalException(Exception e) {
|
||||
|
||||
log.error("全局异常信息 ex={}", e.getMessage(), e);
|
||||
|
||||
// 业务异常交由 sentinel 记录
|
||||
|
||||
@@ -106,6 +106,10 @@
|
||||
<groupId>com.pig4cloud</groupId>
|
||||
<artifactId>pigx-common-security</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security.oauth</groupId>
|
||||
<artifactId>spring-security-oauth2</artifactId>
|
||||
</dependency>
|
||||
<!--sentinel-->
|
||||
<dependency>
|
||||
<groupId>com.pig4cloud</groupId>
|
||||
|
||||
Reference in new issue
Block a user