All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.kangjianqun.fast.common.log.util.SysLogUtils Maven / Gradle / Ivy

There is a newer version: 1.0.3
Show newest version
package io.github.kangjianqun.fast.common.log.util;

import cn.hutool.core.util.URLUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.HttpUtil;
import io.github.kangjianqun.fast.common.log.event.ASysLog;
import lombok.experimental.UtilityClass;
import org.springframework.http.HttpHeaders;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * 系统日志工具类
 *
 * @author 康建群
 */
@UtilityClass
public class SysLogUtils {

    public ASysLog getSysLog() {
        HttpServletRequest request = ((ServletRequestAttributes) Objects
                .requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        ASysLog ASysLog = new ASysLog();
//		SysLog.setCreateBy(Objects.requireNonNull(getUsername()));
//		SysLog.setUpdateBy(Objects.requireNonNull(getUsername()));
        ASysLog.setType(LogTypeEnum.NORMAL.getType());
        ASysLog.setRemoteAddr(ServletUtil.getClientIP(request));
        ASysLog.setRequestUri(URLUtil.getPath(request.getRequestURI()));
        ASysLog.setMethod(request.getMethod());
        ASysLog.setUserAgent(request.getHeader(HttpHeaders.USER_AGENT));
        ASysLog.setParams(HttpUtil.toParams(request.getParameterMap()));
//        SysLog.setServiceId(getClientId(request));
        return ASysLog;
    }

//	/**
//	 * 获取客户端
//	 * @return clientId
//	 */
//	private String getClientId(HttpServletRequest request) {
//		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//		if (authentication instanceof OAuth2Authentication) {
//			OAuth2Authentication auth2Authentication = (OAuth2Authentication) authentication;
//			return auth2Authentication.getOAuth2Request().getClientId();
//		}
//		if (authentication instanceof UsernamePasswordAuthenticationToken) {
//			BasicAuthenticationConverter basicAuthenticationConverter = new BasicAuthenticationConverter();
//			UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = basicAuthenticationConverter
//					.convert(request);
//			if (usernamePasswordAuthenticationToken != null) {
//				return usernamePasswordAuthenticationToken.getName();
//			}
//		}
//		return null;
//	}

//    /**
//     * 获取用户名称
//     *
//     * @return username
//     */
//    private String getUsername() {
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//        if (authentication == null) {
//            return null;
//        }
//        return authentication.getName();
//    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy