This commit is contained in:
zhuoda 2022-12-09 18:39:41 +08:00
commit cc590abc9e
4 changed files with 94 additions and 12 deletions

View File

@ -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);
}
}
}
}

View File

@ -1,8 +1,8 @@
package net.lab1024.sa.common.config;
import net.lab1024.sa.common.common.interceptor.AbstractInterceptor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
@ -27,6 +27,9 @@ public class MvcConfig implements WebMvcConfigurer {
@Autowired(required = false)
private List<HandlerInterceptor> interceptorList;
@Value("${file.storage.local.path}")
private String localPath;
@Override
public void addInterceptors (InterceptorRegistry registry) {
if (CollectionUtils.isEmpty(interceptorList)) {
@ -39,7 +42,8 @@ public class MvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/preview/**");
registry.addResourceHandler("/preview/**")
.addResourceLocations("file:" + localPath);
}
@Override

View File

@ -9,7 +9,6 @@ import net.lab1024.sa.common.common.exception.BusinessException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
@ -77,15 +76,6 @@ public class GlobalExceptionHandler {
return ResponseDTO.error(UserErrorCode.PARAM_ERROR);
}
/**
* 权限异常
*/
@ResponseBody
@ExceptionHandler({AccessDeniedException.class})
public ResponseDTO<?> permissionExceptionHandler(AccessDeniedException e) {
return ResponseDTO.error(UserErrorCode.NO_PERMISSION);
}
/**
* 业务异常
*/

View File

@ -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);
}
}