FeignClient 在 oauth2 中与 hystrix 线程策略冲突问题造成的权限问题
来源:互联网 发布:网络联机小精灵 编辑:程序博客网 时间:2024/05/17 04:58
FeignClient 在 oauth2 中与 hystrix 线程策略冲突问题造成的权限问题
- FeignClient 在 oauth2 中与 hystrix 线程策略冲突问题造成的权限问题
- 问题描述
- 问题原因
- 问题解决方法
- 方法1 直接禁用 hystrix
- 方法2 设置hystrix 线程策略
- 最后的工作 OAuth2FeignRequestInterceptor
- 注册自定义的 OAuth2FeignRequestInterceptor
问题描述
在springcloud 系统中,通过feignclient 传递oauth2 token的时候,出现 no full authentication 问题
问题原因
hystrix 默认的线程策略是thread。也就是说在 hystrix启用状态下,当执行feignclient 调用的时候,会另起一个线程,导致安全上下问题传递不到子线程中。
问题解决方法
方法1 直接禁用 hystrix
通过在配置文件application.yaml 中加入如下内容,关闭feign 中的hystrix,但是现实场景最好不关闭。
feign: hystrix: enabled: false
方法2 设置hystrix 线程策略
通过在配置文件application.yaml 中加入如下内容,设置hystrix的线程策略
hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 60000 #这里将线程策略设置为SEMAPHORE strategy: SEMAPHORE
最后的工作 OAuth2FeignRequestInterceptor
添加 OAuth2FeignRequestInterceptor 自定义类
/** * author chen roger */package com.springcloud.template.common.security.jwt.Interceptor;import lombok.extern.slf4j.Slf4j;import org.springframework.cloud.security.oauth2.client.feign.OAuth2FeignRequestInterceptor;import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;import org.springframework.security.oauth2.client.OAuth2ClientContext;import org.springframework.security.oauth2.client.token.AccessTokenProvider;import org.springframework.security.oauth2.client.token.AccessTokenProviderChain;import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider;import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider;import org.springframework.security.oauth2.client.token.grant.implicit.ImplicitAccessTokenProvider;import org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordAccessTokenProvider;import java.util.ArrayList;import java.util.Collections;import java.util.List;@Slf4jpublic class MyOAuth2FeignRequestInterceptor extends OAuth2FeignRequestInterceptor { private final OAuth2ClientContext oAuth2ClientContext; private final AccessTokenProvider accessTokenProvider; public MyOAuth2FeignRequestInterceptor(OAuth2ClientContext oAuth2ClientContext) { super(oAuth2ClientContext, null); HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(); this.oAuth2ClientContext=oAuth2ClientContext; List<AccessTokenProvider> chain = new ArrayList<>(); AuthorizationCodeAccessTokenProvider authorizationCodeAccessTokenProvider= new AuthorizationCodeAccessTokenProvider(); authorizationCodeAccessTokenProvider.setRequestFactory(httpComponentsClientHttpRequestFactory); chain.add(authorizationCodeAccessTokenProvider); ImplicitAccessTokenProvider implicitAccessTokenProvider = new ImplicitAccessTokenProvider(); implicitAccessTokenProvider.setRequestFactory(httpComponentsClientHttpRequestFactory); chain.add(implicitAccessTokenProvider); ResourceOwnerPasswordAccessTokenProvider resourceOwnerPasswordAccessTokenProvider = new ResourceOwnerPasswordAccessTokenProvider(); resourceOwnerPasswordAccessTokenProvider.setRequestFactory(httpComponentsClientHttpRequestFactory); chain.add(resourceOwnerPasswordAccessTokenProvider); ClientCredentialsAccessTokenProvider clientCredentialsAccessTokenProvider = new ClientCredentialsAccessTokenProvider(); clientCredentialsAccessTokenProvider.setRequestFactory(httpComponentsClientHttpRequestFactory); chain.add(clientCredentialsAccessTokenProvider); List<AccessTokenProvider> providers=Collections.unmodifiableList(chain); accessTokenProvider=new AccessTokenProviderChain(providers); }}
注册自定义的 OAuth2FeignRequestInterceptor
/** * author chen roger */package com.springcloud.template.common.security.jwt;import com.springcloud.template.common.security.jwt.Interceptor.MyOAuth2FeignRequestInterceptor;import feign.RequestInterceptor;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;import org.springframework.security.oauth2.client.OAuth2ClientContext;@Configuration@EnableGlobalMethodSecurity(prePostEnabled = true)public class GlobalMethodSecurityConfiguration { @Bean public RequestInterceptor oAuth2FeignRequestInterceptor(OAuth2ClientContext oAuth2ClientContext){ return new MyOAuth2FeignRequestInterceptor(oAuth2ClientContext); }}
阅读全文
1 0
- FeignClient 在 oauth2 中与 hystrix 线程策略冲突问题造成的权限问题
- SpringCloud关于@FeignClient和Hystrix集成对http线程池监控问题
- ajax编码与与Spring设置编码冲突造成乱码的问题
- Atlas与页面缓存冲突造成报错问题
- 在LoaderCallbacks中使用CursorAdapter造成的内存泄露问题
- CFBundleDisplayName 在IOS9中未设置会造成的问题
- Spring Boot开发问题(一.feignclient远程其他restful风格的项目时POST中)
- feignclient设置hystrix参数
- 解决VC++与VS冲突造成具体做法如下:(英文原文文章和FileTool.exe都在附件里了)无法打开文件和无法向工程中添加文件的问题
- 关于在Spring Cloud Feign工程中使用Hystrix配置不生效的问题
- WCF服务策略文件的冲突问题
- 拷贝vmware虚拟机造成的mac地址冲突的问题
- 拷贝VMware造成mac地址冲突的问题
- ajax异步在页面造成的问题
- ubuntu中用户与权限的问题
- 线程中的冲突问题
- Linux Mount NTFS分区造成的权限问题的解决方案
- 组策略权限的继承问题
- android studio上的jni编译流程以及Android.mk文件的处理
- 理解 Angular 中的 $digest() 和 $apply()
- select传值到后台的问题
- bzoj 3545: [ONTAK2010]Peaks 线段树合并
- [leetcode]417. Pacific Atlantic Water Flow
- FeignClient 在 oauth2 中与 hystrix 线程策略冲突问题造成的权限问题
- 50万血的教训告诉你该怎样选择OA系统
- TCP Flood
- .bouncycastle.jce.exception.ExtCertPathValidatorException: Could not validate certificate: null
- 学习文档
- (5)51单片机入门案列-LCD1602静态显示字符
- Struts 2
- 权限管理——RBAC模型总结
- 小白入手mui的学习路线