调整:1、图形验证码获取区分app和admin

2、请求接口默认设置上 userId 字段
This commit is contained in:
ruying408
2024-08-10 15:04:14 +08:00
parent 892176c073
commit c0bf5ae5f7
13 changed files with 182 additions and 21 deletions

View File

@@ -107,7 +107,7 @@ public abstract class BaseController<S extends BaseService<T>, T extends BaseEnt
.set("ids", service.addBatch(requestParams, array.toList(currentEntityClass()))));
} else {
return R.ok(Dict.create().set("id",
service.add(requestParams, JSONUtil.parseObj(body).toBean(currentEntityClass()))));
service.add(requestParams, requestParams.toBean(currentEntityClass()))));
}
}

View File

@@ -1,19 +1,21 @@
package com.cool.core.request;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.jwt.JWT;
import com.cool.core.enums.UserTypeEnum;
import com.cool.core.util.BodyReaderHttpServletRequestWrapper;
import com.cool.core.util.CoolSecurityUtil;
import jakarta.servlet.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import jakarta.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
* 封装请求参数 URL参数 和 body JSON 到同一个 JSONObject 方便读取
@@ -39,9 +41,14 @@ public class RequestParamsFilter implements Filter {
} else {
BodyReaderHttpServletRequestWrapper requestWrapper = new BodyReaderHttpServletRequestWrapper(request);
String body = requestWrapper.getBodyString(requestWrapper);
if (StrUtil.isNotEmpty(body) && JSONUtil.isJson(body) && !JSONUtil.isJsonArray(body)) {
if (StrUtil.isNotEmpty(body) && JSONUtil.isTypeJSON(body) && !JSONUtil.isTypeJSONArray(
body)) {
requestParams = JSONUtil.parseObj(body);
}
// 登录状态设置用户id
setUserId(requestParams);
requestParams.set("body", body);
requestParams.putAll(getAllRequestParam(request));
@@ -55,11 +62,26 @@ public class RequestParamsFilter implements Filter {
}
}
private void setUserId(JSONObject requestParams) {
UserTypeEnum userTypeEnum = CoolSecurityUtil.getCurrentUserType();
switch (userTypeEnum) {
// 只有登录了,才有用户类型, 不然为 UNKNOWN 状态
case ADMIN -> {
// 管理后台由于之前已经有逻辑再了,怕会影响到,如果自己有传了值不覆盖
Object o = requestParams.get("userId");
if (ObjUtil.isNotEmpty(o)) {
return;
}
requestParams.set("userId", CoolSecurityUtil.getCurrentUserId());
}
// app端userId 为当前登录的用户id
case APP -> requestParams.set("userId", CoolSecurityUtil.getCurrentUserId());
}
}
/**
* 获取客户端请求参数中所有的信息
*
* @param request
* @return
*/
private Map<String, Object> getAllRequestParam(final HttpServletRequest request) {
Map<String, Object> res = new HashMap<>();

View File

@@ -90,6 +90,8 @@ public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
authentication.setDetails(
new WebAuthenticationDetailsSource().buildDetails(request));
SecurityContextHolder.getContext().setAuthentication(authentication);
request.setAttribute("userId", jwt.getPayload("userId"));
request.setAttribute("tokenInfo", jwt);
}
}
}

View File

