首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当RestTemplate -cloud-starter-OAuth2验证令牌时请求UnknownHostException

当RestTemplate -cloud-starter-OAuth2验证令牌时请求UnknownHostException
EN

Stack Overflow用户
提问于 2019-09-30 00:58:23
回答 1查看 104关注 0票数 0

使用服务名时restTemplate抛出UnknownHostException

我已经添加了bean restTemplate

代码语言:javascript
复制
@Configuration
public class SpringCloudConfig {
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

我在parent pom中使用了Spring-cloud Greenwich.SR3

依赖关系:

代码语言:javascript
复制
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-oauth2</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
    </dependency>

yml:

代码语言:javascript
复制
#OAuth
security:
  oauth2:
    resource:
      loadBalanced: true
      token-info-uri: http://FLY-AUTH/oauth/check_token
    client:
      client-id: sanke
      client-secret: sanke
      scope: all

yml中的OAuth信息

EN

回答 1

Stack Overflow用户

发布于 2019-10-07 22:29:24

修改ResourceServerConfig文件

代码语言:javascript
复制
@Configuration
@EnableResourceServer
@AllArgsConstructor
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

    private final EntryPointUnauthorizedHandler entryPointUnauthorizedHandler;
    private final MyAccessDeniedHandler myAccessDeniedHandler;
    private final RemoteTokenServices remoteTokenServices;
    private final RestTemplate restTemplate;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .cors()
                .and()
                .authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
                .antMatchers("/auth/**").permitAll()
                .anyRequest().permitAll()
                .and()
                .exceptionHandling()
                .authenticationEntryPoint(entryPointUnauthorizedHandler)
                .accessDeniedHandler(myAccessDeniedHandler);
    }

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        DefaultAccessTokenConverter accessTokenConverter = new DefaultAccessTokenConverter();
        UserAuthenticationConverter userTokenConverter = new FlyUserAuthenticationConverter();
        accessTokenConverter.setUserTokenConverter(userTokenConverter);
        //Config restTemplete
        remoteTokenServices.setRestTemplate(restTemplate);
        remoteTokenServices.setAccessTokenConverter(accessTokenConverter);
        resources.tokenServices(remoteTokenServices);
        resources.authenticationEntryPoint(entryPointUnauthorizedHandler);
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58157356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档