✨ Introducing new features. close #3603 资源服务器本地模式默认为Local 高性能

This commit is contained in:
qraddx committed 2022-02-10 14:32:57 +08:00
1 parent 11badf807a
commit 5283b8ce19
4 files changed
+2 -97

No files matched your search

@@ -40,10 +40,4 @@ import java.lang.annotation.*;
@Import({ PigxResourceServerAutoConfiguration.class, PigxSecurityBeanDefinitionRegistrar.class })
public @interface EnablePigxResourceServer {
/**
* 是否开启本地模式
* @return true
*/
boolean isLocal() default true;
}
@@ -32,7 +32,7 @@ import org.springframework.security.web.AuthenticationEntryPoint;
* @author lengleng
* @date 2020/9/30
* <p>
* 支持本地模式 [不仅过认证中 CheckToken]的的资源服务器配置
* 支持本地模式 [不进过认证中心 CheckToken]的的资源服务器配置
*/
@Slf4j
public class PigxLocalResourceServerConfigurerAdapter extends ResourceServerConfigurerAdapter {
@@ -1,79 +0,0 @@
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.common.security.component;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.authentication.TokenExtractor;
import org.springframework.security.oauth2.provider.token.RemoteTokenServices;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.web.client.RestTemplate;
/**
* @author lengleng
* @date 2018/6/22
* <p>
* 1. 支持remoteTokenServices 负载均衡 2. 支持 获取用户全部信息
*/
@Slf4j
public class PigxResourceServerConfigurerAdapter extends ResourceServerConfigurerAdapter {
@Autowired
protected AuthenticationEntryPoint resourceAuthExceptionEntryPoint;
@Autowired
protected RemoteTokenServices remoteTokenServices;
@Autowired
private PermitAllUrlResolver permitAllUrlResolver;
@Autowired
private TokenExtractor tokenExtractor;
@Autowired
private RestTemplate lbRestTemplate;
/**
* 默认的配置,对外暴露
* @param httpSecurity
*/
@Override
@SneakyThrows
public void configure(HttpSecurity httpSecurity) {
// 允许使用iframe 嵌套,避免swagger-ui 不被加载的问题
httpSecurity.headers().frameOptions().disable();
ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry = httpSecurity
.authorizeRequests();
// 配置对外暴露接口
permitAllUrlResolver.registry(registry);
registry.anyRequest().authenticated().and().csrf().disable();
}
@Override
public void configure(ResourceServerSecurityConfigurer resources) {
remoteTokenServices.setRestTemplate(lbRestTemplate);
resources.authenticationEntryPoint(resourceAuthExceptionEntryPoint).tokenExtractor(tokenExtractor)
.tokenServices(remoteTokenServices);
}
}
@@ -18,7 +18,6 @@
package com.pig4cloud.pigx.common.security.component;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.security.annotation.EnablePigxResourceServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
@@ -45,16 +44,7 @@ public class PigxSecurityBeanDefinitionRegistrar implements ImportBeanDefinition
}
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
Boolean isLocal = (Boolean) metadata.getAnnotationAttributes(EnablePigxResourceServer.class.getName())
.get("isLocal");
if (isLocal) {
beanDefinition.setBeanClass(PigxLocalResourceServerConfigurerAdapter.class);
}
else {
beanDefinition.setBeanClass(PigxResourceServerConfigurerAdapter.class);
}
beanDefinition.setBeanClass(PigxLocalResourceServerConfigurerAdapter.class);
registry.registerBeanDefinition(SecurityConstants.RESOURCE_SERVER_CONFIGURER, beanDefinition);
}