1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package org.dromara.cryptapi.handler;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ReUtil;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.dromara.cryptapi.annotation.ApiDecrypt;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.condition.PathPatternsRequestCondition;
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 wdhcr
 */
@Data
@Component
@RequiredArgsConstructor
public class DecryptUrlHandler implements InitializingBean {
 
    private static final Pattern PATTERN = Pattern.compile("\\{(.*?)}");
 
    private List<String> urls = new ArrayList<>();
 
    private final RequestMappingHandlerMapping requestMappingHandlerMapping;
 
    @Override
    public void afterPropertiesSet() {
        Set<String> set = new HashSet<>();
        Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
        List<RequestMappingInfo> requestMappingInfos = map.entrySet().stream().filter(item -> {
            HandlerMethod method = item.getValue();
            ApiDecrypt decrypt = method.getMethodAnnotation(ApiDecrypt.class);
            // 标有解密注解的并且是post 或者put 请求的handler
            return decrypt != null && CollectionUtil.containsAny(item.getKey().getMethodsCondition().getMethods(), Arrays.asList(RequestMethod.PUT, RequestMethod.POST));
        }).map(Map.Entry::getKey).toList();
        requestMappingInfos.forEach(info -> {
            // 获取注解上边的 path 替代 path variable 为 *
            Optional.ofNullable(info.getPathPatternsCondition())
                .map(PathPatternsRequestCondition::getPatterns)
                .orElseGet(HashSet::new)
                .forEach(url -> set.add(ReUtil.replaceAll(url.getPatternString(), PATTERN, "*")));
        });
        urls.addAll(set);
    }
 
}