spring-cloud-starter-feign 组件在spring boot中的应用:访问Oauth2资源服务器的资源接口

spring-cloud-starter-feign 组件在spring boot中的应用:

访问Oauth2资源服务器的资源接口

   
      如果对https://github.com/OpenFeign/feign  不了解的,可以去官网浏览一下。由于spring对其扩展,我们可以利用spring的注解及spring boot 的特性来使用该组件,可参考文档:http://cloud.spring.io/spring-cloud-static/Dalston.SR1/#spring-cloud-feign 。

    下面的例子是spring boot项目中,访问Oauth2资源服务器的资源接口(非集群环境下)。

    maven pom文件需要引入对spring cloud相关依赖:

 
        
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            
            
                org.springframework.cloud
                spring-cloud-dependencies
                Dalston.SR1
                pom
                import
            
        
    

 为了使用feign组件,我们增加依赖:


            org.springframework.cloud
            spring-cloud-starter-config
            
                
                
                    org.springframework.cloud
                    spring-cloud-config-client
                
            

        
        
            org.springframework.cloud
            spring-cloud-starter-feign
        
        
            com.netflix.feign
            feign-okhttp
            8.18.0
        

     为了使用spring boot 的自动配置,我们需要引入       @EnableFeignClients 注解,自动扫描 Feign相关代码。

@SpringBootApplication(
        exclude = {DataSourceAutoConfiguration.class,
                MybatisAutoConfiguration.class,
                JpaRepositoriesAutoConfiguration.class,
                HibernateJpaAutoConfiguration.class},
        scanBasePackages = {"com.sdcuike.practice",
                "com.sdcuike.spring.controller.advice"})
@EnableFeignClients
@Slf4j
public class Application {
    public static void main(String[] args) throws UnknownHostException {
        SpringBootApplicationUtil.runSpringBootApplication(log, args, Application.class);
    }
    
}


 为了访问资源服务器上的资源,我们定义demo接口:


/**
 * Created by beaver on 2017/5/27.
*

* {@link https://jmnarloch.wordpress.com/2015/10/14/spring-cloud-feign-oauth2-authentication } */ @FeignClient(name = "exampleService", url = "${exampleService.ribbon.listOfServers}", configuration = {OAuth2FeignAutoConfiguration.class}) @RequestMapping(value = "/springBoot/example" , consumes = MediaType.APPLICATION_JSON_UTF8_VALUE , produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public interface ExampleServiceFeignClient { @GetMapping("/") ModelResult home(); }


  由于spring-cloud-feign对feign做了扩展,所以,我们可以使用spring的web相关注解,来使用feign,大大方便了我们的spring 应用。其关键的配置在于:

@FeignClient(name = "exampleService",
        url = "${exampleService.ribbon.listOfServers}",
        configuration = {OAuth2FeignAutoConfiguration.class})

  注解中name属性最新版本必须要求有,而url的出现,主要显示的配置 feign访问的服务地址。
  OAuth2FeignAutoConfiguration类主要是feign组件利用Oauth2的认证token来访问资源服务器上的资源。

/**
 * Created by beaver on 2017/6/3.
 */
@Configuration
public class OAuth2FeignAutoConfiguration {
    
    @Bean
    public RequestInterceptor oauth2FeignRequestInterceptor(@Qualifier("practiceOAuth2RestTemplate") OAuth2RestTemplate oAuth2RestTemplate) {
        return new OAuth2FeignRequestInterceptor(oAuth2RestTemplate);
    }
}

/**
 * Created by beaver on 2017/6/3.
 */
public class OAuth2FeignRequestInterceptor implements RequestInterceptor {
    
    private final Logger LOGGER = LoggerFactory.getLogger(getClass());
    
    private static final String AUTHORIZATION_HEADER = "Authorization";
    
    private static final String BEARER_TOKEN_TYPE = "Bearer";
    
    private final OAuth2RestTemplate oAuth2RestTemplate;
    
    
    public OAuth2FeignRequestInterceptor(OAuth2RestTemplate oAuth2RestTemplate) {
        Assert.notNull(oAuth2RestTemplate, "Context can not be null");
        this.oAuth2RestTemplate = oAuth2RestTemplate;
    }
    
    @Override
    public void apply(RequestTemplate template) {
        LOGGER.debug("Constructing Header {} for Token {}", AUTHORIZATION_HEADER, BEARER_TOKEN_TYPE);
        template.header(AUTHORIZATION_HEADER,
                String.format("%s %s",
                        BEARER_TOKEN_TYPE,
                        oAuth2RestTemplate.getAccessToken().toString()));
        
    }
}

  其主要思路是利用 feign的的扩展RequestInterceptor,对于每次请求我们自动设置Oauth2的AccessToken 头信息。

  对于Oauth2ClientConfig 的配置在此不再复述,具体请看代码:
https://github.com/sdcuike/spring-boot-oauth2-demo/tree/blog-2017-07-16/spring-boot-oauth-resource-server/src/main/java/com/sdcuike/practice


你可能感兴趣的:(Spring,Boot,Spring,Boot,实战)