🐛 Fixing a bug. close #I6L95T 日志管理无ip和客户端

This commit is contained in:
lbw 2023-03-14 13:46:13 +08:00
parent 33793be85e
commit b4a36f6a1b
7 changed files with 42 additions and 11 deletions

View File

@ -23,6 +23,7 @@ import com.pig4cloud.pig.common.core.constant.SecurityConstants;
import com.pig4cloud.pig.common.core.util.MsgUtils;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.core.util.SpringContextHolder;
import com.pig4cloud.pig.common.core.util.WebUtils;
import com.pig4cloud.pig.common.log.event.SysLogEvent;
import com.pig4cloud.pig.common.log.util.LogTypeEnum;
import com.pig4cloud.pig.common.log.util.SysLogUtils;
@ -75,6 +76,8 @@ public class PigAuthenticationFailureEventHandler implements AuthenticationFailu
Long endTime = System.currentTimeMillis();
logVo.setTime(endTime - startTime);
}
logVo.setServiceId(WebUtils.getClientId());
logVo.setCreateBy(username);
logVo.setUpdateBy(username);
SpringContextHolder.publishEvent(new SysLogEvent(logVo));

View File

@ -84,6 +84,8 @@ public class PigAuthenticationSuccessEventHandler implements AuthenticationSucce
Long endTime = System.currentTimeMillis();
logVo.setTime(endTime - startTime);
}
logVo.setServiceId(accessTokenAuthentication.getRegisteredClient().getClientId());
logVo.setCreateBy(userInfo.getName());
logVo.setUpdateBy(userInfo.getName());
SpringContextHolder.publishEvent(new SysLogEvent(logVo));

View File

@ -54,5 +54,9 @@
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-core</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -16,10 +16,12 @@
package com.pig4cloud.pig.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.pig.admin.api.entity.SysLog;
import com.pig4cloud.pig.common.core.constant.SecurityConstants;
import lombok.experimental.UtilityClass;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.expression.EvaluationContext;
@ -29,6 +31,7 @@ import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.http.HttpHeaders;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@ -56,9 +59,28 @@ public class SysLogUtils {
sysLog.setParams(HttpUtil.toParams(request.getParameterMap()));
sysLog.setCreateBy(getUsername());
sysLog.setUpdateBy(getUsername());
sysLog.setServiceId(getClientId());
return sysLog;
}
/**
* 获取客户端
* @return clientId
*/
private String getClientId() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
return null;
}
Object principal = authentication.getPrincipal();
if (principal instanceof OAuth2AuthenticatedPrincipal) {
OAuth2AuthenticatedPrincipal auth2Authentication = (OAuth2AuthenticatedPrincipal) principal;
return MapUtil.getStr(auth2Authentication.getAttributes(), SecurityConstants.CLIENT_ID);
}
return null;
}
/**
* 获取用户名称
* @return username

View File

@ -1,6 +1,7 @@
package com.pig4cloud.pig.common.security.component;
import cn.hutool.extra.spring.SpringUtil;
import com.pig4cloud.pig.common.core.constant.SecurityConstants;
import com.pig4cloud.pig.common.security.service.PigUser;
import com.pig4cloud.pig.common.security.service.PigUserDetailsService;
import lombok.RequiredArgsConstructor;
@ -71,7 +72,13 @@ public class PigCustomOpaqueTokenIntrospector implements OpaqueTokenIntrospector
catch (Exception ex) {
log.error("资源服务器 introspect Token error {}", ex.getLocalizedMessage());
}
return (PigUser) userDetails;
// 注入扩展属性,方便上下文获取客户端ID
PigUser user = (PigUser) userDetails;
Objects.requireNonNull(user)
.getAttributes()
.put(SecurityConstants.CLIENT_ID, oldAuthorization.getRegisteredClientId());
return user;
}
}

View File

@ -36,6 +36,8 @@ public class PigUser extends User implements OAuth2AuthenticatedPrincipal {
private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
private final Map<String, Object> attributes = new HashMap<>();
/**
* 用户ID
*/
@ -71,7 +73,7 @@ public class PigUser extends User implements OAuth2AuthenticatedPrincipal {
*/
@Override
public Map<String, Object> getAttributes() {
return new HashMap<>();
return this.attributes;
}
@Override

View File

@ -230,15 +230,6 @@
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-maven-plugin</artifactId>
<version>${spring.checkstyle.plugin}</version>
<executions>
<execution>
<phase>validate</phase>
<inherited>true</inherited>
<goals>
<goal>validate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>