update 支持注解配置匿名访问
This commit is contained in:
parent
5ced7e05f5
commit
51593aafb2
@ -14,6 +14,5 @@ import java.lang.annotation.Target;
|
|||||||
@Target({ElementType.METHOD, ElementType.TYPE})
|
@Target({ElementType.METHOD, ElementType.TYPE})
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Documented
|
@Documented
|
||||||
public @interface Anonymous
|
public @interface Anonymous {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,8 @@ import cn.dev33.satoken.router.SaRouter;
|
|||||||
import cn.dev33.satoken.stp.StpLogic;
|
import cn.dev33.satoken.stp.StpLogic;
|
||||||
import cn.dev33.satoken.stp.StpUtil;
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
import com.ruoyi.common.helper.LoginHelper;
|
import com.ruoyi.common.helper.LoginHelper;
|
||||||
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
||||||
|
import com.ruoyi.framework.config.properties.ExcludeUrlProperties;
|
||||||
import com.ruoyi.framework.config.properties.SecurityProperties;
|
import com.ruoyi.framework.config.properties.SecurityProperties;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@ -37,12 +39,14 @@ public class SaTokenConfig implements WebMvcConfigurer {
|
|||||||
public void addInterceptors(InterceptorRegistry registry) {
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
// 注册路由拦截器,自定义验证规则
|
// 注册路由拦截器,自定义验证规则
|
||||||
registry.addInterceptor(new SaRouteInterceptor((request, response, handler) -> {
|
registry.addInterceptor(new SaRouteInterceptor((request, response, handler) -> {
|
||||||
|
ExcludeUrlProperties excludeUrlProperties = SpringUtils.getBean(ExcludeUrlProperties.class);
|
||||||
// 登录验证 -- 排除多个路径
|
// 登录验证 -- 排除多个路径
|
||||||
SaRouter
|
SaRouter
|
||||||
// 获取所有的
|
// 获取所有的
|
||||||
.match("/**")
|
.match("/**")
|
||||||
// 排除下不需要拦截的
|
// 排除下不需要拦截的
|
||||||
.notMatch(securityProperties.getExcludes())
|
.notMatch(securityProperties.getExcludes())
|
||||||
|
.notMatch(excludeUrlProperties.getExcludes())
|
||||||
// 对未排除的路径进行检查
|
// 对未排除的路径进行检查
|
||||||
.check(() -> {
|
.check(() -> {
|
||||||
// 检查是否登录 是否有token
|
// 检查是否登录 是否有token
|
||||||
|
@ -0,0 +1,61 @@
|
|||||||
|
package com.ruoyi.framework.config.properties;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.ReUtil;
|
||||||
|
import com.ruoyi.common.annotation.Anonymous;
|
||||||
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
||||||
|
import lombok.Getter;
|
||||||
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
import org.springframework.context.annotation.Lazy;
|
||||||
|
import org.springframework.core.annotation.AnnotationUtils;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.method.HandlerMethod;
|
||||||
|
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
|
||||||
|
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置注解允许匿名访问的url
|
||||||
|
*
|
||||||
|
* @author Lion Li
|
||||||
|
*/
|
||||||
|
@Lazy
|
||||||
|
@Component
|
||||||
|
public class ExcludeUrlProperties implements InitializingBean {
|
||||||
|
|
||||||
|
private static final Pattern PATTERN = Pattern.compile("\\{(.*?)\\}");
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private final List<String> excludes = new ArrayList<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterPropertiesSet() {
|
||||||
|
String asterisk = "*";
|
||||||
|
RequestMappingHandlerMapping mapping = SpringUtils.getBean(RequestMappingHandlerMapping.class);
|
||||||
|
Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
|
||||||
|
|
||||||
|
map.keySet().forEach(info -> {
|
||||||
|
HandlerMethod handlerMethod = map.get(info);
|
||||||
|
|
||||||
|
// 获取方法上边的注解 替代path variable 为 *
|
||||||
|
Anonymous method = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), Anonymous.class);
|
||||||
|
Optional.ofNullable(method).ifPresent(anonymous -> {
|
||||||
|
Set<String> patterns = info.getPatternsCondition().getPatterns();
|
||||||
|
patterns.forEach(url -> {
|
||||||
|
excludes.add(ReUtil.replaceAll(url, PATTERN, asterisk));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// 获取类上边的注解, 替代path variable 为 *
|
||||||
|
Anonymous controller = AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), Anonymous.class);
|
||||||
|
Optional.ofNullable(controller).ifPresent(anonymous -> {
|
||||||
|
Set<String> patterns = info.getPatternsCondition().getPatterns();
|
||||||
|
patterns.forEach(url -> {
|
||||||
|
excludes.add(ReUtil.replaceAll(url, PATTERN, asterisk));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,72 +0,0 @@
|
|||||||
package com.ruoyi.framework.config.properties;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
import org.apache.commons.lang3.RegExUtils;
|
|
||||||
import org.springframework.beans.BeansException;
|
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.context.ApplicationContextAware;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.core.annotation.AnnotationUtils;
|
|
||||||
import org.springframework.web.method.HandlerMethod;
|
|
||||||
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
|
|
||||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
|
||||||
import com.ruoyi.common.annotation.Anonymous;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置Anonymous注解允许匿名访问的url
|
|
||||||
*
|
|
||||||
* @author ruoyi
|
|
||||||
*/
|
|
||||||
@Configuration
|
|
||||||
public class PermitAllUrlProperties implements InitializingBean, ApplicationContextAware
|
|
||||||
{
|
|
||||||
private static final Pattern PATTERN = Pattern.compile("\\{(.*?)\\}");
|
|
||||||
|
|
||||||
private ApplicationContext applicationContext;
|
|
||||||
|
|
||||||
private List<String> urls = new ArrayList<>();
|
|
||||||
|
|
||||||
public String ASTERISK = "*";
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void afterPropertiesSet()
|
|
||||||
{
|
|
||||||
RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
|
|
||||||
Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
|
|
||||||
|
|
||||||
map.keySet().forEach(info -> {
|
|
||||||
HandlerMethod handlerMethod = map.get(info);
|
|
||||||
|
|
||||||
// 获取方法上边的注解 替代path variable 为 *
|
|
||||||
Anonymous method = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), Anonymous.class);
|
|
||||||
Optional.ofNullable(method).ifPresent(anonymous -> info.getPatternsCondition().getPatterns()
|
|
||||||
.forEach(url -> urls.add(RegExUtils.replaceAll(url, PATTERN, ASTERISK))));
|
|
||||||
|
|
||||||
// 获取类上边的注解, 替代path variable 为 *
|
|
||||||
Anonymous controller = AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), Anonymous.class);
|
|
||||||
Optional.ofNullable(controller).ifPresent(anonymous -> info.getPatternsCondition().getPatterns()
|
|
||||||
.forEach(url -> urls.add(RegExUtils.replaceAll(url, PATTERN, ASTERISK))));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setApplicationContext(ApplicationContext context) throws BeansException
|
|
||||||
{
|
|
||||||
this.applicationContext = context;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<String> getUrls()
|
|
||||||
{
|
|
||||||
return urls;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUrls(List<String> urls)
|
|
||||||
{
|
|
||||||
this.urls = urls;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user