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

com.rivers.core.util.SysLogUtils Maven / Gradle / Ivy

/*
 *  Copyright (c) 2019-2020, 冷冷 ([email protected]).
 *  

* Licensed under the GNU Lesser General Public License 3.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

* https://www.gnu.org/licenses/lgpl.html *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.rivers.core.util; import cn.hutool.core.util.URLUtil; import cn.hutool.extra.servlet.ServletUtil; import cn.hutool.http.HttpUtil; import com.rivers.core.log.entity.SysLog; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.oauth2.provider.OAuth2Authentication; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.util.Objects; /** * 系统日志工具类 * * @author riversking */ public class SysLogUtils { public static SysLog getSysLog() { HttpServletRequest request = ((ServletRequestAttributes) Objects .requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); SysLog sysLog = new SysLog(); sysLog.setCreateBy(Objects.requireNonNull(getUsername())); sysLog.setType("0"); 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 static String getClientId() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication instanceof OAuth2Authentication) { OAuth2Authentication auth2Authentication = (OAuth2Authentication) authentication; return auth2Authentication.getOAuth2Request().getClientId(); } return null; } /** * 获取用户名称 * * @return username */ private static String getUsername() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication == null) { return null; } return authentication.getName(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy