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