feat(修改): 🚀 修改视图模型位置以及可以使用Web自带拦截器,可以选择性使用

This commit is contained in:
bunny 2024-05-24 14:21:00 +08:00
parent 50c67dbd5d
commit 05535f66d0
82 changed files with 186 additions and 105 deletions

View File

@ -20,7 +20,7 @@
<dependencies>
<dependency>
<groupId>cn.bunny</groupId>
<artifactId>model</artifactId>
<artifactId>dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>

View File

@ -1,51 +0,0 @@
package cn.bunny.common.utils;
import cn.bunny.vo.system.comment.CommentVo;
import java.util.ArrayList;
import java.util.List;
public class CommentUtil {
/**
* 构建树型结构
*
* @param commentList 评论列表
* @return 结构列表
*/
public static List<CommentVo> buildTree(List<CommentVo> commentList) {
// 构建树形结构
List<CommentVo> tree = new ArrayList<>();
// 遍历评论列表
for (CommentVo comment : commentList) {
// 找到顶级评论没有父评论
if (comment.getPCommentId() == 0) {
// 递归构建子评论
comment.setChildren(getChildren(comment.getId(), commentList));
tree.add(comment);
}
}
return tree;
}
/**
* 递归获取子评论
*
* @param commentId 当前评论ID
* @param commentList 评论列表
* @return 子评论列表
*/
private static List<CommentVo> getChildren(Long commentId, List<CommentVo> commentList) {
List<CommentVo> children = new ArrayList<>();
// 遍历评论列表
for (CommentVo comment : commentList) {
// 找到当前评论的子评论
if (Long.valueOf(comment.getPCommentId()).equals(commentId)) {
// 递归构建子评论的子评论
comment.setChildren(getChildren(comment.getId(), commentList));
children.add(comment);
}
}
return children;
}
}

View File

