feat: 添加登录设备ip相关功能

This commit is contained in:
landaiqing
2024-07-20 16:52:14 +08:00
parent f609e5b63d
commit 13ab70b70f
21 changed files with 1299 additions and 271 deletions

View File

@@ -0,0 +1,136 @@
package com.schisandra.auth.application.controller;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.schisandra.auth.application.convert.SchisandraAuthAddressDTOConverter;
import com.schisandra.auth.application.dto.SchisandraAuthAddressDTO;
import com.schisandra.auth.common.entity.Result;
import com.schisandra.auth.domain.bo.SchisandraAuthAddressBO;
import com.schisandra.auth.domain.service.SchisandraAuthAddressDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* 登录设备ip controller
*
* @author landaiqing
* @since 2024-07-20 15:33:38
*/
@RestController
@RequestMapping("/auth/address/")
@Slf4j
public class SchisandraAuthAddressController {
@Resource
private SchisandraAuthAddressDomainService schisandraAuthAddressDomainService;
/**
* 新增登录设备ip
*/
@RequestMapping("add")
public Result<Boolean> add(@RequestBody SchisandraAuthAddressDTO schisandraAuthAddressDTO) {
try {
if (log.isInfoEnabled()) {
log.info("SchisandraAuthAddressController.add.dto:{}", JSON.toJSONString(schisandraAuthAddressDTO));
}
Preconditions.checkNotNull(schisandraAuthAddressDTO.getId(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getUserId(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getIp(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getLocation(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getAgent(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getExtJson(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getCreatedBy(), "创建人不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getCreatedTime(), "创建时间不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getUpdateBy(), "更新人不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getUpdateTime(), "更新时间不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getIsDeleted(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getBrowser(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getOperatingSystem(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getBrowserVersion(), "不能为空");
SchisandraAuthAddressBO SchisandraAuthAddressBO = SchisandraAuthAddressDTOConverter.INSTANCE.convertDTOToBO(schisandraAuthAddressDTO);
return Result.ok(schisandraAuthAddressDomainService.add(SchisandraAuthAddressBO));
} catch (Exception e) {
log.error("SchisandraAuthAddressController.register.error:{}", e.getMessage(), e);
return Result.fail("新增登录设备ip失败");
}
}
/**
* 修改登录设备ip
*/
@RequestMapping("update")
public Result<Boolean> update(@RequestBody SchisandraAuthAddressDTO schisandraAuthAddressDTO) {
try {
if (log.isInfoEnabled()) {
log.info("SchisandraAuthAddressController.update.dto:{}", JSON.toJSONString(schisandraAuthAddressDTO));
}
Preconditions.checkNotNull(schisandraAuthAddressDTO.getId(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getUserId(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getIp(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getLocation(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getAgent(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getExtJson(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getCreatedBy(), "创建人不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getCreatedTime(), "创建时间不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getUpdateBy(), "更新人不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getUpdateTime(), "更新时间不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getIsDeleted(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getBrowser(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getOperatingSystem(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getBrowserVersion(), "不能为空");
SchisandraAuthAddressBO schisandraAuthAddressBO = SchisandraAuthAddressDTOConverter.INSTANCE.convertDTOToBO(schisandraAuthAddressDTO);
return Result.ok(schisandraAuthAddressDomainService.update(schisandraAuthAddressBO));
} catch (Exception e) {
log.error("SchisandraAuthAddressController.update.error:{}", e.getMessage(), e);
return Result.fail("更新登录设备ip信息失败");
}
}
/**
* 删除登录设备ip
*/
@RequestMapping("delete")
public Result<Boolean> delete(@RequestBody SchisandraAuthAddressDTO schisandraAuthAddressDTO) {
try {
if (log.isInfoEnabled()) {
log.info("SchisandraAuthAddressController.delete.dto:{}", JSON.toJSONString(schisandraAuthAddressDTO));
}
Preconditions.checkNotNull(schisandraAuthAddressDTO.getId(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getUserId(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getIp(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getLocation(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getAgent(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getExtJson(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getCreatedBy(), "创建人不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getCreatedTime(), "创建时间不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getUpdateBy(), "更新人不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getUpdateTime(), "更新时间不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getIsDeleted(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getBrowser(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getOperatingSystem(), "不能为空");
Preconditions.checkNotNull(schisandraAuthAddressDTO.getBrowserVersion(), "不能为空");
SchisandraAuthAddressBO schisandraAuthAddressBO = SchisandraAuthAddressDTOConverter.INSTANCE.convertDTOToBO(schisandraAuthAddressDTO);
return Result.ok(schisandraAuthAddressDomainService.delete(schisandraAuthAddressBO));
} catch (Exception e) {
log.error("SchisandraAuthAddressController.delete.error:{}", e.getMessage(), e);
return Result.fail("删除登录设备ip信息失败");
}
}
@PostMapping("getUserAddress")
public Result<SchisandraAuthAddressDTO> getUserAddress(@RequestParam("userId") String userId) {
Preconditions.checkNotNull(userId, "userId不能为空");
SchisandraAuthAddressBO schisandraAuthAddressBO = schisandraAuthAddressDomainService.queryByUserId(userId);
SchisandraAuthAddressDTO schisandraAuthAddressDTO = SchisandraAuthAddressDTOConverter.INSTANCE.convertBOToDTO(schisandraAuthAddressBO);
return Result.ok(schisandraAuthAddressDTO);
}
}

View File

@@ -13,16 +13,16 @@ import com.schisandra.auth.application.utils.CheckRouteCaptcha;
import com.schisandra.auth.common.entity.CaptchaResult;
import com.schisandra.auth.common.entity.Result;
import com.schisandra.auth.common.enums.UserRoleEnum;
import com.schisandra.auth.domain.bo.SchisandraAuthAddressBO;
import com.schisandra.auth.domain.bo.SchisandraAuthRolePermissionBO;
import com.schisandra.auth.domain.bo.SchisandraAuthUserBO;
import com.schisandra.auth.domain.redis.RedisUtil;
import com.schisandra.auth.domain.service.SchisandraAuthPermissionDomainService;
import com.schisandra.auth.domain.service.SchisandraAuthRoleDomainService;
import com.schisandra.auth.domain.service.SchisandraAuthRolePermissionDomainService;
import com.schisandra.auth.domain.service.SchisandraAuthUserDomainService;
import com.schisandra.auth.domain.service.*;
import com.schisandra.auth.domain.utils.IPUtil;
import com.schisandra.auth.infra.basic.entity.SchisandraAuthPermission;
import com.schisandra.auth.infra.basic.entity.SchisandraAuthRole;
import com.schisandra.auth.infra.basic.entity.SchisandraAuthUser;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
@@ -30,6 +30,7 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@@ -66,6 +67,8 @@ public class SchisandraAuthUserController {
@Resource
private SchisandraAuthPermissionDomainService schisandraAuthPermissionDomainService;
@Resource
private SchisandraAuthAddressDomainService schisandraAuthAddressDomainService;
/**
* @description: 注册
@@ -124,7 +127,7 @@ public class SchisandraAuthUserController {
* @date: 2024/5/31 1:19
*/
@PostMapping("login")
public CaptchaResult login(@RequestBody SchisandraAuthUserDTO schisandraAuthUserDTO) {
public CaptchaResult login(@RequestBody SchisandraAuthUserDTO schisandraAuthUserDTO, HttpServletRequest httpServletRequest) {
if (schisandraAuthUserDTO.getDeg() == null && schisandraAuthUserDTO.getToken() == null) {
return CaptchaResult.fail("验证失败!");
}
@@ -136,6 +139,21 @@ public class SchisandraAuthUserController {
SchisandraAuthUserBO schisandraAuthUserBO = SchisandraAuthUserDTOConverter.INSTANCE.convertDTOToBO(schisandraAuthUserDTO);
SchisandraAuthUserBO login = schisandraAuthUserDomainService.login(schisandraAuthUserBO);
SchisandraAuthUserDTO result = SchisandraAuthUserDTOConverter.INSTANCE.convertBOToDTO(login);
// 加入登录IP地址
String ip = IPUtil.getIp(httpServletRequest);
UserAgent userAgent = IPUtil.getUserAgent(httpServletRequest);
String ip2region = IPUtil.getIp2region(ip);
SchisandraAuthAddressBO schisandraAuthAddressBO = new SchisandraAuthAddressBO();
schisandraAuthAddressBO.setAgent(userAgent.toString());
schisandraAuthAddressBO.setUserId(result.getId());
schisandraAuthAddressBO.setIp(ip);
schisandraAuthAddressBO.setBrowser(userAgent.getBrowser().toString());
schisandraAuthAddressBO.setBrowserVersion(userAgent.getBrowserVersion().getVersion());
schisandraAuthAddressBO.setLocation(ip2region);
Boolean add = schisandraAuthAddressDomainService.add(schisandraAuthAddressBO);
assert add;
map.put("user", result);
if (login != null) {
if (StpUtil.isLogin(result.getId())) {
@@ -166,7 +184,7 @@ public class SchisandraAuthUserController {
* @date: 2024/6/9 0:54
*/
@PostMapping("loginByPhone")
public CaptchaResult loginByPhone(@RequestBody SchisandraAuthUserDTO schisandraAuthUserDTO) {
public CaptchaResult loginByPhone(@RequestBody SchisandraAuthUserDTO schisandraAuthUserDTO, HttpServletRequest httpServletRequest) {
if (schisandraAuthUserDTO.getDeg() == null && schisandraAuthUserDTO.getToken() == null) {
return CaptchaResult.fail("验证失败!");
}
@@ -183,6 +201,22 @@ public class SchisandraAuthUserController {
SchisandraAuthUserBO schisandraAuthUserBO = SchisandraAuthUserDTOConverter.INSTANCE.convertDTOToBO(schisandraAuthUserDTO);
SchisandraAuthUserBO loginByPhone = schisandraAuthUserDomainService.loginByPhone(schisandraAuthUserBO);
SchisandraAuthUserDTO result = SchisandraAuthUserDTOConverter.INSTANCE.convertBOToDTO(loginByPhone);
// 加入登录IP地址
String ip = IPUtil.getIp(httpServletRequest);
UserAgent userAgent = IPUtil.getUserAgent(httpServletRequest);
String ip2region = IPUtil.getIp2region(ip);
SchisandraAuthAddressBO schisandraAuthAddressBO = new SchisandraAuthAddressBO();
schisandraAuthAddressBO.setAgent(userAgent.toString());
schisandraAuthAddressBO.setUserId(result.getId());
schisandraAuthAddressBO.setIp(ip);
schisandraAuthAddressBO.setBrowser(userAgent.getBrowser().toString());
schisandraAuthAddressBO.setBrowserVersion(userAgent.getBrowserVersion().getVersion());
schisandraAuthAddressBO.setLocation(ip2region);
Boolean add = schisandraAuthAddressDomainService.add(schisandraAuthAddressBO);
assert add;
map.put("user", result);
if (loginByPhone != null) {
if (StpUtil.isLogin(result.getId())) {
@@ -305,8 +339,8 @@ public class SchisandraAuthUserController {
*/
@SneakyThrows
@PostMapping("wechatRegister")
public Boolean wechatRegister(@RequestParam(name = "appId") String appId, @RequestParam(name = "openId") String openId, @RequestParam("clientId") String clientId) {
Boolean result = schisandraAuthUserDomainService.wechatRegister(appId, openId, clientId);
public Boolean wechatRegister(@RequestParam(name = "appId") String appId, @RequestParam(name = "openId") String openId, @RequestParam("clientId") String clientId, HttpServletRequest httpServletRequest) {
Boolean result = schisandraAuthUserDomainService.wechatRegister(appId, openId, clientId, httpServletRequest);
if (result) {
return true;
}

View File

@@ -1,98 +1,99 @@
package com.schisandra.auth.application.controller;
import cn.dev33.satoken.stp.StpUtil;
import com.schisandra.auth.application.factory.OauthTypeHandlerFactory;
import com.schisandra.auth.application.handler.oauth.SchisandraOauthTypeHandler;
import com.schisandra.auth.common.entity.Result;
import com.schisandra.auth.domain.service.SchisandraAuthUserDomainService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* ClassName:SchisandraOauthController
* Package:com.schisandra.auth.application.controller
* Description:实现justauth第三方登录
*
* @Author:fanyang
* @Create:2024/5/9 - 21:30
* @Version: v1.0
*/
@Slf4j
@RestController
@RequestMapping("/oauth/")
public class SchisandraOauthController {
@Resource
private OauthTypeHandlerFactory oauthTypeHandlerFactory;
@Resource
private SchisandraAuthUserDomainService schisandraAuthUserDomainService;
@Value("${web.url}")
private String url;
/**
* @description: 获取授权链接并跳转到第三方授权页面
* @param: []
* @return: com.schisandra.auth.common.entity.Result
* {@code @author:} landaiqing
* @date: 2024/5/26 0:26
*/
@GetMapping("/render/{type}")
public Result<String> renderAuth(@PathVariable("type") String type) throws IOException {
AuthRequest authRequest = getAuthRequest(type);
String token = AuthStateUtils.createState();
String url = authRequest.authorize(token);
return Result.ok(url);
}
private AuthRequest getAuthRequest(String type) {
SchisandraOauthTypeHandler handler = oauthTypeHandlerFactory.getHandler(type);
AuthRequest authRequest = handler.getAuthRequest(type);
return authRequest;
}
/**
* @description: 回调函数
* @param: [callback]
* @return: void
* @author: landaiqing
* @date: 2024/5/26 13:20
*/
@GetMapping("/callback/{type}")
@SneakyThrows
public void login(@PathVariable("type") String type, AuthCallback callback, HttpServletResponse response) {
AuthRequest authRequest = getAuthRequest(type);
AuthResponse<AuthUser> authResponse = authRequest.login(callback);
AuthUser data = authResponse.getData();
AuthToken token = data.getToken();
if (token == null) {
return;
}
Result result = schisandraAuthUserDomainService.insertAuthUserByOauth(data, type);
if (result.getSuccess()) {
response.sendRedirect(url + "loading?token=" + token.getAccessToken() + "&userId=" + StpUtil.getLoginIdAsString());
} else {
log.error("登录失败");
}
}
}
package com.schisandra.auth.application.controller;
import cn.dev33.satoken.stp.StpUtil;
import com.schisandra.auth.application.factory.OauthTypeHandlerFactory;
import com.schisandra.auth.application.handler.oauth.SchisandraOauthTypeHandler;
import com.schisandra.auth.common.entity.Result;
import com.schisandra.auth.domain.service.SchisandraAuthUserDomainService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* ClassName:SchisandraOauthController
* Package:com.schisandra.auth.application.controller
* Description:实现justauth第三方登录
*
* @Author:fanyang
* @Create:2024/5/9 - 21:30
* @Version: v1.0
*/
@Slf4j
@RestController
@RequestMapping("/oauth/")
public class SchisandraOauthController {
@Resource
private OauthTypeHandlerFactory oauthTypeHandlerFactory;
@Resource
private SchisandraAuthUserDomainService schisandraAuthUserDomainService;
@Value("${web.url}")
private String url;
/**
* @description: 获取授权链接并跳转到第三方授权页面
* @param: []
* @return: com.schisandra.auth.common.entity.Result
* {@code @author:} landaiqing
* @date: 2024/5/26 0:26
*/
@GetMapping("/render/{type}")
public Result<String> renderAuth(@PathVariable("type") String type) throws IOException {
AuthRequest authRequest = getAuthRequest(type);
String token = AuthStateUtils.createState();
String url = authRequest.authorize(token);
return Result.ok(url);
}
private AuthRequest getAuthRequest(String type) {
SchisandraOauthTypeHandler handler = oauthTypeHandlerFactory.getHandler(type);
AuthRequest authRequest = handler.getAuthRequest(type);
return authRequest;
}
/**
* @description: 回调函数
* @param: [callback]
* @return: void
* @author: landaiqing
* @date: 2024/5/26 13:20
*/
@GetMapping("/callback/{type}")
@SneakyThrows
public void login(@PathVariable("type") String type, AuthCallback callback, HttpServletResponse response, HttpServletRequest httpServletRequest) {
AuthRequest authRequest = getAuthRequest(type);
AuthResponse<AuthUser> authResponse = authRequest.login(callback);
AuthUser data = authResponse.getData();
AuthToken token = data.getToken();
if (token == null) {
return;
}
Result result = schisandraAuthUserDomainService.insertAuthUserByOauth(data, type,httpServletRequest);
if (result.getSuccess()) {
response.sendRedirect(url + "loading?token=" + token.getAccessToken() + "&userId=" + StpUtil.getLoginIdAsString());
} else {
log.error("登录失败");
}
}
}

View File

@@ -0,0 +1,25 @@
package com.schisandra.auth.application.convert;
import com.schisandra.auth.application.dto.SchisandraAuthAddressDTO;
import com.schisandra.auth.domain.bo.SchisandraAuthAddressBO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
/**
* 登录设备ip dto转换器
*
* @author landaiqing
* @since 2024-07-20 15:33:38
*/
@Mapper
public interface SchisandraAuthAddressDTOConverter {
SchisandraAuthAddressDTOConverter INSTANCE = Mappers.getMapper(SchisandraAuthAddressDTOConverter.class);
SchisandraAuthAddressBO convertDTOToBO(SchisandraAuthAddressDTO schisandraAuthAddressDTO);
SchisandraAuthAddressDTO convertBOToDTO(SchisandraAuthAddressBO schisandraAuthAddressBO);
List<SchisandraAuthAddressDTO> convertBOToDTOList(List<SchisandraAuthAddressBO> schisandraAuthAddressBOList);
}

View File

@@ -0,0 +1,88 @@
package com.schisandra.auth.application.dto;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* 登录设备ip dto
*
* @author landaiqing
* @since 2024-07-20 15:33:38
*/
@Data
public class SchisandraAuthAddressDTO implements Serializable {
/**
*
*/
private String id;
/**
*
*/
private String userId;
/**
*
*/
private String ip;
/**
*
*/
private String location;
/**
*
*/
private String agent;
/**
*
*/
private String extJson;
/**
* 创建人
*/
private String createdBy;
/**
* 创建时间
*/
private Date createdTime;
/**
* 更新人
*/
private String updateBy;
/**
* 更新时间
*/
private Date updateTime;
/**
*
*/
private Integer isDeleted;
/**
*
*/
private String browser;
/**
*
*/
private String operatingSystem;
/**
*
*/
private String browserVersion;
}

View File

@@ -1,76 +1,88 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<parent>
<groupId>com.schisandra</groupId>
<artifactId>schisandra-cloud-storage-auth</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>schisandra-cloud-storage-auth-domain</artifactId>
<name>schisandra-cloud-storage-auth-domain</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</path>
<!-- 必须要加, 否则生成不了 MapperImpl 实现类 -->
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.4.2.Final</version>
</path>
<!-- 如果是 0.1.0 有可能出现生成了maptruct的实现类, 但该类只创建了对象, 没有进行赋值 -->
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
<path>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-processor</artifactId>
<version>1.9.0</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.schisandra</groupId>
<artifactId>schisandra-cloud-storage-auth-infra</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.schisandra</groupId>
<artifactId>schisandra-cloud-storage-auth-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
</project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<parent>
<groupId>com.schisandra</groupId>
<artifactId>schisandra-cloud-storage-auth</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>schisandra-cloud-storage-auth-domain</artifactId>
<name>schisandra-cloud-storage-auth-domain</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</path>
<!-- 必须要加, 否则生成不了 MapperImpl 实现类 -->
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.4.2.Final</version>
</path>
<!-- 如果是 0.1.0 有可能出现生成了maptruct的实现类, 但该类只创建了对象, 没有进行赋值 -->
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
<path>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-processor</artifactId>
<version>1.9.0</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.schisandra</groupId>
<artifactId>schisandra-cloud-storage-auth-infra</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.schisandra</groupId>
<artifactId>schisandra-cloud-storage-auth-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.lionsoul/ip2region -->
<dependency>
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>eu.bitwalker</groupId>
<artifactId>UserAgentUtils</artifactId>
<version>1.21</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,87 @@
package com.schisandra.auth.domain.bo;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* 登录设备ip bo
*
* @author landaiqing
* @since 2024-07-20 15:33:38
*/
@Data
public class SchisandraAuthAddressBO implements Serializable {
/**
*
*/
private String id;
/**
*
*/
private String userId;
/**
*
*/
private String ip;
/**
*
*/
private String location;
/**
*
*/
private String agent;
/**
*
*/
private String extJson;
/**
* 创建人
*/
private String createdBy;
/**
* 创建时间
*/
private Date createdTime;
/**
* 更新人
*/
private String updateBy;
/**
* 更新时间
*/
private Date updateTime;
/**
*
*/
private Integer isDeleted;
/**
*
*/
private String browser;
/**
*
*/
private String operatingSystem;
/**
*
*/
private String browserVersion;
}

View File

@@ -0,0 +1,22 @@
package com.schisandra.auth.domain.convert;
import com.schisandra.auth.domain.bo.SchisandraAuthAddressBO;
import com.schisandra.auth.infra.basic.entity.SchisandraAuthAddress;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
/**
* 登录设备ip bo转换器
*
* @author landaiqing
* @since 2024-07-20 15:33:38
*/
@Mapper
public interface SchisandraAuthAddressBOConverter {
SchisandraAuthAddressBOConverter INSTANCE = Mappers.getMapper(SchisandraAuthAddressBOConverter.class);
SchisandraAuthAddress convertBOToEntity(SchisandraAuthAddressBO schisandraAuthAddressBO);
SchisandraAuthAddressBO convertEntityToBO(SchisandraAuthAddress schisandraAuthAddress);
}

View File

@@ -0,0 +1,29 @@
package com.schisandra.auth.domain.service;
import com.schisandra.auth.domain.bo.SchisandraAuthAddressBO;
/**
* 登录设备ip 领域service
*
* @author landaiqing
* @since 2024-07-20 15:33:38
*/
public interface SchisandraAuthAddressDomainService {
/**
* 添加 登录设备ip 信息
*/
Boolean add(SchisandraAuthAddressBO schisandraAuthAddressBO);
/**
* 更新 登录设备ip 信息
*/
Boolean update(SchisandraAuthAddressBO schisandraAuthAddressBO);
/**
* 删除 登录设备ip 信息
*/
Boolean delete(SchisandraAuthAddressBO schisandraAuthAddressBO);
SchisandraAuthAddressBO queryByUserId(String userId);
}

View File

@@ -9,6 +9,7 @@ import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
@@ -77,7 +78,7 @@ public interface SchisandraAuthUserDomainService {
* @date: 2024/5/26 17:27
*/
Result insertAuthUserByOauth(AuthUser data, String type);
Result insertAuthUserByOauth(AuthUser data, String type, HttpServletRequest httpServletRequest);
/**
* @description: 通过id查询用户信息
@@ -108,6 +109,6 @@ public interface SchisandraAuthUserDomainService {
SchisandraAuthUser queryByPhone(String phone);
Boolean wechatRegister(String appId, String openId, String clientId);
Boolean wechatRegister(String appId, String openId, String clientId, HttpServletRequest httpServletRequest);
}

View File

@@ -0,0 +1,55 @@
package com.schisandra.auth.domain.service.impl;
import com.schisandra.auth.common.enums.IsDeletedFlagEnum;
import com.schisandra.auth.domain.bo.SchisandraAuthAddressBO;
import com.schisandra.auth.domain.convert.SchisandraAuthAddressBOConverter;
import com.schisandra.auth.domain.service.SchisandraAuthAddressDomainService;
import com.schisandra.auth.infra.basic.entity.SchisandraAuthAddress;
import com.schisandra.auth.infra.basic.service.SchisandraAuthAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* 登录设备ip 领域service实现了
*
* @author landaiqing
* @since 2024-07-20 15:33:38
*/
@Service
@Slf4j
public class SchisandraAuthAddressDomainServiceImpl implements SchisandraAuthAddressDomainService {
@Resource
private SchisandraAuthAddressService schisandraAuthAddressService;
@Override
public Boolean add(SchisandraAuthAddressBO schisandraAuthAddressBO) {
SchisandraAuthAddress schisandraAuthAddress = SchisandraAuthAddressBOConverter.INSTANCE.convertBOToEntity(schisandraAuthAddressBO);
schisandraAuthAddress.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
return schisandraAuthAddressService.insert(schisandraAuthAddress) > 0;
}
@Override
public Boolean update(SchisandraAuthAddressBO schisandraAuthAddressBO) {
SchisandraAuthAddress schisandraAuthAddress = SchisandraAuthAddressBOConverter.INSTANCE.convertBOToEntity(schisandraAuthAddressBO);
return schisandraAuthAddressService.update(schisandraAuthAddress) > 0;
}
@Override
public Boolean delete(SchisandraAuthAddressBO schisandraAuthAddressBO) {
SchisandraAuthAddress schisandraAuthAddress = new SchisandraAuthAddress();
schisandraAuthAddress.setId(schisandraAuthAddressBO.getId());
schisandraAuthAddress.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
return schisandraAuthAddressService.update(schisandraAuthAddress) > 0;
}
@Override
public SchisandraAuthAddressBO queryByUserId(String userId) {
SchisandraAuthAddress schisandraAuthAddress = schisandraAuthAddressService.queryByUserId(userId);
SchisandraAuthAddressBO schisandraAuthAddressBO = SchisandraAuthAddressBOConverter.INSTANCE.convertEntityToBO(schisandraAuthAddress);
return schisandraAuthAddressBO;
}
}

View File

@@ -10,14 +10,13 @@ import com.schisandra.auth.common.entity.Result;
import com.schisandra.auth.common.enums.IsDeletedFlagEnum;
import com.schisandra.auth.common.enums.UserRoleEnum;
import com.schisandra.auth.domain.bo.*;
import com.schisandra.auth.domain.convert.SchisandraAuthUserBOConverter;
import com.schisandra.auth.domain.convert.SchisandraAuthUserRoleBOConverter;
import com.schisandra.auth.domain.convert.SchisandraSocialUserAuthBOConverter;
import com.schisandra.auth.domain.convert.SchisandraSocialUserBOConverter;
import com.schisandra.auth.domain.convert.*;
import com.schisandra.auth.domain.redis.RedisUtil;
import com.schisandra.auth.domain.service.SchisandraAuthUserDomainService;
import com.schisandra.auth.domain.utils.IPUtil;
import com.schisandra.auth.infra.basic.entity.*;
import com.schisandra.auth.infra.basic.service.*;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser;
@@ -26,6 +25,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
@@ -70,6 +70,9 @@ public class SchisandraAuthUserDomainServiceImpl implements SchisandraAuthUserDo
@Resource
private SchisandraAuthPermissionService schisandraAuthPermissionService;
@Resource
private SchisandraAuthAddressService schisandraAuthAddressService;
@Override
public List<SchisandraAuthUserBO> listAllUsers() {
@@ -231,10 +234,29 @@ public class SchisandraAuthUserDomainServiceImpl implements SchisandraAuthUserDo
* @date: 2024/6/27 下午2:58
*/
@Override
public Boolean wechatRegister(String appId, String openId,String clientId) {
public Boolean wechatRegister(String appId, String openId,String clientId, HttpServletRequest httpServletRequest) {
SchisandraSocialUser socialUser = schisandraSocialUserService.selectByOpenId(openId);
if (ObjectUtils.isNotEmpty(socialUser)) {
String userId = socialUser.getId();
// 加入登录IP地址
String ip = IPUtil.getIp(httpServletRequest);
UserAgent userAgent = IPUtil.getUserAgent(httpServletRequest);
String ip2region = IPUtil.getIp2region(ip);
SchisandraAuthAddress authAddress= schisandraAuthAddressService.queryByUserId(userId);
SchisandraAuthAddressBO schisandraAuthAddressBO = new SchisandraAuthAddressBO();
schisandraAuthAddressBO.setId(authAddress.getId());
schisandraAuthAddressBO.setAgent(userAgent.toString());
schisandraAuthAddressBO.setUserId(userId);
schisandraAuthAddressBO.setIp(ip);
schisandraAuthAddressBO.setBrowser(userAgent.getBrowser().toString());
schisandraAuthAddressBO.setBrowserVersion(userAgent.getBrowserVersion().getVersion());
schisandraAuthAddressBO.setLocation(ip2region);
SchisandraAuthAddress schisandraAuthAddress = SchisandraAuthAddressBOConverter.INSTANCE.convertBOToEntity(schisandraAuthAddressBO);
int insert = schisandraAuthAddressService.update(schisandraAuthAddress);
assert insert>0;
// redis存储用户角色与权限信息
userInfoPersistence(userId);
StpUtil.login(userId);
@@ -268,6 +290,23 @@ public class SchisandraAuthUserDomainServiceImpl implements SchisandraAuthUserDo
}
String authUserId = schisandraAuthUser.getId();
String socialUserId = schisandraSocialUser.getId();
// 加入登录IP地址
String ip = IPUtil.getIp(httpServletRequest);
UserAgent userAgent = IPUtil.getUserAgent(httpServletRequest);
String ip2region = IPUtil.getIp2region(ip);
SchisandraAuthAddressBO schisandraAuthAddressBO = new SchisandraAuthAddressBO();
schisandraAuthAddressBO.setAgent(userAgent.toString());
schisandraAuthAddressBO.setUserId(authUserId);
schisandraAuthAddressBO.setIp(ip);
schisandraAuthAddressBO.setBrowser(userAgent.getBrowser().toString());
schisandraAuthAddressBO.setBrowserVersion(userAgent.getBrowserVersion().getVersion());
schisandraAuthAddressBO.setLocation(ip2region);
SchisandraAuthAddress schisandraAuthAddress = SchisandraAuthAddressBOConverter.INSTANCE.convertBOToEntity(schisandraAuthAddressBO);
int insertIp = schisandraAuthAddressService.insert(schisandraAuthAddress);
assert insertIp>0;
// 建立社会用户与用户信息映射
SchisandraSocialUserAuthBO socialUserAuthBO = new SchisandraSocialUserAuthBO();
socialUserAuthBO.setUserId(String.valueOf(authUserId));
@@ -315,7 +354,7 @@ public class SchisandraAuthUserDomainServiceImpl implements SchisandraAuthUserDo
*/
@Override
@Transactional
public Result insertAuthUserByOauth(AuthUser data, String type) {
public Result insertAuthUserByOauth(AuthUser data, String type, HttpServletRequest httpServletRequest) {
String uuid = data.getUuid();
AuthToken token = data.getToken();
SchisandraSocialUser socialUser = schisandraSocialUserService.selectByUuidAndType(uuid, type);
@@ -345,6 +384,25 @@ public class SchisandraAuthUserDomainServiceImpl implements SchisandraAuthUserDo
return Result.fail("Social User update fail");
}
String userId = socialUser.getId();
// 加入登录IP地址
String ip = IPUtil.getIp(httpServletRequest);
UserAgent userAgent = IPUtil.getUserAgent(httpServletRequest);
String ip2region = IPUtil.getIp2region(ip);
SchisandraAuthAddress authAddress= schisandraAuthAddressService.queryByUserId(userId);
SchisandraAuthAddressBO schisandraAuthAddressBO = new SchisandraAuthAddressBO();
schisandraAuthAddressBO.setId(authAddress.getId());
schisandraAuthAddressBO.setAgent(userAgent.toString());
schisandraAuthAddressBO.setUserId(userId);
schisandraAuthAddressBO.setIp(ip);
schisandraAuthAddressBO.setBrowser(userAgent.getBrowser().toString());
schisandraAuthAddressBO.setBrowserVersion(userAgent.getBrowserVersion().getVersion());
schisandraAuthAddressBO.setLocation(ip2region);
SchisandraAuthAddress schisandraAuthAddress = SchisandraAuthAddressBOConverter.INSTANCE.convertBOToEntity(schisandraAuthAddressBO);
int insert = schisandraAuthAddressService.update(schisandraAuthAddress);
assert insert>0;
// redis存储用户角色与权限信息
userInfoPersistence(userId);
StpUtil.login(userId, SaLoginConfig.setToken(token.getAccessToken()));
@@ -397,6 +455,21 @@ public class SchisandraAuthUserDomainServiceImpl implements SchisandraAuthUserDo
String authUserId = schisandraAuthUser.getId();
String socialUserId = schisandraSocialUser.getId();
// 加入登录IP地址
String ip = IPUtil.getIp(httpServletRequest);
UserAgent userAgent = IPUtil.getUserAgent(httpServletRequest);
String ip2region = IPUtil.getIp2region(ip);
SchisandraAuthAddressBO schisandraAuthAddressBO = new SchisandraAuthAddressBO();
schisandraAuthAddressBO.setAgent(userAgent.toString());
schisandraAuthAddressBO.setUserId(authUserId);
schisandraAuthAddressBO.setIp(ip);
schisandraAuthAddressBO.setBrowser(userAgent.getBrowser().toString());
schisandraAuthAddressBO.setBrowserVersion(userAgent.getBrowserVersion().getVersion());
schisandraAuthAddressBO.setLocation(ip2region);
SchisandraAuthAddress schisandraAuthAddress = SchisandraAuthAddressBOConverter.INSTANCE.convertBOToEntity(schisandraAuthAddressBO);
int insertIp = schisandraAuthAddressService.insert(schisandraAuthAddress);
assert insertIp>0;
// 建立社会用户与用户信息映射
SchisandraSocialUserAuthBO socialUserAuthBO = new SchisandraSocialUserAuthBO();
socialUserAuthBO.setUserId(String.valueOf(authUserId));

View File

@@ -0,0 +1,193 @@
package com.schisandra.auth.domain.utils;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.lionsoul.ip2region.xdb.Searcher;
import org.springframework.util.FileCopyUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @author HAOYANG
* @create 2023-08-02 17:03
*/
@Slf4j
public class IPUtil {
private final static String localIp = "127.0.0.1";
private static Searcher searcher = null;
/**
* 在服务启动时加载 ip2region.db 到内存中
* 解决打包jar后找不到 ip2region.db 的问题
*/
static {
try {
InputStream ris = IPUtil.class.getResourceAsStream("/ip2region/ip2region.xdb");
byte[] dbBinStr = FileCopyUtils.copyToByteArray(ris);
searcher = Searcher.newWithBuffer(dbBinStr);
//注意不能使用文件类型打成jar包后会找不到文件
log.debug("缓存成功!!!!");
} catch (IOException e) {
log.error("解析ip地址失败,无法创建搜索器: {}", e);
throw new RuntimeException(e);
}
}
/**
* 获取用户真实IP地址不使用request.getRemoteAddr();的原因是有可能用户使用了代理软件方式避免真实IP地址,
* 参考文章: http://developer.51cto.com/art/201111/305181.htm
* <p>
* 可是如果通过了多级反向代理的话X-Forwarded-For的值并不止一个而是一串IP值究竟哪个才是真正的用户端的真实IP呢
* 答案是取X-Forwarded-For中第一个非unknown的有效IP字符串。
* <p>
* 如X-Forwarded-For192.168.1.110, 192.168.1.120, 192.168.1.130,
* 192.168.1.100
* <p>
* 用户真实IP为 192.168.1.110
*
* @param request
* @return
*/
public static String getIp(HttpServletRequest request) {
String ipAddress;
try {
// 以下两个获取在k8s中将真实的客户端IP放到了x-Original-Forwarded-For。而将WAF的回源地址放到了 x-Forwarded-For了。
ipAddress = request.getHeader("X-Original-Forwarded-For");
if (ipAddress == null || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("X-Forwarded-For");
}
//获取nginx等代理的ip
if (ipAddress == null || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("x-forwarded-for");
}
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("HTTP_CLIENT_IP");
}
if (ipAddress == null || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
}
// 2.如果没有转发的ip则取当前通信的请求端的ip(兼容k8s集群获取ip)
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
// 如果是127.0.0.1则取本地真实ip
if (localIp.equals(ipAddress)) {
// 根据网卡取本机配置的IP
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
ipAddress = inet.getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
// 对于通过多个代理的情况第一个IP为客户端真实IP,多个IP按照','分割
if (ipAddress != null && ipAddress.length() > 15) {
// = 15
if (ipAddress.indexOf(",") > 0) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
}
} catch (Exception e) {
log.error("解析请求IP失败", e);
ipAddress = "";
}
return "0:0:0:0:0:0:0:1".equals(ipAddress) ? localIp : ipAddress;
}
/**
* 获取访问设备
*
* @param request
* @return
*/
public static UserAgent getUserAgent(HttpServletRequest request) {
return UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
}
/**
* 根据ip获取 城市信息
*
* @param ipAddress
* @return
*/
public static String getCityInfo(String ipAddress) {
String cityInfo = null;
try {
return searcher.search(ipAddress);
} catch (Exception e) {
log.error("搜索:{} 失败: {}", ipAddress, e);
}
return null;
}
/**
* 根据ip2region解析ip地址
*
* @param ip ip地址
* @return 解析后的ip地址信息
*/
public static String getIp2region(String ip) {
if (searcher == null) {
log.error("Error: DbSearcher is null");
return null;
}
try {
String ipInfo = searcher.search(ip);
if (!StringUtils.isEmpty(ipInfo)) {
ipInfo = ipInfo.replace("|0", "");
ipInfo = ipInfo.replace("0|", "");
}
return ipInfo;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获取IP地址
*
* @return 本地IP地址
*/
public static String getHostIp() {
try {
return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
}
return localIp;
}
/**
* 获取主机名
*
* @return 本地主机名
*/
public static String getHostName() {
try {
return InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
}
return "未知";
}
}

View File

@@ -0,0 +1,17 @@
package com.schisandra.auth.infra.basic.dao;
import com.schisandra.auth.infra.basic.entity.SchisandraAuthAddress;
import com.mybatisflex.core.BaseMapper;
import org.springframework.stereotype.Repository;
/**
* 登录设备ip 表数据库访问层
*
* @author landaiqing
* @since 2024-07-20 15:33:38
*/
@Repository
public interface SchisandraAuthAddressDao extends BaseMapper<SchisandraAuthAddress> {
}

View File

@@ -0,0 +1,108 @@
package com.schisandra.auth.infra.basic.entity;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table;
import com.mybatisflex.core.keygen.KeyGenerators;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* 登录设备ip 实体类
*
* @author landaiqing
* @since 2024-07-20 15:33:38
*/
@Data
@Table("schisandra_auth_address")
public class SchisandraAuthAddress implements Serializable {
/**
*
*/
@Id(keyType=KeyType.Generator, value= KeyGenerators.flexId)
private String id;
/**
*
*/
@Column("user_id")
private String userId;
/**
*
*/
@Column("ip")
private String ip;
/**
*
*/
@Column("location")
private String location;
/**
*
*/
@Column("agent")
private String agent;
/**
*
*/
@Column("ext_json")
private String extJson;
/**
* 创建人
*/
@Column("created_by")
private String createdBy;
/**
* 创建时间
*/
@Column("created_time")
private Date createdTime;
/**
* 更新人
*/
@Column("update_by")
private String updateBy;
/**
* 更新时间
*/
@Column("update_time")
private Date updateTime;
/**
*
*/
@Column("is_deleted")
private Integer isDeleted;
/**
*
*/
@Column("browser")
private String browser;
/**
*
*/
@Column("operating_system")
private String operatingSystem;
/**
*
*/
@Column("browser_version")
private String browserVersion;
}

View File

@@ -0,0 +1,47 @@
package com.schisandra.auth.infra.basic.service;
import com.schisandra.auth.infra.basic.entity.SchisandraAuthAddress;
/**
* 登录设备ip 表服务接口
*
* @author landaiqing
* @since 2024-07-20 15:33:38
*/
public interface SchisandraAuthAddressService {
/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
SchisandraAuthAddress queryById(Long id);
/**
* 新增数据
*
* @param schisandraAuthAddress 实例对象
* @return 实例对象
*/
int insert(SchisandraAuthAddress schisandraAuthAddress);
/**
* 修改数据
*
* @param schisandraAuthAddress 实例对象
* @return 实例对象
*/
int update(SchisandraAuthAddress schisandraAuthAddress);
/**
* 通过主键删除数据
*
* @param id 主键
* @return 是否成功
*/
boolean deleteById(Long id);
SchisandraAuthAddress queryByUserId(String userId);
}

View File

@@ -0,0 +1,73 @@
package com.schisandra.auth.infra.basic.service.impl;
import com.schisandra.auth.infra.basic.entity.SchisandraAuthAddress;
import com.schisandra.auth.infra.basic.dao.SchisandraAuthAddressDao;
import com.schisandra.auth.infra.basic.entity.table.SchisandraAuthAddressTableDef;
import com.schisandra.auth.infra.basic.service.SchisandraAuthAddressService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* 登录设备ip 表服务实现类
*
* @author landaiqing
* @since 2024-07-20 15:33:38
*/
@Service("SchisandraAuthAddressService")
public class SchisandraAuthAddressServiceImpl implements SchisandraAuthAddressService {
@Resource
private SchisandraAuthAddressDao schisandraAuthAddressDao;
/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
@Override
public SchisandraAuthAddress queryById(Long id) {
return this.schisandraAuthAddressDao.selectOneById(id);
}
/**
* 新增数据
*
* @param schisandraAuthAddress 实例对象
* @return 实例对象
*/
@Override
public int insert(SchisandraAuthAddress schisandraAuthAddress) {
return this.schisandraAuthAddressDao.insertSelective(schisandraAuthAddress);
}
/**
* 修改数据
*
* @param schisandraAuthAddress 实例对象
* @return 实例对象
*/
@Override
public int update(SchisandraAuthAddress schisandraAuthAddress) {
return this.schisandraAuthAddressDao.update(schisandraAuthAddress,true);
}
/**
* 通过主键删除数据
*
* @param id 主键
* @return 是否成功
*/
@Override
public boolean deleteById(Long id) {
return this.schisandraAuthAddressDao.deleteById(id) > 0;
}
@Override
public SchisandraAuthAddress queryByUserId(String userId) {
return this.schisandraAuthAddressDao.selectOneByCondition(SchisandraAuthAddressTableDef.SCHISANDRA_AUTH_ADDRESS.USER_ID.eq(userId));
}
}

View File

@@ -1,6 +1,7 @@
package com.schisandra.auth.infra.basic.service.impl;
import com.mybatisflex.core.mask.MaskManager;
import com.mybatisflex.core.update.UpdateChain;
import com.schisandra.auth.infra.basic.dao.SchisandraAuthUserDao;
import com.schisandra.auth.infra.basic.entity.SchisandraAuthUser;
@@ -30,14 +31,18 @@ public class SchisandraAuthUserServiceImpl implements SchisandraAuthUserService
@Override
public SchisandraAuthUser queryByEmail(String email) {
return schisandraAuthUserDao.selectOneByCondition(SchisandraAuthUserTableDef.SCHISANDRA_AUTH_USER.EMAIL.eq(email)
.and(SchisandraAuthUserTableDef.SCHISANDRA_AUTH_USER.IS_DELETED.eq(0)));
SchisandraAuthUser schisandraAuthUser = MaskManager.execWithoutMask(() ->
schisandraAuthUserDao.selectOneByCondition(SchisandraAuthUserTableDef.SCHISANDRA_AUTH_USER.EMAIL.eq(email)
.and(SchisandraAuthUserTableDef.SCHISANDRA_AUTH_USER.IS_DELETED.eq(0))));
return schisandraAuthUser;
}
@Override
public SchisandraAuthUser queryByPhone(String phone) {
return schisandraAuthUserDao.selectOneByCondition(SchisandraAuthUserTableDef.SCHISANDRA_AUTH_USER.PHONE.eq(phone)
.and(SchisandraAuthUserTableDef.SCHISANDRA_AUTH_USER.IS_DELETED.eq(0)));
SchisandraAuthUser schisandraAuthUser = MaskManager.execWithoutMask(() -> schisandraAuthUserDao.selectOneByCondition(SchisandraAuthUserTableDef.SCHISANDRA_AUTH_USER.PHONE.eq(phone)
.and(SchisandraAuthUserTableDef.SCHISANDRA_AUTH_USER.IS_DELETED.eq(0))));
return schisandraAuthUser;
}
/**

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.schisandra.auth.infra.basic.dao.SchisandraAuthAddressDao">
<resultMap id="BaseResultMap" type="com.schisandra.auth.infra.basic.entity.SchisandraAuthAddress">
<id column="id" jdbcType="VARCHAR" property="id"/>
<result column="user_id" jdbcType="VARCHAR" property="userId"/>
<result column="ip" jdbcType="VARCHAR" property="ip"/>
<result column="location" jdbcType="VARCHAR" property="location"/>
<result column="agent" jdbcType="VARCHAR" property="agent"/>
<result column="ext_json" jdbcType="VARCHAR" property="extJson"/>
<result column="created_by" jdbcType="VARCHAR" property="createdBy"/>
<result column="created_time" jdbcType="TIMESTAMP" property="createdTime"/>
<result column="update_by" jdbcType="VARCHAR" property="updateBy"/>
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
<result column="is_deleted" jdbcType="INTEGER" property="isDeleted"/>
<result column="browser" jdbcType="VARCHAR" property="browser"/>
<result column="operating_system" jdbcType="VARCHAR" property="operatingSystem"/>
<result column="browser_version" jdbcType="VARCHAR" property="browserVersion"/>
</resultMap>
</mapper>

View File

@@ -20,92 +20,92 @@
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.7</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.4.2.Final</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.4.2.Final</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.7</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.4.2.Final</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.4.2.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.79</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
<!-- 提供Redis连接池 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.5.15</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.79</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
<!-- 提供Redis连接池 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.5.15</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-extra</artifactId>
<version>5.8.27</version>
</dependency>
<dependency>
<groupId>org.apache.directory.studio</groupId>
<artifactId>org.apache.commons.codec</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-extra</artifactId>
<version>5.8.27</version>
</dependency>
<dependency>
<groupId>org.apache.directory.studio</groupId>
<artifactId>org.apache.commons.codec</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>