mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-11 21:29:02 +08:00
♻️ Refactoring code. OAuth拦截器还原token 重写
This commit is contained in:
1 parent
9ae4bdf590
commit
89c14ede22
4 files changed
+28
-33
No files matched your search
+5
@@ -47,6 +47,11 @@ public interface SecurityConstants {
|
||||
*/
|
||||
String FROM_IN = "Y";
|
||||
|
||||
/**
|
||||
* 标志
|
||||
*/
|
||||
String FROM = "from";
|
||||
|
||||
/**
|
||||
* {bcrypt} 加密的特征码
|
||||
*/
|
||||
|
||||
+2
-26
@@ -2,15 +2,13 @@ package com.pig4cloud.pigx.common.security.component;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.AllNestedConditions;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.autoconfigure.condition.*;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration;
|
||||
import org.springframework.cloud.security.oauth2.client.AccessTokenContextRelay;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.security.oauth2.client.OAuth2ClientContext;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.OAuth2ClientConfiguration;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfiguration;
|
||||
@@ -38,28 +36,6 @@ public class PigxResourceServerTokenRelayAutoConfiguration {
|
||||
return new AccessTokenContextRelay(context);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class ResourceServerTokenRelayRegistrationAutoConfiguration implements WebMvcConfigurer {
|
||||
@Autowired
|
||||
AccessTokenContextRelay accessTokenContextRelay;
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(
|
||||
new HandlerInterceptorAdapter() {
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request,
|
||||
HttpServletResponse response, Object handler) throws Exception {
|
||||
accessTokenContextRelay.copyToken();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
|
||||
+5
-3
@@ -21,7 +21,9 @@ package com.pig4cloud.pigx.common.security.feign;
|
||||
|
||||
import feign.RequestInterceptor;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.cloud.security.oauth2.client.AccessTokenContextRelay;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.oauth2.client.OAuth2ClientContext;
|
||||
@@ -37,10 +39,10 @@ import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResour
|
||||
@AllArgsConstructor
|
||||
@ConditionalOnProperty("security.oauth2.client.client-id")
|
||||
public class PigxFeignClientConfiguration {
|
||||
|
||||
@Bean
|
||||
public RequestInterceptor oauth2FeignRequestInterceptor(OAuth2ClientContext oAuth2ClientContext,
|
||||
OAuth2ProtectedResourceDetails resource) {
|
||||
return new PigxFeignClientInterceptor(oAuth2ClientContext, resource);
|
||||
OAuth2ProtectedResourceDetails resource,
|
||||
AccessTokenContextRelay accessTokenContextRelay) {
|
||||
return new PigxFeignClientInterceptor(oAuth2ClientContext, resource,accessTokenContextRelay);
|
||||
}
|
||||
}
|
||||
+16
-4
@@ -17,12 +17,17 @@
|
||||
|
||||
package com.pig4cloud.pigx.common.security.feign;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
|
||||
import feign.RequestTemplate;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cloud.security.oauth2.client.AccessTokenContextRelay;
|
||||
import org.springframework.cloud.security.oauth2.client.feign.OAuth2FeignRequestInterceptor;
|
||||
import org.springframework.security.oauth2.client.OAuth2ClientContext;
|
||||
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2018/8/13
|
||||
@@ -31,32 +36,39 @@ import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResour
|
||||
@Slf4j
|
||||
public class PigxFeignClientInterceptor extends OAuth2FeignRequestInterceptor {
|
||||
private final OAuth2ClientContext oAuth2ClientContext;
|
||||
private final AccessTokenContextRelay accessTokenContextRelay;
|
||||
|
||||
/**
|
||||
* Default constructor which uses the provided OAuth2ClientContext and Bearer tokens
|
||||
* within Authorization header
|
||||
*
|
||||
* @param oAuth2ClientContext provided context
|
||||
* @param resource type of resource to be accessed
|
||||
* @param oAuth2ClientContext provided context
|
||||
* @param resource type of resource to be accessed
|
||||
* @param accessTokenContextRelay
|
||||
*/
|
||||
public PigxFeignClientInterceptor(OAuth2ClientContext oAuth2ClientContext
|
||||
, OAuth2ProtectedResourceDetails resource) {
|
||||
, OAuth2ProtectedResourceDetails resource, AccessTokenContextRelay accessTokenContextRelay) {
|
||||
super(oAuth2ClientContext, resource);
|
||||
this.oAuth2ClientContext = oAuth2ClientContext;
|
||||
this.accessTokenContextRelay = accessTokenContextRelay;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a template with the header of provided name and extracted extract
|
||||
* 1. 如果使用 非web 请求,header 区别
|
||||
* 2. 根据authentication 还原请求token
|
||||
*
|
||||
* @param template
|
||||
*/
|
||||
@Override
|
||||
public void apply(RequestTemplate template) {
|
||||
if (template.headers().isEmpty()) {
|
||||
Collection<String> fromHeader = template.headers().get(SecurityConstants.FROM);
|
||||
if (CollUtil.isNotEmpty(fromHeader) && fromHeader.contains(SecurityConstants.FROM_IN)) {
|
||||
return;
|
||||
}
|
||||
|
||||
accessTokenContextRelay.copyToken();
|
||||
if (oAuth2ClientContext != null
|
||||
&& oAuth2ClientContext.getAccessToken() != null) {
|
||||
super.apply(template);
|
||||
|
||||
Reference in new issue
Block a user