设备指纹引入
This commit is contained in:
1 parent
06167dd9e9
commit
aa5281da83
8 files changed
+319
-5
No files matched your search
+38
-5
@@ -17,6 +17,7 @@ import com.bsd.pjcloud.thirdparty.entity.ThirdPartyUserEntity;
|
||||
import com.bsd.pjcloud.thirdparty.service.SmsService;
|
||||
import com.bsd.pjcloud.thirdparty.service.ThirdPartyOAuth2Service;
|
||||
import com.bsd.pjcloud.thirdparty.service.ThirdPartyUserService;
|
||||
import com.bsd.pjcloud.thirdparty.utils.DeviceFingerprintUtil;
|
||||
import com.bsd.pjcloud.thirdparty.utils.SmsCodeUtil;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
@@ -203,20 +204,21 @@ public class ThirdPartyOAth2Cotroller {
|
||||
@Transactional
|
||||
@GlobalTransactional
|
||||
public R shjnsso(@RequestBody @Valid SHJNSSODTO shjnsso) {
|
||||
//检查设备指纹
|
||||
ZZDUserDTO zzdUserDTO = thirdPartyOAuth2Service.shjnsso(shjnsso.getAccountId(), shjnsso.getSessionId());
|
||||
if (null==zzdUserDTO){
|
||||
throw new RuntimeException("");
|
||||
}
|
||||
ZZDUserDTO.Ext ext = zzdUserDTO.getExt();
|
||||
String mobile = zzdUserDTO.getMobile();
|
||||
R<Map> r;
|
||||
R<List<SysUser>> userByMobileOrEmployeeCode = remoteUserService.getUserByMobileOrEmployeeCode(null, mobile);
|
||||
List<SysUser> sysUserList = userByMobileOrEmployeeCode.getData();
|
||||
if(!CollectionUtils.isEmpty(sysUserList)){
|
||||
//登录成功生成token
|
||||
UserTokenDTO userTokenDTO = new UserTokenDTO();
|
||||
userTokenDTO.setSysUser(sysUserList.get(0));
|
||||
r=remoteTokenService.generateToken(userTokenDTO);
|
||||
|
||||
return remoteTokenService.generateToken(userTokenDTO);
|
||||
}else {
|
||||
//自动注册
|
||||
// UserDTO userDTO = new UserDTO();
|
||||
@@ -237,10 +239,25 @@ public class ThirdPartyOAth2Cotroller {
|
||||
// r=remoteTokenService.generateToken(userTokenDTO);
|
||||
return R.failed("系统中没有当前用户信息,请联系管理员进行申请!");
|
||||
}
|
||||
return r;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// 在shjnsso方法中添加验证
|
||||
// @PostMapping("/shjnsso")
|
||||
// public R shjnsso(..., HttpServletRequest request) {
|
||||
// // 设备指纹验证
|
||||
// String clientFingerprint = DeviceFingerprintUtil.generate(request);
|
||||
// String storedFingerprint = redisTemplate.opsForValue().get("device_fp:" + shjnsso.getAccountId());
|
||||
//
|
||||
// if (!clientFingerprint.equals(storedFingerprint)) {
|
||||
// // 新设备需要短信验证
|
||||
// String code = SmsCodeUtil.generateNumberCode(6);
|
||||
// smsService.sendSms(mobile, "验证码模板ID", Map.of("code", code));
|
||||
// return R.failed("new_device").data("needSmsVerify", true);
|
||||
// }
|
||||
//
|
||||
// // 原有逻辑继续执行...
|
||||
// }
|
||||
@Inner(value = false)
|
||||
@GetMapping("/getAuthToken/{token}")
|
||||
public R info(@PathVariable(name = "token") String token) {
|
||||
@@ -269,14 +286,30 @@ public class ThirdPartyOAth2Cotroller {
|
||||
@Operation(summary = "短信登录认证")
|
||||
@SysLog("短信登录认证")
|
||||
@Inner(value = false)
|
||||
public R loginSms(@RequestParam String mobilePhone) {
|
||||
public R loginSms(@RequestParam String mobilePhone,HttpServletRequest httpServletRequest) {
|
||||
Map<String, String> stringHashMap = new LinkedHashMap<>();
|
||||
String s = SmsCodeUtil.generateNumberCode(6);
|
||||
stringHashMap.put("code",s);
|
||||
stringHashMap.put("date","5");
|
||||
System.out.println(WebUtils.getIP());
|
||||
System.out.println(WebUtils.getIP(httpServletRequest));
|
||||
smsService.sendSms(mobilePhone, "20657",stringHashMap, WebUtils.getIP());
|
||||
|
||||
redisTemplate.opsForSet().add("smsCode_"+mobilePhone,s, 5, TimeUnit.MINUTES);
|
||||
|
||||
//记录信任的设备
|
||||
// 2. 记录信任设备
|
||||
// String deviceFingerprint = DeviceFingerprintUtil.generate(request);
|
||||
// deviceService.addTrustedDevice(
|
||||
// dto.getUserId(),
|
||||
// deviceFingerprint,
|
||||
// LocalDateTime.now().plusMonths(3) // 信任3个月
|
||||
// );
|
||||
//
|
||||
// // 3. 生成令牌
|
||||
// User user = userService.getById(dto.getUserId());
|
||||
// String token = jwtService.generateToken(user);
|
||||
// return R.ok().data("token", token);
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.bsd.pjcloud.thirdparty.utils;
|
||||
|
||||
import com.bsd.pjcloud.common.core.util.WebUtils;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.util.DigestUtils;
|
||||
|
||||
public class DeviceFingerprintUtil {
|
||||
public static String generate(HttpServletRequest request) {
|
||||
String userAgent = request.getHeader("User-Agent");
|
||||
String ip = WebUtils.getIP(request);
|
||||
String accept = request.getHeader("Accept");
|
||||
return DigestUtils.md5DigestAsHex((userAgent + ip + accept).getBytes());
|
||||
}
|
||||
}
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
package com.bsd.pjcloud.admin.api.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import com.baomidou.mybatisplus.extension.activerecord.Model;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 设备指纹
|
||||
*
|
||||
* @author zjf
|
||||
* @date 2025-04-22 17:14:57
|
||||
*/
|
||||
@Data
|
||||
@TableName("user_trusted_device")
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Schema(description = "设备指纹")
|
||||
public class UserTrustedDeviceEntity extends Model<UserTrustedDeviceEntity> {
|
||||
|
||||
|
||||
/**
|
||||
* 主键ID
|
||||
*/
|
||||
@TableId(type = IdType.ASSIGN_ID)
|
||||
@Schema(description="主键ID")
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 用户id
|
||||
*/
|
||||
@Schema(description="用户id")
|
||||
private Long userId;
|
||||
|
||||
/**
|
||||
* MD5
|
||||
*/
|
||||
@Schema(description="MD5")
|
||||
private String deviceFingerprint;
|
||||
|
||||
/**
|
||||
* 设备名称
|
||||
*/
|
||||
@Schema(description="设备名称")
|
||||
private String deviceName;
|
||||
|
||||
/**
|
||||
* 最后一次登录时间
|
||||
*/
|
||||
@Schema(description="最后一次登录时间")
|
||||
private LocalDateTime lastLoginTime;
|
||||
|
||||
/**
|
||||
* 过期时间
|
||||
*/
|
||||
@Schema(description="过期时间")
|
||||
private LocalDateTime expireTime;
|
||||
|
||||
/**
|
||||
* 创建人
|
||||
*/
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
@Schema(description="创建人")
|
||||
private String createBy;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
@Schema(description="创建时间")
|
||||
private LocalDateTime createTime;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||
@Schema(description="更新时间")
|
||||
private String updateBy;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||
@Schema(description="更新时间")
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
/**
|
||||
* 删除标记
|
||||
*/
|
||||
@TableLogic
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
@Schema(description="删除标记")
|
||||
private String delFlag;
|
||||
}
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
package com.bsd.pjcloud.admin.controller;
|
||||
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.bsd.pjcloud.common.core.util.R;
|
||||
import com.bsd.pjcloud.common.log.annotation.SysLog;
|
||||
import com.bsd.pjcloud.admin.api.entity.UserTrustedDeviceEntity;
|
||||
import com.bsd.pjcloud.admin.service.UserTrustedDeviceService;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import com.bsd.pjcloud.common.excel.annotation.ResponseExcel;
|
||||
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
||||
import org.springdoc.core.annotations.ParameterObject;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 设备指纹
|
||||
*
|
||||
* @author zjf
|
||||
* @date 2025-04-22 17:14:57
|
||||
*/
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/userTrustedDevice" )
|
||||
@Tag(description = "userTrustedDevice" , name = "设备指纹管理" )
|
||||
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
|
||||
public class UserTrustedDeviceController {
|
||||
|
||||
private final UserTrustedDeviceService userTrustedDeviceService;
|
||||
|
||||
/**
|
||||
* 分页查询
|
||||
* @param page 分页对象
|
||||
* @param userTrustedDevice 设备指纹
|
||||
* @return
|
||||
*/
|
||||
@Operation(summary = "分页查询" , description = "分页查询" )
|
||||
@GetMapping("/page" )
|
||||
@PreAuthorize("@pms.hasPermission('admin_userTrustedDevice_view')" )
|
||||
public R getUserTrustedDevicePage(@ParameterObject Page page, @ParameterObject UserTrustedDeviceEntity userTrustedDevice) {
|
||||
LambdaQueryWrapper<UserTrustedDeviceEntity> wrapper = Wrappers.lambdaQuery();
|
||||
return R.ok(userTrustedDeviceService.page(page, wrapper));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 通过id查询设备指纹
|
||||
* @param id id
|
||||
* @return R
|
||||
*/
|
||||
@Operation(summary = "通过id查询" , description = "通过id查询" )
|
||||
@GetMapping("/{id}" )
|
||||
@PreAuthorize("@pms.hasPermission('admin_userTrustedDevice_view')" )
|
||||
public R getById(@PathVariable("id" ) Long id) {
|
||||
return R.ok(userTrustedDeviceService.getById(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增设备指纹
|
||||
* @param userTrustedDevice 设备指纹
|
||||
* @return R
|
||||
*/
|
||||
@Operation(summary = "新增设备指纹" , description = "新增设备指纹" )
|
||||
@SysLog("新增设备指纹" )
|
||||
@PostMapping
|
||||
@PreAuthorize("@pms.hasPermission('admin_userTrustedDevice_add')" )
|
||||
public R save(@RequestBody UserTrustedDeviceEntity userTrustedDevice) {
|
||||
return R.ok(userTrustedDeviceService.save(userTrustedDevice));
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改设备指纹
|
||||
* @param userTrustedDevice 设备指纹
|
||||
* @return R
|
||||
*/
|
||||
@Operation(summary = "修改设备指纹" , description = "修改设备指纹" )
|
||||
@SysLog("修改设备指纹" )
|
||||
@PutMapping
|
||||
@PreAuthorize("@pms.hasPermission('admin_userTrustedDevice_edit')" )
|
||||
public R updateById(@RequestBody UserTrustedDeviceEntity userTrustedDevice) {
|
||||
return R.ok(userTrustedDeviceService.updateById(userTrustedDevice));
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过id删除设备指纹
|
||||
* @param ids id列表
|
||||
* @return R
|
||||
*/
|
||||
@Operation(summary = "通过id删除设备指纹" , description = "通过id删除设备指纹" )
|
||||
@SysLog("通过id删除设备指纹" )
|
||||
@DeleteMapping
|
||||
@PreAuthorize("@pms.hasPermission('admin_userTrustedDevice_del')" )
|
||||
public R removeById(@RequestBody Long[] ids) {
|
||||
return R.ok(userTrustedDeviceService.removeBatchByIds(CollUtil.toList(ids)));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 导出excel 表格
|
||||
* @param userTrustedDevice 查询条件
|
||||
* @param ids 导出指定ID
|
||||
* @return excel 文件流
|
||||
*/
|
||||
@ResponseExcel
|
||||
@GetMapping("/export")
|
||||
@PreAuthorize("@pms.hasPermission('admin_userTrustedDevice_export')" )
|
||||
public List<UserTrustedDeviceEntity> export(UserTrustedDeviceEntity userTrustedDevice,Long[] ids) {
|
||||
return userTrustedDeviceService.list(Wrappers.lambdaQuery(userTrustedDevice).in(ArrayUtil.isNotEmpty(ids), UserTrustedDeviceEntity::getId, ids));
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.bsd.pjcloud.admin.mapper;
|
||||
|
||||
import com.bsd.pjcloud.common.data.datascope.PjcloudBaseMapper;
|
||||
import com.bsd.pjcloud.admin.api.entity.UserTrustedDeviceEntity;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface UserTrustedDeviceMapper extends PjcloudBaseMapper<UserTrustedDeviceEntity> {
|
||||
|
||||
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.bsd.pjcloud.admin.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.bsd.pjcloud.admin.api.entity.UserTrustedDeviceEntity;
|
||||
|
||||
public interface UserTrustedDeviceService extends IService<UserTrustedDeviceEntity> {
|
||||
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.bsd.pjcloud.admin.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.bsd.pjcloud.admin.api.entity.UserTrustedDeviceEntity;
|
||||
import com.bsd.pjcloud.admin.mapper.UserTrustedDeviceMapper;
|
||||
import com.bsd.pjcloud.admin.service.UserTrustedDeviceService;
|
||||
import org.springframework.stereotype.Service;
|
||||
/**
|
||||
* 设备指纹
|
||||
*
|
||||
* @author zjf
|
||||
* @date 2025-04-22 17:14:57
|
||||
*/
|
||||
@Service
|
||||
public class UserTrustedDeviceServiceImpl extends ServiceImpl<UserTrustedDeviceMapper, UserTrustedDeviceEntity> implements UserTrustedDeviceService {
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?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.bsd.pjcloud.admin.mapper.UserTrustedDeviceMapper">
|
||||
|
||||
<resultMap id="userTrustedDeviceMap" type="com.bsd.pjcloud.admin.api.entity.UserTrustedDeviceEntity">
|
||||
<id property="id" column="id"/>
|
||||
<result property="userId" column="user_id"/>
|
||||
<result property="deviceFingerprint" column="device_fingerprint"/>
|
||||
<result property="deviceName" column="device_name"/>
|
||||
<result property="lastLoginTime" column="last_login_time"/>
|
||||
<result property="expireTime" column="expire_time"/>
|
||||
<result property="createBy" column="create_by"/>
|
||||
<result property="createTime" column="create_time"/>
|
||||
<result property="updateBy" column="update_by"/>
|
||||
<result property="updateTime" column="update_time"/>
|
||||
<result property="delFlag" column="del_flag"/>
|
||||
</resultMap>
|
||||
</mapper>
|
||||
Reference in new issue
Block a user