@@ -1,6 +1,7 @@
package com.cool.modules.base.controller.admin;
import com.cool.core.annotation.CoolRestController;
import com.cool.core.enums.UserTypeEnum;
import com.cool.core.eps.CoolEps;
import com.cool.core.file.FileUploadStrategyFactory;
import com.cool.core.request.R;
@@ -9,13 +10,12 @@ import com.cool.modules.base.service.sys.BaseSysLoginService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import jakarta.servlet.http.HttpServletRequest;
/**
* 系统开放接口,无需权限校验
*/
@@ -51,9 +51,9 @@ public class AdminBaseOpenController {
@Operation(summary = "验证码")
@GetMapping("/captcha")
public R captcha(@Parameter(description = "类型svg|base64") @RequestParam(defaultValue = "base64") String type,
@Parameter(description = "宽度") @RequestParam(defaultValue = "150") Integer width,
@Parameter(description = "高度") @RequestParam(defaultValue = "50") Integer height) {
return R.ok(baseSysLoginService.captcha(type, width, height));
@Parameter(description = "宽度") @RequestParam(defaultValue = "150") Integer width,
@Parameter(description = "高度") @RequestParam(defaultValue = "50") Integer height) {
return R.ok(baseSysLoginService.captcha(UserTypeEnum.ADMIN, type, width, height));
}
@Operation(summary = "刷新token")
@@ -65,7 +65,7 @@ public class AdminBaseOpenController {
@Operation(summary = "文件上传")
@PostMapping(value = "/upload", consumes = { MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.ALL_VALUE })
public R upload(@RequestPart(value = "file", required = false) @Parameter(description = "文件") MultipartFile[] files,
HttpServletRequest request) {
HttpServletRequest request) {
return R.ok(fileUploadStrategyFactory.upload(files, request));
}

View File

@@ -1,5 +1,6 @@
package com.cool.modules.base.service.sys;
import com.cool.core.enums.UserTypeEnum;
import com.cool.modules.base.dto.sys.BaseSysLoginDto;
/**
@@ -14,7 +15,7 @@ public interface BaseSysLoginService {
* @param height 高度
* @return base64 验证码与ID
*/
Object captcha(String type, Integer width, Integer height);
Object captcha(UserTypeEnum userTypeEnum, String type, Integer width, Integer height);
/**
* 校验验证码

View File

@@ -5,6 +5,7 @@ import cn.hutool.captcha.GifCaptcha;
import cn.hutool.captcha.generator.RandomGenerator;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.jwt.JWT;
@@ -44,14 +45,17 @@ public class BaseSysLoginServiceImpl implements BaseSysLoginService {
private final BaseSysPermsService baseSysPermsService;
@Override
public Object captcha(String type, Integer width, Integer height) {
public Object captcha(UserTypeEnum userTypeEnum, String type, Integer width, Integer height) {
// 1、生成验证码 2、生成对应的ID并设置在缓存中验证码过期时间30分钟
Map<String, Object> result = new HashMap<>();
String captchaId = StrUtil.uuid();
result.put("captchaId", captchaId);
RandomGenerator randomGenerator = new RandomGenerator(4);
GifCaptcha gifCaptcha = CaptchaUtil.createGifCaptcha(width, height);
gifCaptcha.setGenerator(randomGenerator);
if (ObjUtil.equals(userTypeEnum, UserTypeEnum.APP)) {
gifCaptcha.setGenerator(new RandomGenerator("0123456789", 4));
} else {
gifCaptcha.setGenerator(new RandomGenerator(4));
}
gifCaptcha.setBackground(new Color(248, 248, 248));
gifCaptcha.setMaxColor(60);
gifCaptcha.setMinColor(55);

View File

@@ -0,0 +1,41 @@
package com.cool.modules.user.controller.app;
import static com.cool.modules.user.entity.table.UserAddressEntityTableDef.USER_ADDRESS_ENTITY;
import cn.hutool.json.JSONObject;
import com.cool.core.annotation.CoolRestController;
import com.cool.core.base.BaseController;
import com.cool.core.request.R;
import com.cool.core.util.CoolSecurityUtil;
import com.cool.modules.user.entity.UserAddressEntity;
import com.cool.modules.user.service.UserAddressService;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
/**
* 用户模块-收货地址
*/
@Tag(name = "用户模块-收货地址", description = "用户模块-收货地址")
@CoolRestController(api = {"add", "delete", "update", "page", "list", "info"})
public class AppUserAddressController extends BaseController<UserAddressService, UserAddressEntity> {
@Override
protected void init(HttpServletRequest request, JSONObject requestParams) {
setPageOption(
createOp()
.queryWrapper(
QueryWrapper.create()
.and(USER_ADDRESS_ENTITY.USER_ID.eq(CoolSecurityUtil.getCurrentUserId()))
.orderBy(
USER_ADDRESS_ENTITY.IS_DEFAULT.getName(), false)));
}
@Operation(summary = "默认地址", description = "默认地址")
@GetMapping("/default")
public R getDefault() {
Long userId = CoolSecurityUtil.getCurrentUserId();
return R.ok(this.service.getDefault(userId));
}
}

View File

@@ -2,6 +2,7 @@ package com.cool.modules.user.controller.app;
import com.cool.core.annotation.CoolRestController;
import com.cool.core.annotation.TokenIgnore;
import com.cool.core.enums.UserTypeEnum;
import com.cool.core.exception.CoolPreconditions;
import com.cool.core.request.R;
import com.cool.modules.base.service.sys.BaseSysLoginService;
@@ -14,6 +15,7 @@ import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@@ -121,7 +123,7 @@ public class AppUserLoginController {
@Operation(summary = "图片验证码")
@GetMapping("/captcha")
public R captcha(
@RequestBody CaptchaParam param) {
@ModelAttribute CaptchaParam param) {
String type = param.getType();
Integer width = param.getWidth();
Integer height = param.getHeight();
@@ -130,7 +132,7 @@ public class AppUserLoginController {
CoolPreconditions.checkEmpty(width);
CoolPreconditions.checkEmpty(height);
return R.ok(baseSysLoginService.captcha(type, width, height));
return R.ok(baseSysLoginService.captcha(UserTypeEnum.APP, type, width, height));
}
/**

View File

@@ -7,6 +7,6 @@ import lombok.Setter;
@Getter
public class CaptchaParam {
private String type;
private Integer width;
private Integer height;
private Integer width = 150;
private Integer height = 50;
}

View File

@@ -0,0 +1,43 @@
package com.cool.modules.user.entity;
import com.cool.core.base.BaseEntity;
import com.mybatisflex.annotation.Table;
import com.tangzc.autotable.annotation.Index;
import com.tangzc.mybatisflex.autotable.annotation.ColumnDefine;
import lombok.Getter;
import lombok.Setter;
/**
* 用户模块-收货地址
*/
@Getter
@Setter
@Table(value = "user_address", comment = "用户模块-收货地址")
public class UserAddressEntity extends BaseEntity<UserAddressEntity> {
@Index
@ColumnDefine(comment = "用户ID", notNull = true)
private Long userId;
@ColumnDefine(comment = "联系人", notNull = true)
private String contact;
@Index
@ColumnDefine(comment = "手机号", length = 11, notNull = true)
private String phone;
@ColumnDefine(comment = "", notNull = true)
private String province;
@ColumnDefine(comment = "", notNull = true)
private String city;
@ColumnDefine(comment = "", notNull = true)
private String district;
@ColumnDefine(comment = "地址", notNull = true)
private String address;
@ColumnDefine(comment = "是否默认", defaultValue = "false")
private Boolean isDefault;
}

View File

@@ -0,0 +1,10 @@
package com.cool.modules.user.mapper;
import com.cool.modules.user.entity.UserAddressEntity;
import com.mybatisflex.core.BaseMapper;
/**
* 用户模块-收货地址
*/
public interface UserAddressMapper extends BaseMapper<UserAddressEntity> {
}

View File

@@ -0,0 +1,15 @@
package com.cool.modules.user.service;
import com.cool.core.base.BaseService;
import com.cool.modules.user.entity.UserAddressEntity;
/**
* 用户模块-收货地址
*/
public interface UserAddressService extends BaseService<UserAddressEntity> {
/**
* 获取默认地址
*/
Object getDefault(Long userId);
}

View File

@@ -0,0 +1,21 @@
package com.cool.modules.user.service.impl;
import com.cool.core.base.BaseServiceImpl;
import com.cool.modules.user.entity.UserAddressEntity;
import com.cool.modules.user.mapper.UserAddressMapper;
import com.cool.modules.user.service.UserAddressService;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.stereotype.Service;
/**
* 用户模块-收货地址
*/
@Service
public class UserAddressServiceImpl extends BaseServiceImpl<UserAddressMapper, UserAddressEntity> implements UserAddressService {
@Override
public Object getDefault(Long userId) {
return this.getOne(QueryWrapper.create().eq(UserAddressEntity::getUserId, userId)
.eq(UserAddressEntity::getIsDefault, true).limit(1));
}
}