代码优化
This commit is contained in:
parent
5217cc32fa
commit
98e5db30cc
@ -5,5 +5,6 @@ public class FileConstant {
|
|||||||
* 路径分隔符
|
* 路径分隔符
|
||||||
*/
|
*/
|
||||||
public static final String pathSeparator = "/";
|
public static final String pathSeparator = "/";
|
||||||
|
public static final int deleteFileRandomSize = 999999;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.qiwenshare.common.util;
|
package com.qiwenshare.common.util;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.RandomUtil;
|
||||||
import com.qiwenshare.common.config.PropertiesUtil;
|
import com.qiwenshare.common.config.PropertiesUtil;
|
||||||
import com.qiwenshare.common.constant.FileConstant;
|
import com.qiwenshare.common.constant.FileConstant;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
@ -73,9 +74,10 @@ public class PathUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
String path = "aaa/bbb/ccc/";
|
System.out.println(RandomUtil.randomLong(999999));
|
||||||
System.out.println(getParentPath(path));
|
// String path = "aaa/bbb/ccc/";
|
||||||
String fileName = path.substring(path.lastIndexOf("/"));
|
// System.out.println(getParentPath(path));
|
||||||
System.out.println(fileName);
|
// String fileName = path.substring(path.lastIndexOf("/"));
|
||||||
|
// System.out.println(fileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,9 +10,7 @@ import com.qiwenshare.file.mapper.UserFileMapper;
|
|||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Queue;
|
import java.util.Queue;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@ -109,68 +107,65 @@ public class FileDealComp {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//
|
|
||||||
// public TreeNode insertTreeNode(TreeNode treeNode, String filePath, Queue<String> nodeNameQueue){
|
public TreeNode insertTreeNode(TreeNode treeNode, long id, String filePath, Queue<String> nodeNameQueue){
|
||||||
//
|
|
||||||
// List<TreeNode> childrenTreeNodes = treeNode.getChildren();
|
List<TreeNode> childrenTreeNodes = treeNode.getChildren();
|
||||||
// String currentNodeName = nodeNameQueue.peek();
|
String currentNodeName = nodeNameQueue.peek();
|
||||||
// if (currentNodeName == null){
|
if (currentNodeName == null){
|
||||||
// return treeNode;
|
return treeNode;
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// Map<String, String> map = new HashMap<>();
|
filePath = filePath + currentNodeName + "/";
|
||||||
// filePath = filePath + currentNodeName + "/";
|
|
||||||
// map.put("filePath", filePath);
|
if (!isExistPath(childrenTreeNodes, currentNodeName)){ //1、判断有没有该子节点,如果没有则插入
|
||||||
//
|
//插入
|
||||||
// if (!isExistPath(childrenTreeNodes, currentNodeName)){ //1、判断有没有该子节点,如果没有则插入
|
TreeNode resultTreeNode = new TreeNode();
|
||||||
// //插入
|
|
||||||
// TreeNode resultTreeNode = new TreeNode();
|
resultTreeNode.setFilePath(filePath);
|
||||||
//
|
resultTreeNode.setLabel(nodeNameQueue.poll());
|
||||||
//
|
resultTreeNode.setId(++id);
|
||||||
// resultTreeNode.setAttributes(map);
|
|
||||||
// resultTreeNode.setLabel(nodeNameQueue.poll());
|
childrenTreeNodes.add(resultTreeNode);
|
||||||
// resultTreeNode.setId(treeid++);
|
|
||||||
//
|
}else{ //2、如果有,则跳过
|
||||||
// childrenTreeNodes.add(resultTreeNode);
|
nodeNameQueue.poll();
|
||||||
//
|
}
|
||||||
// }else{ //2、如果有,则跳过
|
|
||||||
// nodeNameQueue.poll();
|
if (nodeNameQueue.size() != 0) {
|
||||||
// }
|
for (int i = 0; i < childrenTreeNodes.size(); i++) {
|
||||||
//
|
|
||||||
// if (nodeNameQueue.size() != 0) {
|
TreeNode childrenTreeNode = childrenTreeNodes.get(i);
|
||||||
// for (int i = 0; i < childrenTreeNodes.size(); i++) {
|
if (currentNodeName.equals(childrenTreeNode.getLabel())){
|
||||||
//
|
childrenTreeNode = insertTreeNode(childrenTreeNode, id * 10, filePath, nodeNameQueue);
|
||||||
// TreeNode childrenTreeNode = childrenTreeNodes.get(i);
|
childrenTreeNodes.remove(i);
|
||||||
// if (currentNodeName.equals(childrenTreeNode.getLabel())){
|
childrenTreeNodes.add(childrenTreeNode);
|
||||||
// childrenTreeNode = insertTreeNode(childrenTreeNode, filePath, nodeNameQueue);
|
treeNode.setChildren(childrenTreeNodes);
|
||||||
// childrenTreeNodes.remove(i);
|
}
|
||||||
// childrenTreeNodes.add(childrenTreeNode);
|
|
||||||
// treeNode.setChildren(childrenTreeNodes);
|
}
|
||||||
// }
|
}else{
|
||||||
//
|
treeNode.setChildren(childrenTreeNodes);
|
||||||
// }
|
}
|
||||||
// }else{
|
|
||||||
// treeNode.setChildren(childrenTreeNodes);
|
return treeNode;
|
||||||
// }
|
|
||||||
//
|
}
|
||||||
// return treeNode;
|
|
||||||
//
|
public boolean isExistPath(List<TreeNode> childrenTreeNodes, String path){
|
||||||
// }
|
boolean isExistPath = false;
|
||||||
//
|
|
||||||
// public boolean isExistPath(List<TreeNode> childrenTreeNodes, String path){
|
try {
|
||||||
// boolean isExistPath = false;
|
for (int i = 0; i < childrenTreeNodes.size(); i++){
|
||||||
//
|
if (path.equals(childrenTreeNodes.get(i).getLabel())){
|
||||||
// try {
|
isExistPath = true;
|
||||||
// for (int i = 0; i < childrenTreeNodes.size(); i++){
|
}
|
||||||
// if (path.equals(childrenTreeNodes.get(i).getLabel())){
|
}
|
||||||
// isExistPath = true;
|
}catch (Exception e){
|
||||||
// }
|
e.printStackTrace();
|
||||||
// }
|
}
|
||||||
// }catch (Exception e){
|
|
||||||
// e.printStackTrace();
|
|
||||||
// }
|
return isExistPath;
|
||||||
//
|
}
|
||||||
//
|
|
||||||
// return isExistPath;
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,58 @@
|
|||||||
|
package com.qiwenshare.file.component;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.qiwenshare.file.domain.UserBean;
|
||||||
|
import com.qiwenshare.file.mapper.UserMapper;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class UserDealComp {
|
||||||
|
@Resource
|
||||||
|
UserMapper userMapper;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检测用户名是否存在
|
||||||
|
*
|
||||||
|
* @param userBean
|
||||||
|
*/
|
||||||
|
public Boolean isUserNameExit(UserBean userBean) {
|
||||||
|
LambdaQueryWrapper<UserBean> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
||||||
|
lambdaQueryWrapper.eq(UserBean::getUsername, userBean.getUsername());
|
||||||
|
List<UserBean> list = userMapper.selectList(lambdaQueryWrapper);
|
||||||
|
if (list != null && !list.isEmpty()) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检测手机号是否存在
|
||||||
|
*
|
||||||
|
* @param userBean
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public Boolean isPhoneExit(UserBean userBean) {
|
||||||
|
|
||||||
|
LambdaQueryWrapper<UserBean> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
||||||
|
lambdaQueryWrapper.eq(UserBean::getTelephone, userBean.getTelephone());
|
||||||
|
List<UserBean> list = userMapper.selectList(lambdaQueryWrapper);
|
||||||
|
if (list != null && !list.isEmpty()) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean isPhoneFormatRight(String phone){
|
||||||
|
String regex = "^1\\d{10}";
|
||||||
|
boolean isRight = Pattern.matches(regex, phone);
|
||||||
|
return isRight;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,52 @@
|
|||||||
|
package com.qiwenshare.file.config;
|
||||||
|
|
||||||
|
|
||||||
|
import com.qiwenshare.file.interceptor.AuthenticationInterceptor;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class WebMvcConfig implements WebMvcConfigurer {
|
||||||
|
@Autowired
|
||||||
|
private AuthenticationInterceptor authenticationInterceptor;
|
||||||
|
|
||||||
|
// @Override
|
||||||
|
// public void addCorsMappings(CorsRegistry registry) {
|
||||||
|
// registry.addMapping("/**").allowedOriginPatterns("*").allowCredentials(true).maxAge(3600).allowedMethods("GET", "POST","OPTIONS");
|
||||||
|
// }
|
||||||
|
|
||||||
|
// @Override
|
||||||
|
// protected void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||||
|
// //默认的资源映射需要填写,不然不能正常访问
|
||||||
|
// registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
|
||||||
|
// //配置外部资源目录的映射,/image目录为前端访问的路径,后面配置静态资源的绝对路径
|
||||||
|
// registry.addResourceHandler("/image/**").addResourceLocations("file:"+uploadFolder);
|
||||||
|
// //调用基类的方法
|
||||||
|
// super.addResourceHandlers(registry);
|
||||||
|
// }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* app拦截器
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
|
|
||||||
|
List<String> list = new ArrayList<>();
|
||||||
|
list.add("/file/**");
|
||||||
|
list.add("/filetransfer/**");
|
||||||
|
list.add("/recoveryfile/**");
|
||||||
|
list.add("/share/**");
|
||||||
|
registry.addInterceptor(authenticationInterceptor)
|
||||||
|
.addPathPatterns(list)
|
||||||
|
.excludePathPatterns("/filetransfer/downloadfile", "/filetransfer/preview");
|
||||||
|
// //登录、登出
|
||||||
|
// .excludePathPatterns("/user/checkuserlogininfo", "/user/login", "/user/register");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -17,6 +17,7 @@ import com.qiwenshare.common.util.PathUtil;
|
|||||||
import com.qiwenshare.file.anno.MyLog;
|
import com.qiwenshare.file.anno.MyLog;
|
||||||
import com.qiwenshare.file.api.*;
|
import com.qiwenshare.file.api.*;
|
||||||
import com.qiwenshare.common.config.QiwenFileConfig;
|
import com.qiwenshare.common.config.QiwenFileConfig;
|
||||||
|
import com.qiwenshare.file.component.FileDealComp;
|
||||||
import com.qiwenshare.file.config.es.FileSearch;
|
import com.qiwenshare.file.config.es.FileSearch;
|
||||||
import com.qiwenshare.file.domain.*;
|
import com.qiwenshare.file.domain.*;
|
||||||
import com.qiwenshare.file.dto.*;
|
import com.qiwenshare.file.dto.*;
|
||||||
@ -59,6 +60,8 @@ public class FileController {
|
|||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ElasticsearchRestTemplate elasticsearchRestTemplate;
|
private ElasticsearchRestTemplate elasticsearchRestTemplate;
|
||||||
|
@Resource
|
||||||
|
FileDealComp fileDealComp;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
QiwenFileConfig qiwenFileConfig;
|
QiwenFileConfig qiwenFileConfig;
|
||||||
@ -379,6 +382,7 @@ public class FileController {
|
|||||||
tempFileBean.setTimeStampName(FileUtil.getFileNameNotExtend(currentFile.getName()));
|
tempFileBean.setTimeStampName(FileUtil.getFileNameNotExtend(currentFile.getName()));
|
||||||
tempFileBean.setFileUrl(File.separator + (currentFile.getPath()).replace(PathUtil.getStaticPath(), ""));
|
tempFileBean.setFileUrl(File.separator + (currentFile.getPath()).replace(PathUtil.getStaticPath(), ""));
|
||||||
tempFileBean.setPointCount(1);
|
tempFileBean.setPointCount(1);
|
||||||
|
tempFileBean.setStorageType(0);
|
||||||
fileService.save(tempFileBean);
|
fileService.save(tempFileBean);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -540,7 +544,7 @@ public class FileController {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
resultTreeNode = insertTreeNode(resultTreeNode, id++, "/" , queue);
|
resultTreeNode = fileDealComp.insertTreeNode(resultTreeNode, id++, "/" , queue);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -558,65 +562,6 @@ public class FileController {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public TreeNode insertTreeNode(TreeNode treeNode, long id, String filePath, Queue<String> nodeNameQueue){
|
|
||||||
|
|
||||||
List<TreeNode> childrenTreeNodes = treeNode.getChildren();
|
|
||||||
String currentNodeName = nodeNameQueue.peek();
|
|
||||||
if (currentNodeName == null){
|
|
||||||
return treeNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
filePath = filePath + currentNodeName + "/";
|
|
||||||
|
|
||||||
if (!isExistPath(childrenTreeNodes, currentNodeName)){ //1、判断有没有该子节点,如果没有则插入
|
|
||||||
//插入
|
|
||||||
TreeNode resultTreeNode = new TreeNode();
|
|
||||||
|
|
||||||
resultTreeNode.setFilePath(filePath);
|
|
||||||
resultTreeNode.setLabel(nodeNameQueue.poll());
|
|
||||||
resultTreeNode.setId(++id);
|
|
||||||
|
|
||||||
childrenTreeNodes.add(resultTreeNode);
|
|
||||||
|
|
||||||
}else{ //2、如果有,则跳过
|
|
||||||
nodeNameQueue.poll();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (nodeNameQueue.size() != 0) {
|
|
||||||
for (int i = 0; i < childrenTreeNodes.size(); i++) {
|
|
||||||
|
|
||||||
TreeNode childrenTreeNode = childrenTreeNodes.get(i);
|
|
||||||
if (currentNodeName.equals(childrenTreeNode.getLabel())){
|
|
||||||
childrenTreeNode = insertTreeNode(childrenTreeNode, id * 10, filePath, nodeNameQueue);
|
|
||||||
childrenTreeNodes.remove(i);
|
|
||||||
childrenTreeNodes.add(childrenTreeNode);
|
|
||||||
treeNode.setChildren(childrenTreeNodes);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
treeNode.setChildren(childrenTreeNodes);
|
|
||||||
}
|
|
||||||
|
|
||||||
return treeNode;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isExistPath(List<TreeNode> childrenTreeNodes, String path){
|
|
||||||
boolean isExistPath = false;
|
|
||||||
|
|
||||||
try {
|
|
||||||
for (int i = 0; i < childrenTreeNodes.size(); i++){
|
|
||||||
if (path.equals(childrenTreeNodes.get(i).getLabel())){
|
|
||||||
isExistPath = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}catch (Exception e){
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return isExistPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -105,12 +105,6 @@ public class FiletransferController {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 上传文件
|
|
||||||
*
|
|
||||||
* @param request
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
@Operation(summary = "上传文件", description = "真正的上传文件接口", tags = {"filetransfer"})
|
@Operation(summary = "上传文件", description = "真正的上传文件接口", tags = {"filetransfer"})
|
||||||
@RequestMapping(value = "/uploadfile", method = RequestMethod.POST)
|
@RequestMapping(value = "/uploadfile", method = RequestMethod.POST)
|
||||||
@MyLog(operation = "上传文件", module = CURRENT_MODULE)
|
@MyLog(operation = "上传文件", module = CURRENT_MODULE)
|
||||||
@ -132,11 +126,7 @@ public class FiletransferController {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 下载文件
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
@Operation(summary = "下载文件", description = "下载文件接口", tags = {"filetransfer"})
|
@Operation(summary = "下载文件", description = "下载文件接口", tags = {"filetransfer"})
|
||||||
@MyLog(operation = "下载文件", module = CURRENT_MODULE)
|
@MyLog(operation = "下载文件", module = CURRENT_MODULE)
|
||||||
@RequestMapping(value = "/downloadfile", method = RequestMethod.GET)
|
@RequestMapping(value = "/downloadfile", method = RequestMethod.GET)
|
||||||
@ -167,7 +157,6 @@ public class FiletransferController {
|
|||||||
UserFile userFile = userFileService.getById(previewDTO.getUserFileId());
|
UserFile userFile = userFileService.getById(previewDTO.getUserFileId());
|
||||||
FileBean fileBean = fileService.getById(userFile.getFileId());
|
FileBean fileBean = fileService.getById(userFile.getFileId());
|
||||||
String mime= MimeUtils.getMime(userFile.getExtendName());
|
String mime= MimeUtils.getMime(userFile.getExtendName());
|
||||||
// httpServletResponse.setContentType(mime);
|
|
||||||
httpServletResponse.setHeader("Content-Type", mime);
|
httpServletResponse.setHeader("Content-Type", mime);
|
||||||
String rangeString = httpServletRequest.getHeader("Range");//如果是video标签发起的请求就不会为null
|
String rangeString = httpServletRequest.getHeader("Range");//如果是video标签发起的请求就不会为null
|
||||||
if (StringUtils.isNotEmpty(rangeString)) {
|
if (StringUtils.isNotEmpty(rangeString)) {
|
||||||
|
@ -24,11 +24,7 @@ import javax.annotation.Resource;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
|
||||||
* 用户控制类
|
|
||||||
*
|
|
||||||
* @author ma116
|
|
||||||
*/
|
|
||||||
@Tag(name = "user", description = "该接口为用户接口,主要做用户登录,注册和校验token")
|
@Tag(name = "user", description = "该接口为用户接口,主要做用户登录,注册和校验token")
|
||||||
@RestController
|
@RestController
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@ -44,11 +40,7 @@ public class UserController {
|
|||||||
|
|
||||||
public static Map<String, String> verificationCodeMap = new HashMap<>();
|
public static Map<String, String> verificationCodeMap = new HashMap<>();
|
||||||
|
|
||||||
public static final int TEXT = 4;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 当前模块
|
|
||||||
*/
|
|
||||||
public static final String CURRENT_MODULE = "用户管理";
|
public static final String CURRENT_MODULE = "用户管理";
|
||||||
|
|
||||||
|
|
||||||
@ -93,7 +85,7 @@ public class UserController {
|
|||||||
|
|
||||||
UserLoginVo userLoginVo = new UserLoginVo();
|
UserLoginVo userLoginVo = new UserLoginVo();
|
||||||
BeanUtil.copyProperties(saveUserBean, userLoginVo);
|
BeanUtil.copyProperties(saveUserBean, userLoginVo);
|
||||||
userLoginVo.setToken(jwt);
|
userLoginVo.setToken("Bearer " + jwt);
|
||||||
restResult.setData(userLoginVo);
|
restResult.setData(userLoginVo);
|
||||||
restResult.setSuccess(true);
|
restResult.setSuccess(true);
|
||||||
} else {
|
} else {
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
package com.qiwenshare.file.interceptor;
|
package com.qiwenshare.file.interceptor;
|
||||||
|
|
||||||
|
import com.qiwenshare.common.exception.NotLoginException;
|
||||||
import com.qiwenshare.file.api.IUserService;
|
import com.qiwenshare.file.api.IUserService;
|
||||||
import com.qiwenshare.file.domain.UserBean;
|
import com.qiwenshare.file.domain.UserBean;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.servlet.HandlerInterceptor;
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
import org.springframework.web.servlet.ModelAndView;
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
|
||||||
@ -15,25 +18,28 @@ import javax.servlet.http.HttpServletResponse;
|
|||||||
* token验证拦截
|
* token验证拦截
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@Component
|
||||||
public class AuthenticationInterceptor implements HandlerInterceptor {
|
public class AuthenticationInterceptor implements HandlerInterceptor {
|
||||||
@Autowired
|
@Autowired
|
||||||
private IUserService userService;
|
private IUserService userService;
|
||||||
@Override
|
@Override
|
||||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
log.info("---------------------------------------------------------");
|
|
||||||
// 取得token
|
// 取得token
|
||||||
String tokenHeader = request.getHeader(HttpHeaders.AUTHORIZATION);
|
String token = request.getHeader("token");//HttpHeaders.AUTHORIZATION
|
||||||
if (tokenHeader == null || "".equals(tokenHeader)) {
|
if("undefined".equals(token) || StringUtils.isEmpty(token)){
|
||||||
throw new Exception("token不存在");
|
token=request.getParameter("token不存在");
|
||||||
}
|
}
|
||||||
if (!tokenHeader.startsWith("Bearer")) {
|
if ("undefined".equals(token) || StringUtils.isEmpty(token)) {
|
||||||
throw new Exception("token格式错误");
|
throw new NotLoginException("token不存在");
|
||||||
}
|
}
|
||||||
String token = tokenHeader.replace("Bearer", "");
|
if (!token.startsWith("Bearer ")) {
|
||||||
|
throw new NotLoginException("token格式错误");
|
||||||
|
}
|
||||||
|
token = token.replace("Bearer ", "");
|
||||||
UserBean userBean = userService.getUserBeanByToken(token);
|
UserBean userBean = userService.getUserBeanByToken(token);
|
||||||
if (userBean == null) {
|
if (userBean == null) {
|
||||||
return false;
|
throw new NotLoginException();
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -4,6 +4,7 @@ import cn.hutool.core.util.RandomUtil;
|
|||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.qiwenshare.common.constant.FileConstant;
|
||||||
import com.qiwenshare.common.util.DateUtil;
|
import com.qiwenshare.common.util.DateUtil;
|
||||||
import com.qiwenshare.file.api.IUserFileService;
|
import com.qiwenshare.file.api.IUserFileService;
|
||||||
import com.qiwenshare.common.config.QiwenFileConfig;
|
import com.qiwenshare.common.config.QiwenFileConfig;
|
||||||
@ -183,7 +184,7 @@ public class UserFileService extends ServiceImpl<UserFileMapper, UserFile> impl
|
|||||||
String uuid = UUID.randomUUID().toString();
|
String uuid = UUID.randomUUID().toString();
|
||||||
if (userFile.getIsDir() == 1) {
|
if (userFile.getIsDir() == 1) {
|
||||||
LambdaUpdateWrapper<UserFile> userFileLambdaUpdateWrapper = new LambdaUpdateWrapper<UserFile>();
|
LambdaUpdateWrapper<UserFile> userFileLambdaUpdateWrapper = new LambdaUpdateWrapper<UserFile>();
|
||||||
userFileLambdaUpdateWrapper.set(UserFile::getDeleteFlag, RandomUtil.randomInt(10))
|
userFileLambdaUpdateWrapper.set(UserFile::getDeleteFlag, RandomUtil.randomInt(FileConstant.deleteFileRandomSize))
|
||||||
.set(UserFile::getDeleteBatchNum, uuid)
|
.set(UserFile::getDeleteBatchNum, uuid)
|
||||||
.set(UserFile::getDeleteTime, DateUtil.getCurrentTime())
|
.set(UserFile::getDeleteTime, DateUtil.getCurrentTime())
|
||||||
.eq(UserFile::getUserFileId, userFileId);
|
.eq(UserFile::getUserFileId, userFileId);
|
||||||
@ -193,20 +194,15 @@ public class UserFileService extends ServiceImpl<UserFileMapper, UserFile> impl
|
|||||||
updateFileDeleteStateByFilePath(filePath, userFile.getDeleteBatchNum(), sessionUserId);
|
updateFileDeleteStateByFilePath(filePath, userFile.getDeleteBatchNum(), sessionUserId);
|
||||||
|
|
||||||
}else{
|
}else{
|
||||||
|
|
||||||
UserFile userFileTemp = userFileMapper.selectById(userFileId);
|
UserFile userFileTemp = userFileMapper.selectById(userFileId);
|
||||||
FileBean fileBean = fileMapper.selectById(userFileTemp.getFileId());
|
|
||||||
|
|
||||||
LambdaUpdateWrapper<UserFile> userFileLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
|
LambdaUpdateWrapper<UserFile> userFileLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
|
||||||
userFileLambdaUpdateWrapper.set(UserFile::getDeleteFlag, RandomUtil.randomInt(10))
|
userFileLambdaUpdateWrapper.set(UserFile::getDeleteFlag, RandomUtil.randomInt(FileConstant.deleteFileRandomSize))
|
||||||
.set(UserFile::getDeleteTime, DateUtil.getCurrentTime())
|
.set(UserFile::getDeleteTime, DateUtil.getCurrentTime())
|
||||||
.set(UserFile::getDeleteBatchNum, uuid)
|
.set(UserFile::getDeleteBatchNum, uuid)
|
||||||
.eq(UserFile::getUserFileId, userFileTemp.getUserFileId());
|
.eq(UserFile::getUserFileId, userFileTemp.getUserFileId());
|
||||||
userFileMapper.update(null, userFileLambdaUpdateWrapper);
|
userFileMapper.update(null, userFileLambdaUpdateWrapper);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
RecoveryFile recoveryFile = new RecoveryFile();
|
RecoveryFile recoveryFile = new RecoveryFile();
|
||||||
recoveryFile.setUserFileId(userFileId);
|
recoveryFile.setUserFileId(userFileId);
|
||||||
recoveryFile.setDeleteTime(DateUtil.getCurrentTime());
|
recoveryFile.setDeleteTime(DateUtil.getCurrentTime());
|
||||||
@ -226,7 +222,7 @@ public class UserFileService extends ServiceImpl<UserFileMapper, UserFile> impl
|
|||||||
public void run() {
|
public void run() {
|
||||||
//标记删除标志
|
//标记删除标志
|
||||||
LambdaUpdateWrapper<UserFile> userFileLambdaUpdateWrapper1 = new LambdaUpdateWrapper<>();
|
LambdaUpdateWrapper<UserFile> userFileLambdaUpdateWrapper1 = new LambdaUpdateWrapper<>();
|
||||||
userFileLambdaUpdateWrapper1.set(UserFile::getDeleteFlag, RandomUtil.randomInt(10))
|
userFileLambdaUpdateWrapper1.set(UserFile::getDeleteFlag, RandomUtil.randomInt(FileConstant.deleteFileRandomSize))
|
||||||
.set(UserFile::getDeleteTime, DateUtil.getCurrentTime())
|
.set(UserFile::getDeleteTime, DateUtil.getCurrentTime())
|
||||||
.set(UserFile::getDeleteBatchNum, deleteBatchNum)
|
.set(UserFile::getDeleteBatchNum, deleteBatchNum)
|
||||||
.eq(UserFile::getUserFileId, userFileTemp.getUserFileId())
|
.eq(UserFile::getUserFileId, userFileTemp.getUserFileId())
|
||||||
|
@ -8,6 +8,7 @@ import com.qiwenshare.common.result.RestResult;
|
|||||||
import com.qiwenshare.common.util.JjwtUtil;
|
import com.qiwenshare.common.util.JjwtUtil;
|
||||||
import com.qiwenshare.common.util.PasswordUtil;
|
import com.qiwenshare.common.util.PasswordUtil;
|
||||||
import com.qiwenshare.file.api.IUserService;
|
import com.qiwenshare.file.api.IUserService;
|
||||||
|
import com.qiwenshare.file.component.UserDealComp;
|
||||||
import com.qiwenshare.file.controller.UserController;
|
import com.qiwenshare.file.controller.UserController;
|
||||||
import com.qiwenshare.file.domain.UserBean;
|
import com.qiwenshare.file.domain.UserBean;
|
||||||
import com.qiwenshare.file.mapper.UserMapper;
|
import com.qiwenshare.file.mapper.UserMapper;
|
||||||
@ -29,15 +30,20 @@ public class UserService extends ServiceImpl<UserMapper, UserBean> implements IU
|
|||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
UserMapper userMapper;
|
UserMapper userMapper;
|
||||||
|
@Resource
|
||||||
|
UserDealComp userDealComp;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserBean getUserBeanByToken(String token){
|
public UserBean getUserBeanByToken(String token){
|
||||||
Claims c = null;
|
Claims c = null;
|
||||||
|
if (StringUtils.isEmpty(token)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
token = token.replace("Bearer ", "");
|
||||||
try {
|
try {
|
||||||
log.debug("token:" + token);
|
|
||||||
c = JjwtUtil.parseJWT(token);
|
c = JjwtUtil.parseJWT(token);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("解码异常");
|
log.error("解码异常:" + e);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (c == null) {
|
if (c == null) {
|
||||||
@ -111,13 +117,13 @@ public class UserService extends ServiceImpl<UserMapper, UserBean> implements IU
|
|||||||
if (userBean.getUsername() == null || "".equals(userBean.getUsername())){
|
if (userBean.getUsername() == null || "".equals(userBean.getUsername())){
|
||||||
return RestResult.fail().message("用户名不能为空!");
|
return RestResult.fail().message("用户名不能为空!");
|
||||||
}
|
}
|
||||||
if (isUserNameExit(userBean)) {
|
if (userDealComp.isUserNameExit(userBean)) {
|
||||||
return RestResult.fail().message("用户名已存在!");
|
return RestResult.fail().message("用户名已存在!");
|
||||||
}
|
}
|
||||||
if (!isPhoneFormatRight(userBean.getTelephone())){
|
if (!userDealComp.isPhoneFormatRight(userBean.getTelephone())){
|
||||||
return RestResult.fail().message("手机号格式不正确!");
|
return RestResult.fail().message("手机号格式不正确!");
|
||||||
}
|
}
|
||||||
if (isPhoneExit(userBean)) {
|
if (userDealComp.isPhoneExit(userBean)) {
|
||||||
return RestResult.fail().message("手机号已存在!");
|
return RestResult.fail().message("手机号已存在!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,9 +137,6 @@ public class UserService extends ServiceImpl<UserMapper, UserBean> implements IU
|
|||||||
userBean.setRegisterTime(DateUtil.getCurrentTime());
|
userBean.setRegisterTime(DateUtil.getCurrentTime());
|
||||||
int result = userMapper.insertUser(userBean);
|
int result = userMapper.insertUser(userBean);
|
||||||
userMapper.insertUserRole(userBean.getUserId(), 2);
|
userMapper.insertUserRole(userBean.getUserId(), 2);
|
||||||
// UserImageBean userImageBean = new UserImageBean();
|
|
||||||
// userImageBean.setImageUrl("");
|
|
||||||
// userImageBean.setUserId(userBean.getUserId());
|
|
||||||
if (result == 1) {
|
if (result == 1) {
|
||||||
return RestResult.success();
|
return RestResult.success();
|
||||||
} else {
|
} else {
|
||||||
@ -143,46 +146,6 @@ public class UserService extends ServiceImpl<UserMapper, UserBean> implements IU
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 检测用户名是否存在
|
|
||||||
*
|
|
||||||
* @param userBean
|
|
||||||
*/
|
|
||||||
private Boolean isUserNameExit(UserBean userBean) {
|
|
||||||
LambdaQueryWrapper<UserBean> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
|
||||||
lambdaQueryWrapper.eq(UserBean::getUsername, userBean.getUsername());
|
|
||||||
List<UserBean> list = userMapper.selectList(lambdaQueryWrapper);
|
|
||||||
if (list != null && !list.isEmpty()) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 检测手机号是否存在
|
|
||||||
*
|
|
||||||
* @param userBean
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private Boolean isPhoneExit(UserBean userBean) {
|
|
||||||
|
|
||||||
LambdaQueryWrapper<UserBean> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
|
||||||
lambdaQueryWrapper.eq(UserBean::getTelephone, userBean.getTelephone());
|
|
||||||
List<UserBean> list = userMapper.selectList(lambdaQueryWrapper);
|
|
||||||
if (list != null && !list.isEmpty()) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private Boolean isPhoneFormatRight(String phone){
|
|
||||||
String regex = "^1\\d{10}";
|
|
||||||
boolean isRight = Pattern.matches(regex, phone);
|
|
||||||
return isRight;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -33,41 +33,4 @@
|
|||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<!-- <update id="updateFile" parameterType="java">-->
|
|
||||||
<!-- <choose>-->
|
|
||||||
<!-- <when test="isDir == 1">-->
|
|
||||||
<!-- UPDATE file SET filename=#{fileName}, uploadTime = #{uploadTime}-->
|
|
||||||
<!-- where fileId = #{fileId};-->
|
|
||||||
<!-- UPDATE file SET filepath=REPLACE(filepath, #{oldFilePath}, #{filePath}) WHERE filepath LIKE N'${oldFilePath}%';-->
|
|
||||||
<!-- </when>-->
|
|
||||||
<!-- <otherwise>-->
|
|
||||||
<!-- update file-->
|
|
||||||
<!-- <set>-->
|
|
||||||
<!-- <if test="fileName != null">-->
|
|
||||||
<!-- fileName = #{fileName},-->
|
|
||||||
<!-- </if>-->
|
|
||||||
<!-- <if test="uploadTime != null">-->
|
|
||||||
<!-- uploadTime = #{uploadTime},-->
|
|
||||||
<!-- </if>-->
|
|
||||||
<!-- <if test="fileUrl != null">-->
|
|
||||||
<!-- fileUrl = #{fileUrl},-->
|
|
||||||
<!-- </if>-->
|
|
||||||
<!-- </set>-->
|
|
||||||
<!-- where fileId = #{fileId}-->
|
|
||||||
<!-- </otherwise>-->
|
|
||||||
<!-- </choose>-->
|
|
||||||
|
|
||||||
<!-- </update>-->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
Loading…
Reference in New Issue
Block a user