@ -1,5 +1,8 @@
package cn.bunny.common.utils;
/**
* 计算 kb mb gb
*/
public class FileUtil {
public static String getSize(Long fileSize) {
double fileSizeInKB = fileSize / 1024.00;

View File

@ -19,7 +19,7 @@
<dependencies>
<dependency>
<groupId>cn.bunny</groupId>
<artifactId>model</artifactId>
<artifactId>dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- httpclient -->

View File

@ -0,0 +1,38 @@
package cn.bunny.common.service.config;
import cn.bunny.common.service.interceptor.UserTokenInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@Slf4j
public class WebMvcConfiguration implements WebMvcConfigurer {
@Autowired
private UserTokenInterceptor userTokenInterceptor;
/**
* 跨域配置
*
* @param registry 跨域注册表
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
// 是否发送Cookies
.allowCredentials(true)
// 放行哪些原始域
.allowedOriginPatterns("*").allowedMethods("GET", "POST", "PUT", "DELETE").allowedHeaders("*").exposedHeaders("*");
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
String[] excludeList = {"/api/checkCode", "/api/sendEmailCode", "/api/register", "/api/login", "/api/article/loadArticle/**"};
log.info("WebMvcConfiguration===>开始注册自定义拦截器...");
// TODO 如果想使用普通JWT可以使用这个不使用 SpringSecurity6
// registry.addInterceptor(userTokenInterceptor).addPathPatterns("/api/**").excludePathPatterns(excludeList);
}
}

View File

@ -1,6 +1,6 @@
package cn.bunny.common.service.exception;
import cn.bunny.result.ResultCodeEnum;
import cn.bunny.pojo.result.ResultCodeEnum;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

View File

@ -1,9 +1,9 @@
package cn.bunny.common.service.exception;
import cn.bunny.result.Result;
import cn.bunny.result.ResultCodeEnum;
import cn.bunny.result.constant.ExceptionConstant;
import cn.bunny.pojo.result.Result;
import cn.bunny.pojo.result.ResultCodeEnum;
import cn.bunny.pojo.result.constant.ExceptionConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

View File

@ -0,0 +1,54 @@
package cn.bunny.common.service.interceptor;
import cn.bunny.common.service.context.BaseContext;
import cn.bunny.common.service.utils.JwtHelper;
import cn.bunny.common.service.utils.ResponseUtil;
import cn.bunny.pojo.result.Result;
import cn.bunny.pojo.result.ResultCodeEnum;
import cn.bunny.pojo.result.constant.RedisUserConstant;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
@Component
@Slf4j
public class UserTokenInterceptor implements HandlerInterceptor {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.info("UserTokenInterceptor===>设置拦截器");
String token = request.getHeader("token");
Long userId = JwtHelper.getUserId(token);
String username = JwtHelper.getUsername(token);
Object redisUserinfo = redisTemplate.opsForValue().get(RedisUserConstant.getUserLoginInfoPrefix(username));
// 不是动态方法直接返回
if (!(handler instanceof HandlerMethod)) return true;
// 解析不到userId
if (userId == null) {
ResponseUtil.out(response, Result.error(ResultCodeEnum.LOGIN_AUTH));
return false;
}
if (redisUserinfo == null) {
ResponseUtil.out(response, Result.error(ResultCodeEnum.LOGIN_AUTH));
return false;
}
BaseContext.setUserId(userId);
BaseContext.setUsername(username);
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
BaseContext.removeUser();
}
}

View File

@ -1,6 +1,6 @@
package cn.bunny.common.service.utils;
import cn.bunny.result.Result;
import cn.bunny.pojo.result.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import jakarta.servlet.http.HttpServletResponse;

View File

@ -7,7 +7,7 @@
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>model</artifactId>
<artifactId>dao</artifactId>
<packaging>jar</packaging>
<name>model</name>

View File

@ -1,4 +1,4 @@
package cn.bunny.enums;
package cn.bunny.pojo.enums;
/**
* 数据库操作类型

View File

@ -1,4 +1,4 @@
package cn.bunny.result;
package cn.bunny.pojo.result;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package cn.bunny.result;
package cn.bunny.pojo.result;
import lombok.Getter;

View File

@ -1,4 +1,4 @@
package cn.bunny.result.constant;
package cn.bunny.pojo.result.constant;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package cn.bunny.result.constant;
package cn.bunny.pojo.result.constant;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package cn.bunny.result.constant;
package cn.bunny.pojo.result.constant;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package cn.bunny.result.constant;
package cn.bunny.pojo.result.constant;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package cn.bunny.result.constant;
package cn.bunny.pojo.result.constant;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package cn.bunny.result.constant;
package cn.bunny.pojo.result.constant;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package cn.bunny.result.constant;
package cn.bunny.pojo.result.constant;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package cn.bunny.result.constant;
package cn.bunny.pojo.result.constant;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package cn.bunny.result.constant;
package cn.bunny.pojo.result.constant;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package cn.bunny.tree;
package cn.bunny.pojo.tree;
import java.util.List;

View File

@ -1,4 +1,4 @@
package cn.bunny.tree;
package cn.bunny.pojo.tree;
import java.util.ArrayList;
import java.util.List;

View File

@ -1,6 +1,6 @@
package cn.bunny.vo.system.board;
import cn.bunny.tree.AbstractTreeNode;
import cn.bunny.pojo.tree.AbstractTreeNode;
import lombok.Data;
import java.util.List;

View File

@ -1,6 +1,6 @@
package cn.bunny.vo.system.login;
import cn.bunny.result.constant.LocalDateTimeConstant;
import cn.bunny.pojo.result.constant.LocalDateTimeConstant;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

View File

@ -1,6 +1,6 @@
package cn.bunny.vo.system.user;
import cn.bunny.result.constant.LocalDateTimeConstant;
import cn.bunny.pojo.result.constant.LocalDateTimeConstant;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

View File

@ -2,7 +2,7 @@ package cn.bunny.module.mail.utils;
import cn.bunny.common.service.utils.EmptyUtil;
import cn.bunny.entity.system.email.EmailSend;
import cn.bunny.result.constant.MailMessageConstant;
import cn.bunny.pojo.result.constant.MailMessageConstant;
public class MailSendCheckUtil {
/**

View File

@ -1,7 +1,7 @@
package cn.bunny.module.minio.utils;
import cn.bunny.result.constant.FileMessageConstant;
import cn.bunny.common.service.exception.BunnyException;
import cn.bunny.pojo.result.constant.FileMessageConstant;
import io.minio.*;
import io.minio.messages.*;
import lombok.extern.slf4j.Slf4j;

View File

@ -17,6 +17,7 @@
<module>module-mail</module>
<module>module-rabbitMQ</module>
<module>module-websocket</module>
<module>spring-security</module>
<module>module-task</module>
</modules>

View File

@ -4,9 +4,9 @@ import cn.bunny.common.service.context.BaseContext;
import cn.bunny.common.service.exception.BunnyException;
import cn.bunny.common.service.utils.JwtHelper;
import cn.bunny.common.service.utils.ResponseUtil;
import cn.bunny.result.Result;
import cn.bunny.result.ResultCodeEnum;
import cn.bunny.result.constant.RedisUserConstant;
import cn.bunny.pojo.result.Result;
import cn.bunny.pojo.result.ResultCodeEnum;
import cn.bunny.pojo.result.constant.RedisUserConstant;
import cn.bunny.vo.system.login.LoginVo;
import com.alibaba.fastjson2.JSON;
import jakarta.servlet.FilterChain;

View File

@ -3,9 +3,9 @@ package cn.bunny.security.filter;
import cn.bunny.common.service.utils.ResponseUtil;
import cn.bunny.dto.user.LoginDto;
import cn.bunny.result.Result;
import cn.bunny.result.ResultCodeEnum;
import cn.bunny.result.constant.RedisUserConstant;
import cn.bunny.pojo.result.Result;
import cn.bunny.pojo.result.ResultCodeEnum;
import cn.bunny.pojo.result.constant.RedisUserConstant;
import cn.bunny.security.handelr.SecurityAuthenticationFailureHandler;
import cn.bunny.security.handelr.SecurityAuthenticationSuccessHandler;
import cn.bunny.security.service.CustomUserDetailsService;

View File

@ -1,7 +1,7 @@
package cn.bunny.security.handelr;
import cn.bunny.result.Result;
import cn.bunny.result.ResultCodeEnum;
import cn.bunny.pojo.result.Result;
import cn.bunny.pojo.result.ResultCodeEnum;
import com.alibaba.fastjson2.JSON;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

View File

@ -1,8 +1,8 @@
package cn.bunny.security.handelr;
import cn.bunny.common.service.utils.ResponseUtil;
import cn.bunny.result.Result;
import cn.bunny.result.ResultCodeEnum;
import cn.bunny.pojo.result.Result;
import cn.bunny.pojo.result.ResultCodeEnum;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

View File

@ -1,6 +1,6 @@
package cn.bunny.security.handelr;
import cn.bunny.result.Result;
import cn.bunny.pojo.result.Result;
import com.alibaba.fastjson2.JSON;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;

View File

@ -1,6 +1,6 @@
package cn.bunny.security.handelr;
import cn.bunny.result.Result;
import cn.bunny.pojo.result.Result;
import com.alibaba.fastjson2.JSON;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

View File

@ -17,7 +17,7 @@
<modules>
<module>common</module>
<module>model</module>
<module>dao</module>
<module>service</module>
<module>module</module>
</modules>

View File

@ -1,6 +1,6 @@
package cn.bunny.service.aop.annotation;
import cn.bunny.enums.OperationType;
import cn.bunny.pojo.enums.OperationType;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;

View File

@ -2,7 +2,7 @@ package cn.bunny.service.controller;
import cn.bunny.dto.user.LoginDto;
import cn.bunny.result.Result;
import cn.bunny.pojo.result.Result;
import cn.bunny.service.service.UserService;
import cn.bunny.vo.system.login.LoginVo;
import io.swagger.v3.oas.annotations.Operation;

View File

@ -1,13 +1,20 @@
package cn.bunny.service.security;
import cn.bunny.common.service.utils.JwtHelper;
import cn.bunny.entity.system.admin.AdminPower;
import cn.bunny.security.service.CustomAuthorizationManagerService;
import cn.bunny.service.mapper.AdminPowerMapper;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authorization.AuthorizationDecision;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.web.access.intercept.RequestAuthorizationContext;
import org.springframework.stereotype.Service;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.function.Supplier;
@ -15,22 +22,51 @@ import java.util.function.Supplier;
* 自定义权限判断
* 判断用户有哪些权限
*/
@Service
@Component
@Slf4j
public class CustomAuthorizationManagerServiceImpl implements CustomAuthorizationManagerService {
@Autowired
private AdminPowerMapper adminPowerMapper;
@Override
public void verify(Supplier<Authentication> authentication, RequestAuthorizationContext requestAuthorizationContext) {
CustomAuthorizationManagerService.super.verify(authentication, requestAuthorizationContext);
}
@Override
public AuthorizationDecision check(Supplier<Authentication> authentication, RequestAuthorizationContext object) {
String token = object.getRequest().getHeader("token");
public AuthorizationDecision check(Supplier<Authentication> authentication, RequestAuthorizationContext context) {
// 用户的token和用户id请求Url
HttpServletRequest request = context.getRequest();
String token = request.getHeader("token");
Long userId = JwtHelper.getUserId(token);// 用户id
String requestURI = request.getRequestURI();// 请求地址
String method = request.getMethod();// 请求方式
List<String> roleCodeList = authentication.get().getAuthorities().stream().map(GrantedAuthority::getAuthority).toList();// 角色代码列表
if (token == null) {
throw new AccessDeniedException("");
}
return new AuthorizationDecision(true);
return new AuthorizationDecision(hasRoleList(requestURI, method, userId));
}
/**
* 查询用户所属的角色信息
*
* @param requestURI 请求url地址
* @param method 请求方式
* @param userId 用户id
*/
private Boolean hasRoleList(String requestURI, String method, Long userId) {
// 查询用户权限
List<AdminPower> powerList = adminPowerMapper.queryByUserIdWithPower(userId);
// 如果查询到当前地址符合这个地址
for (AdminPower adminPower : powerList) {
String description = adminPower.getDescription();
if (description.equals(requestURI) || requestURI.matches(description)) {
return true;
}
}
return false;
}
}

View File

@ -10,14 +10,14 @@ import cn.bunny.service.service.UserService;
import cn.bunny.vo.system.login.LoginVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import java.util.List;
@Configuration
@Component
public class CustomUserDetailsService implements cn.bunny.security.service.CustomUserDetailsService {
@Autowired
private UserMapper userMapper;
@ -51,4 +51,4 @@ public class CustomUserDetailsService implements cn.bunny.security.service.Custo
public LoginVo login(LoginDto loginDto) {
return userService.login(loginDto);
}
}
}

View File

@ -10,9 +10,9 @@ import cn.bunny.entity.system.email.EmailSendInit;
import cn.bunny.entity.system.email.EmailUsers;
import cn.bunny.entity.system.user.User;
import cn.bunny.module.mail.utils.MailSenderUtil;
import cn.bunny.result.constant.ExceptionConstant;
import cn.bunny.result.constant.MailMessageConstant;
import cn.bunny.result.constant.RedisUserConstant;
import cn.bunny.pojo.result.constant.ExceptionConstant;
import cn.bunny.pojo.result.constant.MailMessageConstant;
import cn.bunny.pojo.result.constant.RedisUserConstant;
import cn.bunny.service.mapper.AdminPowerMapper;
import cn.bunny.service.mapper.AdminRoleMapper;
import cn.bunny.service.mapper.EmailUsersMapper;