yangys
2024-04-04 ed4a5236bab800094be4a8378f5098eebe3de6ac
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.qianwen.core.secure.config;
 
import java.util.List;
import java.util.stream.Collectors;
import com.qianwen.core.secure.aspect.AuthAspect;
import com.qianwen.core.secure.handler.ISecureHandler;
import com.qianwen.core.secure.props.AuthSecure;
import com.qianwen.core.secure.props.BasicSecure;
import com.qianwen.core.secure.props.BladeSecureProperties;
import com.qianwen.core.secure.props.SignSecure;
import com.qianwen.core.secure.provider.ClientDetailsServiceImpl;
import com.qianwen.core.secure.provider.IClientDetailsService;
import com.qianwen.core.secure.registry.SecureRegistry;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.lang.NonNull;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
@EnableConfigurationProperties({BladeSecureProperties.class})
@Configuration(proxyBeanMethods = false)
@Order
 
public class SecureConfiguration implements WebMvcConfigurer {
    private final SecureRegistry secureRegistry;
    private final BladeSecureProperties secureProperties;
    private final JdbcTemplate jdbcTemplate;
    private final ISecureHandler secureHandler;
 
    public SecureConfiguration(final SecureRegistry secureRegistry, final BladeSecureProperties secureProperties, final JdbcTemplate jdbcTemplate, final ISecureHandler secureHandler) {
        this.secureRegistry = secureRegistry;
        this.secureProperties = secureProperties;
        this.jdbcTemplate = jdbcTemplate;
        this.secureHandler = secureHandler;
    }
 
    public void addInterceptors(@NonNull InterceptorRegistry registry) {
        if (this.secureRegistry.isAuthEnabled() || this.secureProperties.getAuthEnabled().booleanValue()) {
            List<AuthSecure> authSecures = this.secureRegistry.addAuthPatterns(this.secureProperties.getAuth()).getAuthSecures();
            if (authSecures.size() > 0) {
                registry.addInterceptor(this.secureHandler.authInterceptor(authSecures));
                this.secureRegistry.excludePathPatterns(authSecures.stream().map((v0) -> {
                    return v0.getPattern();
                }).collect(Collectors.toList()));
            }
        }
        if (this.secureRegistry.isBasicEnabled() || this.secureProperties.getBasicEnabled().booleanValue()) {
            List<BasicSecure> basicSecures = this.secureRegistry.addBasicPatterns(this.secureProperties.getBasic()).getBasicSecures();
            if (basicSecures.size() > 0) {
                registry.addInterceptor(this.secureHandler.basicInterceptor(basicSecures));
                this.secureRegistry.excludePathPatterns(basicSecures.stream().map((v0) -> {
                    return v0.getPattern();
                }).collect(Collectors.toList()));
            }
        }
        if (this.secureRegistry.isSignEnabled() || this.secureProperties.getSignEnabled().booleanValue()) {
            List<SignSecure> signSecures = this.secureRegistry.addSignPatterns(this.secureProperties.getSign()).getSignSecures();
            if (signSecures.size() > 0) {
                registry.addInterceptor(this.secureHandler.signInterceptor(signSecures));
                this.secureRegistry.excludePathPatterns( signSecures.stream().map((v0) -> {
                    return v0.getPattern();
                }).collect(Collectors.toList()));
            }
        }
        if (this.secureRegistry.isClientEnabled() || this.secureProperties.getClientEnabled().booleanValue()) {
            this.secureProperties.getClient().forEach(clientSecure -> {
                registry.addInterceptor(this.secureHandler.clientInterceptor(clientSecure.getClientId())).addPathPatterns(clientSecure.getPathPatterns());
            });
        }
        if (this.secureRegistry.isEnabled() || this.secureProperties.getEnabled().booleanValue()) {
            registry.addInterceptor(this.secureHandler.tokenInterceptor()).excludePathPatterns(this.secureRegistry.getExcludePatterns()).excludePathPatterns(this.secureRegistry.getDefaultExcludePatterns()).excludePathPatterns(this.secureProperties.getSkipUrl());
        }
    }
 
    @Bean
    public AuthAspect authAspect() {
        return new AuthAspect();
    }
 
    @ConditionalOnMissingBean({IClientDetailsService.class})
    @Bean
    public IClientDetailsService clientDetailsService() {
        return new ClientDetailsServiceImpl(this.jdbcTemplate);
    }
}