spring - Trying to use Oauth2 token with feign client and hystrix -
i trying call "serviceb" "servicea", both of services resource server, trying make inter service call through "feign client , oauth2 toke" working fine below bean implmentation in configuration class:
@bean public requestinterceptor requesttokenbearerinterceptor() { return new requestinterceptor() { @override public void apply(requesttemplate requesttemplate) { oauth2authenticationdetails details = (oauth2authenticationdetails) securitycontextholder.getcontext().getauthentication() .getdetails(); requesttemplate.header("authorization", "bearer " + details.gettokenvalue()); } }; }
when trying use feign client fallback i.e. hystrix without oauth token(i.e. when none of services resource server) working fine.
but while trying use 3 of these (i.e. feignclient, hystrix , oauth2)all together, not working. every time going fallback method though services , running.
below code:
app.java
import org.json.simple.jsonobject; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.cloud.client.circuitbreaker.enablecircuitbreaker; import org.springframework.cloud.netflix.eureka.enableeurekaclient; import org.springframework.cloud.netflix.feign.enablefeignclients; import org.springframework.context.annotation.bean; import org.springframework.security.core.context.securitycontextholder; import org.springframework.security.oauth2.provider.authentication.oauth2authenticationdetails; import org.springframework.web.bind.annotation.requestbody; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.restcontroller; import feign.requestinterceptor; import feign.requesttemplate; @springbootapplication @restcontroller @enablefeignclients @enableeurekaclient @enablecircuitbreaker public class app { /*@autowired @qualifier("abc") private githubclient github;*/ @autowired private callservice callservice; public static void main( string[] args ) { springapplication.run(app.class, args); } @requestmapping(value="/feign",method=requestmethod.post,consumes="application/json") public string contributors1(@requestbody jsonobject payload) { string callservice2 = callservice.callservice(payload); return callservice2; } @bean public requestinterceptor requesttokenbearerinterceptor() { return new requestinterceptor() { @override public void apply(requesttemplate requesttemplate) { oauth2authenticationdetails details = (oauth2authenticationdetails) securitycontextholder.getcontext().getauthentication() .getdetails(); requesttemplate.header("authorization", "bearer " + details.gettokenvalue()); } }; } }
callervice.java
import org.json.simple.jsonobject; import org.springframework.beans.factory.annotation.autowired; import org.springframework.beans.factory.annotation.qualifier; import org.springframework.stereotype.service; @service public class callservice { @autowired @qualifier("abc") private githubclient github; public string callservice(jsonobject payload){ string forobject =github.contributors(payload); return forobject; } }
hystrixwrappedclient.java
import org.json.simple.jsonobject; import org.springframework.beans.factory.annotation.autowired; import org.springframework.beans.factory.annotation.qualifier; import org.springframework.stereotype.component; import com.netflix.hystrix.contrib.javanica.annotation.hystrixcommand; @component("abc") public class hystrixwrappedclient implements githubclient{ @autowired @qualifier("githubclient") private githubclient github; @override @hystrixcommand(fallbackmethod="failure") public string contributors(jsonobject payload) { return github.contributors(payload); } public string failure(jsonobject payload){ system.out.println(payload); return "failure"; } }
githubclient.java
import org.json.simple.jsonobject; import org.springframework.cloud.netflix.feign.feignclient; import org.springframework.web.bind.annotation.requestbody; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; @feignclient("mockrestservice") interface githubclient { @requestmapping(method = requestmethod.post, value = "/test",consumes="application/json") string contributors(@requestbody jsonobject payload); }
pom.xml
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.2.5.release</version> </parent> <dependencies> <dependency> <groupid>com.googlecode.json-simple</groupid> <artifactid>json-simple</artifactid> <version>1.1.1</version> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-config-client</artifactid> <version>1.0.0.release</version> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-actuator</artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka</artifactid> <version>1.0.1.release</version> </dependency> <!-- swagger documenation --> <dependency> <groupid>com.mangofactory</groupid> <artifactid>swagger-springmvc</artifactid> <version>1.0.2</version> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-security</artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-hystrix</artifactid> </dependency> <dependency> <groupid>org.springframework.security.oauth</groupid> <artifactid>spring-security-oauth2</artifactid> </dependency> <!-- new --> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-feign</artifactid> </dependency> </dependencies> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-parent</artifactid> <version>1.0.1.release</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencymanagement>
please suggest. whenever trying use feignclient, oauth2 , hystrix going fallback method.
u can http://kodgemisi.com/2017/02/use-securitycontext-feign-requestinterceptor/ hystrix execute in another/different thread, transfer security context hystrixrequestvariabledefault
Comments
Post a Comment