mirror of
https://gitee.com/lab1024/smart-admin.git
synced 2025-09-18 19:36:39 +08:00
1.解决不需要security的项目是通过拦截器判断是否存在用户,此时common会自动引入security包
2.解决本地文件上传,图片找不到问题 3.添加UrlMatcherUtil,用于判断是否包含特定的URL
This commit is contained in:
parent
945e31290e
commit
6149ef9f0c
@ -0,0 +1,56 @@
|
|||||||
|
package net.lab1024.sa.common.common.util;
|
||||||
|
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
import org.springframework.util.AntPathMatcher;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 罗伊
|
||||||
|
* @description:
|
||||||
|
* @date 2022/11/25 9:32 上午
|
||||||
|
*/
|
||||||
|
public class SmartUrlMatcherUtil {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当前请求是与urlPatterns中的匹配
|
||||||
|
* @param urlPatterns
|
||||||
|
* @param request
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static Boolean contain(List<String> urlPatterns, HttpServletRequest request) {
|
||||||
|
if (CollectionUtils.isEmpty(urlPatterns)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
String uri = request.getRequestURI();
|
||||||
|
for (String urlPattern : urlPatterns) {
|
||||||
|
AntPathMatcher antPathMatcher = new AntPathMatcher();
|
||||||
|
boolean match = antPathMatcher.match(urlPattern, uri);
|
||||||
|
if (match) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
List<String> ignoreUrlList = Lists.newArrayList();
|
||||||
|
ignoreUrlList.add("/swagger-ui.html");
|
||||||
|
ignoreUrlList.add("/swagger-resources/**");
|
||||||
|
ignoreUrlList.add("/*/api-docs");
|
||||||
|
|
||||||
|
String uri = "/v2/api-docs?group=Admin";
|
||||||
|
for (String urlPattern : ignoreUrlList) {
|
||||||
|
AntPathMatcher antPathMatcher = new AntPathMatcher();
|
||||||
|
boolean match = antPathMatcher.match(urlPattern, uri);
|
||||||
|
if (match) {
|
||||||
|
System.out.println(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,8 @@
|
|||||||
package net.lab1024.sa.common.config;
|
package net.lab1024.sa.common.config;
|
||||||
|
|
||||||
import net.lab1024.sa.common.common.interceptor.AbstractInterceptor;
|
|
||||||
import org.apache.commons.collections4.CollectionUtils;
|
import org.apache.commons.collections4.CollectionUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.web.servlet.HandlerInterceptor;
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
@ -27,6 +27,9 @@ public class MvcConfig implements WebMvcConfigurer {
|
|||||||
@Autowired(required = false)
|
@Autowired(required = false)
|
||||||
private List<HandlerInterceptor> interceptorList;
|
private List<HandlerInterceptor> interceptorList;
|
||||||
|
|
||||||
|
@Value("${file.storage.local.path}")
|
||||||
|
private String localPath;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addInterceptors (InterceptorRegistry registry) {
|
public void addInterceptors (InterceptorRegistry registry) {
|
||||||
if (CollectionUtils.isEmpty(interceptorList)) {
|
if (CollectionUtils.isEmpty(interceptorList)) {
|
||||||
@ -39,7 +42,8 @@ public class MvcConfig implements WebMvcConfigurer {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||||
registry.addResourceHandler("/preview/**");
|
registry.addResourceHandler("/preview/**")
|
||||||
|
.addResourceLocations("file:" + localPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -9,7 +9,6 @@ import net.lab1024.sa.common.common.exception.BusinessException;
|
|||||||
import org.springframework.beans.TypeMismatchException;
|
import org.springframework.beans.TypeMismatchException;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||||
import org.springframework.security.access.AccessDeniedException;
|
|
||||||
import org.springframework.validation.BindException;
|
import org.springframework.validation.BindException;
|
||||||
import org.springframework.validation.FieldError;
|
import org.springframework.validation.FieldError;
|
||||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
@ -77,15 +76,6 @@ public class GlobalExceptionHandler {
|
|||||||
return ResponseDTO.error(UserErrorCode.PARAM_ERROR);
|
return ResponseDTO.error(UserErrorCode.PARAM_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 权限异常
|
|
||||||
*/
|
|
||||||
@ResponseBody
|
|
||||||
@ExceptionHandler({AccessDeniedException.class})
|
|
||||||
public ResponseDTO<?> permissionExceptionHandler(AccessDeniedException e) {
|
|
||||||
return ResponseDTO.error(UserErrorCode.NO_PERMISSION);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 业务异常
|
* 业务异常
|
||||||
*/
|
*/
|
||||||
|
@ -0,0 +1,32 @@
|
|||||||
|
package net.lab1024.sa.common.handler;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import net.lab1024.sa.common.common.code.UserErrorCode;
|
||||||
|
import net.lab1024.sa.common.common.domain.ResponseDTO;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
|
import org.springframework.security.access.AccessDeniedException;
|
||||||
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [ 全局异常拦截 ]
|
||||||
|
*
|
||||||
|
* @author 罗伊
|
||||||
|
* @date 2020/8/25 11:57
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@ControllerAdvice
|
||||||
|
@ConditionalOnClass(AccessDeniedException.class)
|
||||||
|
public class SecurityExceptionHandler {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 权限异常
|
||||||
|
*/
|
||||||
|
@ResponseBody
|
||||||
|
@ExceptionHandler({AccessDeniedException.class})
|
||||||
|
public ResponseDTO<?> permissionExceptionHandler(AccessDeniedException e) {
|
||||||
|
return ResponseDTO.error(UserErrorCode.NO_PERMISSION);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user