feat(新增): 调整项目顺序

This commit is contained in:
bunny 2024-03-23 14:35:11 +08:00
parent 56f7d4a8a5
commit 786c8a7183
25 changed files with 162 additions and 18 deletions

View File

@ -0,0 +1,41 @@
package com.atguigu.utils;
import com.alibaba.fastjson.JSON;
import com.atguigu.constant.MessageConstant;
import com.atguigu.spzx.model.vo.result.Result;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
@Slf4j
public class InterceptorUtil {
/**
* 用户未登录返回响应
*
* @param response 返回体
*/
public static void unLoginInterceptor(HttpServletResponse response) throws IOException {
log.warn("InterceptorUtil===>用户未登录");
customLoginInterceptor(response, MessageConstant.USER_NOT_LOGIN, "");
}
/**
* 构建用户登录失败
*
* @param response HttpServletResponse
* @param message 消息
* @param data 内容
*/
public static void customLoginInterceptor(HttpServletResponse response, String message, String data) throws IOException {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
Result<String> result = new Result<>();
result.setCode(HttpServletResponse.SC_UNAUTHORIZED);
result.setData(data);
result.setMessage(message);
// 将消息写入响应体
response.getWriter().write(JSON.toJSONString(result));
}
}

View File

@ -1,10 +1,10 @@
package com.atguigu.exception;
package com.atguigu.constant;
import lombok.Data;
@Data
public class EnumException {
public class ExceptionConstant {
public static final String USER_NOT_FOUND = "用户不存在";
public static final String USERNAME_IS_EMPTY = "用户名不能为空";
public static final String PASSWORD_ERROR = "密码错误";

View File

@ -1,12 +1,15 @@
package com.atguigu.context;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.entity.user.UserInfo;
public class BaseContext {
public static ThreadLocal<Long> threadLocal = new ThreadLocal<>();
public static ThreadLocal<SysUser> sysUserThreadLocal = new ThreadLocal<>();
public static ThreadLocal<UserInfo> userInfoThreadLocal = new ThreadLocal<>();
/**
* 获取当前用户id
*
* @return 用户id
*/
public static Long getUserId() {
return threadLocal.get();
@ -14,17 +17,57 @@ public class BaseContext {
/**
* 设置用户id
*
* @param userId 用户id
*/
public static void setUserId(Long userId) {
threadLocal.set(userId);
}
/**
* 获取当前 系统用户实体类
*/
public static SysUser getSysUser() {
return sysUserThreadLocal.get();
}
/**
* 设置当前 系统用户实体类
*/
public static void setSysUser(SysUser sysUser) {
sysUserThreadLocal.set(sysUser);
}
/**
* 获取当前 用户实体类
*/
public static UserInfo getUserInfo() {
return userInfoThreadLocal.get();
}
/**
* 设置当前 用户实体类
*/
public static void setUserInfo(UserInfo userInfo) {
userInfoThreadLocal.set(userInfo);
}
/**
* 移出当前id
*/
public static void removeCurrentId() {
public static void remove() {
threadLocal.remove();
}
/**
* 移出当前 系统用户实体类
*/
public static void removeSysUser() {
sysUserThreadLocal.remove();
}
/**
* 移出当前 用户实体类
*/
public static void removeUserInfo() {
userInfoThreadLocal.remove();
}
}

View File

@ -0,0 +1,46 @@
package com.atguigu.json;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import static com.atguigu.constant.LocalDateTimeConstant.*;
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
/**
* 对象映射器:基于jackson将Java对象转为json或者将json转为Java对象
* 将JSON解析为Java对象的过程称为 [从JSON反序列化Java对象]
* 从Java对象生成JSON的过程称为 [序列化Java对象到JSON]
*/
public class JacksonObjectMapper extends ObjectMapper {
public JacksonObjectMapper() {
super();
// 收到未知属性时不报异常
this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
// 反序列化时属性不存在的兼容处理
this.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
SimpleModule simpleModule = new SimpleModule()
.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)))
.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
// 注册功能模块 例如可以添加自定义序列化器和反序列化器
this.registerModule(simpleModule);
}
}

View File

@ -1,4 +1,4 @@
package com.atguigu.utils;
package com.atguigu.lib;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

View File

@ -0,0 +1,14 @@
package com.atguigu.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.List;
@Configuration
@ConfigurationProperties(prefix = "bunny.spzx")
@Data
public class InterceptorsProperties {
private List<String> noAuthUrls;
}

View File

@ -34,8 +34,8 @@ logging:
mybatis:
type-aliases-package: com.atguigu.spzx.model
mapper-locations: classpath:mapper/*.xml
config-location: classpath:mybatis-config.xml
# configuration:
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# map-underscore-to-camel-case: true
# auto-mapping-behavior: full
# config-location: classpath:mybatis-config.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
auto-mapping-behavior: full

View File

@ -34,8 +34,8 @@ logging:
mybatis:
type-aliases-package: com.atguigu.spzx.model
mapper-locations: classpath:mapper/*.xml
config-location: classpath:mybatis-config.xml
# configuration:
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# map-underscore-to-camel-case: true
# auto-mapping-behavior: full
# config-location: classpath:mybatis-config.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
auto-mapping-behavior: full