【问题标题】:Spring cloud Feign OAuth2 request interceptor is not workingSpring Cloud Feign OAuth2 请求拦截器不起作用
【发布时间】:2018-02-02 05:19:15
【问题描述】:

我正在尝试使用 spring cloud feign 创建一个简单的 REST 客户端,以使用受 OAuth2 安全令牌保护的服务。我正在使用 OAuth2FeignRequestInterceptor 添加不记名令牌,请检查我的以下代码。我面临 401。当尝试调试我的代码时,我在我的 Request 对象中找不到承载令牌。

@Configuration
@EnableConfigurationProperties(value=OAuth2ClientCredentialsProperties.class)
@EnableOAuth2Client
@Profile(OAuth2Profiles.CLIENT_CREDENTIALS)
public class ClientCredentialsConfiguration {

    @Autowired
    private OAuth2ClientCredentialsProperties oAuth2ClientCredentialsProperties;

    @Bean
    @Qualifier("ClientCredentialsOAuth2FeignRequestInterceptor")
    public OAuth2FeignRequestInterceptor oauth2schemeRequestInterceptor() {
        return new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), oauth2schemeResourceDetails());
    }

    @Bean
    public ClientCredentialsResourceDetails oauth2schemeResourceDetails() {
        ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails();
        details.setClientId(oAuth2ClientCredentialsProperties.getClientId());
        details.setClientSecret(oAuth2ClientCredentialsProperties.getClientSecret());
        details.setAccessTokenUri(oAuth2ClientCredentialsProperties.getAccessTokenUri());
        details.setScope(oAuth2ClientCredentialsProperties.getScopes());
        return details;
    }

}

这是我的客户端界面

@FeignClient(name = "test", url = "http://localhost:8080", configuration = ClientCredentialsConfiguration.class)
interface GitHubClient {


    @RequestMapping(value = "/api/v1/products",
            produces = "application/json",
            consumes = "application/json;charset=UTF-8",
            method = RequestMethod.POST)
    ResponseEntity<Object> testUsingPOST(@RequestBody TestDTO testDTO);

我的属性在下面

server.port=10080

security.user.name=admin
security.user.password=admin
security.basic.enabled=false

org.springframework.boot.autoconfigure.EnableAutoConfiguration=sgcib.clips.bcsapi.configuration.ClientCredentialsConfiguration

feign.oauth2.enabled=true

feign.hystrix.enabled=false

我的主要课程

@SpringBootApplication
@EnableWebMvc
@Controller
@EnableFeignClients
@EnableAutoConfiguration
public class App extends SpringBootServletInitializer {


    @Autowired
    private GitHubClient gitHub;

    @RequestMapping("/")
    public String home() {
        return "index";
    }

    @RequestMapping("/{owner}")
    @ResponseBody
    public ResponseEntity<Object> contributors(@PathVariable String owner) {
        return gitHub.productsUsingPOST(new TestDTO());
    }

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

【问题讨论】:

    标签: spring-boot spring-security-oauth2 spring-cloud-netflix spring-cloud-feign


    【解决方案1】:

    我也在使用 feign 和请求拦截器。对我来说,它可以将@Bean 方法的返回类型更改为通用的 RequestInterceptor。像这样:

    @Bean
    public RequestInterceptor oauth2FeignRequestInterceptor() {
        return new OAuth2FeignRequestInterceptor(...);
    }
    

    本教程还很好地描述了如何使用 feign 设置 OAuth: spring-cloud-feign-oauth2

    【讨论】:

    • 我发现了这个问题,这是由于 ClientCredentialsConfiguration 类中的注释 @Profile(OAuth2Profiles.CLIENT_CREDENTIALS) 造成的。我错过了启用配置文件,所以我的拦截器 Bean 根本没有加载。
    【解决方案2】:

    我发现了这个问题,这是由于ClientCredentialsConfiguration 类中的注释@Profile(OAuth2Profiles.CLIENT_CREDENTIALS)。我错过了启用配置文件,所以我的拦截器 Bean 根本没有加载。

    【讨论】:

    • 感谢您的回答。使用您链接的解决方案,它对我有用。但我想使用 Spring Security 5,因为 Spring Security OAuth 已被弃用 -> spring.io/projects/spring-security-oauth。所以我不想用这个开始一个新项目。
    猜你喜欢
    • 2015-10-21
    • 1970-01-01
    • 2013-03-17
    • 2017-08-22
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 2021-07-03
    • 2017-03-08
    相关资源
    最近更新 更多