提交 | 用户 | 时间
|
fd2207
|
1 |
package com.jcdm.framework.config.properties; |
懒 |
2 |
|
|
3 |
import java.util.ArrayList; |
|
4 |
import java.util.List; |
|
5 |
import java.util.Map; |
|
6 |
import java.util.Objects; |
|
7 |
import java.util.Optional; |
|
8 |
import java.util.regex.Pattern; |
|
9 |
import org.apache.commons.lang3.RegExUtils; |
|
10 |
import org.springframework.beans.BeansException; |
|
11 |
import org.springframework.beans.factory.InitializingBean; |
|
12 |
import org.springframework.context.ApplicationContext; |
|
13 |
import org.springframework.context.ApplicationContextAware; |
|
14 |
import org.springframework.context.annotation.Configuration; |
|
15 |
import org.springframework.core.annotation.AnnotationUtils; |
|
16 |
import org.springframework.web.method.HandlerMethod; |
|
17 |
import org.springframework.web.servlet.mvc.method.RequestMappingInfo; |
|
18 |
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; |
|
19 |
import com.jcdm.common.annotation.Anonymous; |
|
20 |
|
|
21 |
/** |
|
22 |
* 设置Anonymous注解允许匿名访问的url |
|
23 |
* |
|
24 |
* @author jc |
|
25 |
*/ |
|
26 |
@Configuration |
|
27 |
public class PermitAllUrlProperties implements InitializingBean, ApplicationContextAware |
|
28 |
{ |
|
29 |
private static final Pattern PATTERN = Pattern.compile("\\{(.*?)\\}"); |
|
30 |
|
|
31 |
private ApplicationContext applicationContext; |
|
32 |
|
|
33 |
private List<String> urls = new ArrayList<>(); |
|
34 |
|
|
35 |
public String ASTERISK = "*"; |
|
36 |
|
|
37 |
@Override |
|
38 |
public void afterPropertiesSet() |
|
39 |
{ |
|
40 |
RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class); |
|
41 |
Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods(); |
|
42 |
|
|
43 |
map.keySet().forEach(info -> { |
|
44 |
HandlerMethod handlerMethod = map.get(info); |
|
45 |
|
|
46 |
// 获取方法上边的注解 替代path variable 为 * |
|
47 |
Anonymous method = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), Anonymous.class); |
|
48 |
Optional.ofNullable(method).ifPresent(anonymous -> Objects.requireNonNull(info.getPatternsCondition().getPatterns()) |
|
49 |
.forEach(url -> urls.add(RegExUtils.replaceAll(url, PATTERN, ASTERISK)))); |
|
50 |
|
|
51 |
// 获取类上边的注解, 替代path variable 为 * |
|
52 |
Anonymous controller = AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), Anonymous.class); |
|
53 |
Optional.ofNullable(controller).ifPresent(anonymous -> Objects.requireNonNull(info.getPatternsCondition().getPatterns()) |
|
54 |
.forEach(url -> urls.add(RegExUtils.replaceAll(url, PATTERN, ASTERISK)))); |
|
55 |
}); |
|
56 |
} |
|
57 |
|
|
58 |
@Override |
|
59 |
public void setApplicationContext(ApplicationContext context) throws BeansException |
|
60 |
{ |
|
61 |
this.applicationContext = context; |
|
62 |
} |
|
63 |
|
|
64 |
public List<String> getUrls() |
|
65 |
{ |
|
66 |
return urls; |
|
67 |
} |
|
68 |
|
|
69 |
public void setUrls(List<String> urls) |
|
70 |
{ |
|
71 |
this.urls = urls; |
|
72 |
} |
|
73 |
} |