|
@@ -1,40 +1,28 @@
|
|
|
package com.chelvc.framework.security.config;
|
|
|
|
|
|
-import java.lang.reflect.Method;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Collection;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.List;
|
|
|
import java.util.Set;
|
|
|
import javax.crypto.SecretKey;
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
|
|
|
-import com.chelvc.framework.base.config.MultiserverMvcConfigurer;
|
|
|
import com.chelvc.framework.base.context.ApplicationContextHolder;
|
|
|
import com.chelvc.framework.base.context.LoggingContextHolder;
|
|
|
import com.chelvc.framework.base.context.Result;
|
|
|
import com.chelvc.framework.base.context.SessionContextHolder;
|
|
|
-import com.chelvc.framework.base.util.HttpUtils;
|
|
|
-import com.chelvc.framework.base.util.SpringUtils;
|
|
|
import com.chelvc.framework.common.util.AESUtils;
|
|
|
import com.chelvc.framework.common.util.ObjectUtils;
|
|
|
import com.chelvc.framework.common.util.StringUtils;
|
|
|
-import com.chelvc.framework.security.annotation.Authorize;
|
|
|
import com.chelvc.framework.security.context.SecurityContextHolder;
|
|
|
import com.chelvc.framework.security.session.DefaultSessionValidator;
|
|
|
import com.chelvc.framework.security.session.SessionValidator;
|
|
|
import com.chelvc.framework.security.session.TokenExpiredValidator;
|
|
|
-import com.google.common.collect.Sets;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.aop.framework.AopProxyUtils;
|
|
|
-import org.springframework.beans.factory.ObjectProvider;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
-import org.springframework.core.env.Environment;
|
|
|
-import org.springframework.core.io.Resource;
|
|
|
import org.springframework.http.HttpMethod;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
@@ -143,20 +131,8 @@ public class OAuthConfigurer extends WebSecurityConfigurerAdapter {
|
|
|
|
|
|
@Override
|
|
|
public void configure(WebSecurity web) {
|
|
|
- // 排除配置指定、ErrorController、服务心跳检测接口地址
|
|
|
- Set<String> ignores = Sets.newHashSet(this.properties.getIgnores());
|
|
|
- Environment environment = this.applicationContext.getEnvironment();
|
|
|
- ignores.add(environment.resolveRequiredPlaceholders("${server.error.path:${error.path:/error}}"));
|
|
|
- try {
|
|
|
- Class<?> clazz =
|
|
|
- Class.forName("org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties");
|
|
|
- ObjectProvider<?> actuator = this.applicationContext.getBeanProvider(clazz);
|
|
|
- String basePath = (String) ObjectUtils.getObjectValue(actuator.getIfAvailable(), "basePath");
|
|
|
- if (StringUtils.notEmpty(basePath)) {
|
|
|
- ignores.add(HttpUtils.uri(basePath, "/**"));
|
|
|
- }
|
|
|
- } catch (ClassNotFoundException ignore) {
|
|
|
- }
|
|
|
+ // 排除不需要安全校验的接口地址
|
|
|
+ Set<String> ignores = SecurityContextHolder.getSecurityIgnores(this.applicationContext);
|
|
|
if (ObjectUtils.notEmpty(ignores)) {
|
|
|
web.ignoring().antMatchers(ignores.toArray(StringUtils.EMPTY_ARRAY));
|
|
|
}
|
|
@@ -172,32 +148,8 @@ public class OAuthConfigurer extends WebSecurityConfigurerAdapter {
|
|
|
.authenticationEntryPoint(this.authenticationEntryPoint()).jwt().decoder(this.jwtDecoder())
|
|
|
.jwtAuthenticationConverter(this.jwtAuthenticationConverter()));
|
|
|
|
|
|
- // 排除不需要认证的业务接口地址
|
|
|
- Set<String> ignores = Sets.newHashSet();
|
|
|
- boolean multiserver = this.applicationContext.containsBean(MultiserverMvcConfigurer.class.getName());
|
|
|
- List<Resource> resources = multiserver ?
|
|
|
- ApplicationContextHolder.getApplicationResources() : Collections.emptyList();
|
|
|
- ApplicationContextHolder.lookupControllers(this.applicationContext).forEach(controller -> {
|
|
|
- Class<?> clazz = AopProxyUtils.ultimateTargetClass(controller);
|
|
|
-
|
|
|
- // 如果启用多服务MVC配置则将服务名作为接口地址前缀
|
|
|
- String prefix = null;
|
|
|
- if (multiserver) {
|
|
|
- Resource resource = ApplicationContextHolder.lookupClassResource(clazz, resources);
|
|
|
- prefix = resource == null ? null : ApplicationContextHolder.getApplicationName(resource);
|
|
|
- }
|
|
|
-
|
|
|
- // 遍历所有接口方法
|
|
|
- for (Method method : clazz.getDeclaredMethods()) {
|
|
|
- Authorize authorize = method.getAnnotation(Authorize.class);
|
|
|
- if ((authorize != null || (authorize = clazz.getAnnotation(Authorize.class)) != null)
|
|
|
- && !authorize.enabled()) {
|
|
|
- for (String api : SpringUtils.getApis(method)) {
|
|
|
- ignores.add(HttpUtils.uri(prefix, api));
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
+ // 排除不需要认证的接口地址
|
|
|
+ Set<String> ignores = SecurityContextHolder.getAuthorizeIgnores(this.applicationContext);
|
|
|
if (ObjectUtils.notEmpty(ignores)) {
|
|
|
http.authorizeRequests().antMatchers(ignores.toArray(StringUtils.EMPTY_ARRAY)).permitAll();
|
|
|
}
|