From 93b3a605a02c840c00c3418fa383b1db08541088 Mon Sep 17 00:00:00 2001 From: aoshiguchen <1052045476@qq.com> Date: Tue, 2 Aug 2022 22:21:15 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95/=E7=99=BB=E5=87=BA=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=AE=8C=E5=96=84.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../proxy/server/base/rest/SystemContext.java | 48 ++++++++++++++ .../server/base/rest/SystemContextHolder.java | 22 +++---- .../rest/interceptor/BaseAuthInterceptor.java | 15 +++-- .../proxy/server/service/UserService.java | 11 +++- .../neutrino/proxy/server/util/HttpUtil.java | 64 +++++++++++++++++++ 5 files changed, 141 insertions(+), 19 deletions(-) create mode 100644 neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/SystemContext.java create mode 100644 neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/util/HttpUtil.java diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/SystemContext.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/SystemContext.java new file mode 100644 index 00000000..0817bf06 --- /dev/null +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/SystemContext.java @@ -0,0 +1,48 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.proxy.server.base.rest; + +import fun.asgc.neutrino.proxy.server.dal.entity.UserDO; +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * + * @author: aoshiguchen + * @date: 2022/8/2 + */ +@Accessors(chain = true) +@Data +public class SystemContext { + /** + * 当前用户 + */ + private UserDO user; + /** + * 鉴权token + */ + private String token; + /** + * 客户端ip + */ + private String ip; +} diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/SystemContextHolder.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/SystemContextHolder.java index 396f53c4..a0647e3d 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/SystemContextHolder.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/SystemContextHolder.java @@ -29,27 +29,25 @@ import fun.asgc.neutrino.proxy.server.dal.entity.UserDO; * @date: 2022/8/2 */ public class SystemContextHolder { - private static final ThreadLocal userHolder = new ThreadLocal<>(); - private static final ThreadLocal tokenHolder = new ThreadLocal<>(); + private static final ThreadLocal systemContextHolder = new ThreadLocal<>(); public static void remove() { - userHolder.remove(); - tokenHolder.remove(); + systemContextHolder.remove(); } - public static void setUser(UserDO user) { - userHolder.set(user); + public static void set(SystemContext systemContext) { + systemContextHolder.set(systemContext); } public static UserDO getUser() { - return userHolder.get(); - } - - public static void setToken(String token) { - tokenHolder.set(token); + return systemContextHolder.get().getUser(); } public static String getToken() { - return tokenHolder.get(); + return systemContextHolder.get().getToken(); + } + + public static String getIp() { + return systemContextHolder.get().getIp(); } } diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/interceptor/BaseAuthInterceptor.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/interceptor/BaseAuthInterceptor.java index d790c8da..6cb56aed 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/interceptor/BaseAuthInterceptor.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/interceptor/BaseAuthInterceptor.java @@ -23,15 +23,14 @@ package fun.asgc.neutrino.proxy.server.base.rest.interceptor; import fun.asgc.neutrino.core.util.BeanManager; import fun.asgc.neutrino.core.util.StringUtil; +import fun.asgc.neutrino.core.web.context.HttpContextHolder; import fun.asgc.neutrino.core.web.context.HttpRequestWrapper; import fun.asgc.neutrino.core.web.context.HttpResponseWrapper; import fun.asgc.neutrino.core.web.interceptor.HandlerInterceptor; -import fun.asgc.neutrino.proxy.server.base.rest.Authorization; -import fun.asgc.neutrino.proxy.server.base.rest.ExceptionConstant; -import fun.asgc.neutrino.proxy.server.base.rest.ServiceException; -import fun.asgc.neutrino.proxy.server.base.rest.SystemContextHolder; +import fun.asgc.neutrino.proxy.server.base.rest.*; import fun.asgc.neutrino.proxy.server.dal.entity.UserDO; import fun.asgc.neutrino.proxy.server.service.UserService; +import fun.asgc.neutrino.proxy.server.util.HttpUtil; import java.lang.reflect.Method; @@ -44,6 +43,10 @@ public class BaseAuthInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpRequestWrapper requestParser, HttpResponseWrapper responseWrapper, String route, Method targetMethod) throws Exception { + SystemContext systemContext = new SystemContext(); + SystemContextHolder.set(systemContext); + systemContext.setIp(HttpUtil.getIP(HttpContextHolder.getChannelHandlerContext(), requestParser)); + Authorization authorization = targetMethod.getAnnotation(Authorization.class); if (null == authorization || authorization.login()) { String authorize = requestParser.getHeaderValue("Authorize"); @@ -54,8 +57,8 @@ public class BaseAuthInterceptor implements HandlerInterceptor { if (null == userDO) { throw ServiceException.create(ExceptionConstant.USER_NOT_LOGIN); } - SystemContextHolder.setUser(userDO); - SystemContextHolder.setToken(authorize); + systemContext.setToken(authorize); + systemContext.setUser(userDO); } return true; diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/UserService.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/UserService.java index 93ad7143..ea6976a5 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/UserService.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/UserService.java @@ -77,7 +77,7 @@ public class UserService { // 新增用户登录日志 userLoginRecordMapper.add(new UserLoginRecordDO() .setUserId(userDO.getId()) - .setIp("111") + .setIp(SystemContextHolder.getIp()) .setToken(token) .setType(UserLoginRecordDO.TYPE_LOGIN) .setCreateTime(now) @@ -91,6 +91,15 @@ public class UserService { public void logout() { userTokenMapper.deleteByToken(SystemContextHolder.getToken()); + + // 新增用户登录日志 + userLoginRecordMapper.add(new UserLoginRecordDO() + .setUserId(SystemContextHolder.getUser().getId()) + .setIp(SystemContextHolder.getIp()) + .setToken(SystemContextHolder.getToken()) + .setType(UserLoginRecordDO.TYPE_LOGOUT) + .setCreateTime(new Date()) + ); } public UserDO findByToken(String token) { diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/util/HttpUtil.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/util/HttpUtil.java new file mode 100644 index 00000000..ac318f5b --- /dev/null +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/util/HttpUtil.java @@ -0,0 +1,64 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.proxy.server.util; + +import fun.asgc.neutrino.core.web.context.HttpRequestWrapper; +import io.netty.channel.ChannelHandlerContext; + +import java.net.InetAddress; +import java.net.UnknownHostException; + +/** + * + * @author: aoshiguchen + * @date: 2022/8/2 + */ +public class HttpUtil { + + /** + * 获取客户端IP + */ + public static String getIP(ChannelHandlerContext context, HttpRequestWrapper request) { + String ip = request.getHeaderValue("clientip"); // for UC browser + if (ip == null) { + ip = request.getHeaderValue("X-Real-IP"); + if (ip == null) { + ip = request.getHeaderValue("X-Forwarded-For"); + if (ip == null) { + ip = context.channel().remoteAddress().toString(); + if (ip.equals("127.0.0.1") || ip.equals("0:0:0:0:0:0:0:1")) { + //根据网卡取本机配置的IP + InetAddress inet = null; + try { + inet = InetAddress.getLocalHost(); + } catch (UnknownHostException e) { + e.printStackTrace(); + } + ip = inet.getHostAddress(); + } + } + } + } + return ip; + } + +}