🐛 Fixing a bug. #1133 check_token 没有区分租户 导致client查询报空指针异常

This commit is contained in:
冷冷 committed 2020-04-29 11:24:03 +08:00
1 parent 3af4a03e10
commit 9f88be07dd
8 files changed
+105 -26

No files matched your search

@@ -20,16 +20,23 @@ package com.pig4cloud.pigx.common.data.tenant;
import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestInterceptor;
/**
* @author lengleng
* @date 2018/9/14
* feign 租户信息拦截
* @date 2020/4/29
* <p>
* 租户信息拦截
*/
@Configuration
public class PigxFeignTenantConfiguration {
public class PigxTenantConfiguration {
@Bean
public RequestInterceptor pigxFeignTenantInterceptor() {
return new PigxFeignTenantInterceptor();
}
@Bean
public ClientHttpRequestInterceptor pigxTenantRequestInterceptor() {
return new TenantRequestInterceptor();
}
}
@@ -0,0 +1,23 @@
package com.pig4cloud.pigx.common.data.tenant;
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import java.io.IOException;
/**
* @author lengleng
* @date 2020/4/29
* <p>
* 传递 RestTemplate 请求的租户ID
*/
public class TenantRequestInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
request.getHeaders().set(CommonConstants.TENANT_ID, String.valueOf(TenantContextHolder.getTenantId()));
return execution.execute(request, body);
}
}
@@ -4,6 +4,6 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.pig4cloud.pigx.common.data.cache.RedisCacheAutoConfiguration,\
com.pig4cloud.pigx.common.data.tenant.PigxTenantConfigProperties,\
com.pig4cloud.pigx.common.data.tenant.TenantContextHolderFilter,\
com.pig4cloud.pigx.common.data.tenant.PigxFeignTenantConfiguration,\
com.pig4cloud.pigx.common.data.tenant.PigxTenantConfiguration,\
com.pig4cloud.pigx.common.data.mybatis.MybatisPlusConfiguration,\
com.pig4cloud.pigx.common.data.resolver.WebMvcConfig
@@ -17,43 +17,44 @@
package com.pig4cloud.pigx.common.security.component;
import com.pig4cloud.pigx.common.security.handler.RestResponseErrorHandler;
import org.springframework.beans.BeansException;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Primary;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.util.Collections;
import java.util.ArrayList;
import java.util.Map;
/**
* @author lengleng
* @date 2018/11/10
*/
@ComponentScan("com.pig4cloud.pigx.common.security")
public class PigxResourceServerAutoConfiguration {
public class PigxResourceServerAutoConfiguration implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Bean
@Primary
@LoadBalanced
public RestTemplate lbRestTemplate() {
// 获取上下文配置的ClientHttpRequestInterceptor 实现
Map<String, ClientHttpRequestInterceptor> beansOfType = applicationContext
.getBeansOfType(ClientHttpRequestInterceptor.class);
RestTemplate restTemplate = new RestTemplate();
restTemplate.setInterceptors(Collections.singletonList((request, body, execution) -> {
request.getHeaders().set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
return execution.execute(request, body);
}));
restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
@Override
public void handleError(ClientHttpResponse response) throws IOException {
if (response.getRawStatusCode() != HttpStatus.BAD_REQUEST.value()) {
super.handleError(response);
}
}
});
restTemplate.setInterceptors(new ArrayList<>(beansOfType.values()));
restTemplate.setErrorHandler(new RestResponseErrorHandler());
return restTemplate;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
@@ -0,0 +1,22 @@
package com.pig4cloud.pigx.common.security.handler;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.DefaultResponseErrorHandler;
import java.io.IOException;
/**
* @author lengleng
* @date 2020/4/29
* <p>
* 重写默认的 响应失败处理器,400 不作为异常
*/
public class RestResponseErrorHandler extends DefaultResponseErrorHandler {
@Override
public void handleError(ClientHttpResponse response) throws IOException {
if (response.getRawStatusCode() != HttpStatus.BAD_REQUEST.value()) {
super.handleError(response);
}
}
}
@@ -0,0 +1,26 @@
package com.pig4cloud.pigx.common.security.interceptor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
import java.io.IOException;
/**
* @author lengleng
* @date 2020/4/29
* <p>
* 传递accpet 请求头,避免 请求返回 XML
*/
@Component
public class AccpetRequestInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
request.getHeaders().set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
return execution.execute(request, body);
}
}
@@ -15,7 +15,7 @@
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.common.security.feign;
package com.pig4cloud.pigx.common.security.interceptor;
import cn.hutool.core.collection.CollUtil;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
@@ -15,7 +15,7 @@
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.common.security.feign;
package com.pig4cloud.pigx.common.security.interceptor;
import feign.Feign;
import feign.RequestInterceptor;