登录/登出接口完善.
This commit is contained in:
+48
@@ -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;
|
||||
}
|
||||
+10
-12
@@ -29,27 +29,25 @@ import fun.asgc.neutrino.proxy.server.dal.entity.UserDO;
|
||||
* @date: 2022/8/2
|
||||
*/
|
||||
public class SystemContextHolder {
|
||||
private static final ThreadLocal<UserDO> userHolder = new ThreadLocal<>();
|
||||
private static final ThreadLocal<String> tokenHolder = new ThreadLocal<>();
|
||||
private static final ThreadLocal<SystemContext> 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();
|
||||
}
|
||||
}
|
||||
|
||||
+9
-6
@@ -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;
|
||||
|
||||
+10
-1
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user