【问题标题】:RestTemplate request UnknownHostException when spring-cloud-starter-oauth2 verify Token当 spring-cloud-starter-oauth2 验证 Token 时 RestTemplate 请求 UnknownHostException
【发布时间】:2020-01-29 03:02:03
【问题描述】:

restTemplate 在使用 service-name 时抛出 UnknownHostException

我已经添加了 bean restTemplate

@Configuration
public class SpringCloudConfig {
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

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

依赖:

    <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:

#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 信息

【问题讨论】:

    标签: java spring-cloud spring-security-oauth2 netflix-eureka


    【解决方案1】:

    修改资源服务器配置文件

    @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);
        }
    
    }
    
    

    【讨论】:

    • remoteTokenServices.setRestTemplate(restTemplate);
    猜你喜欢
    • 2019-04-29
    • 2020-03-28
    • 2013-04-11
    • 2021-05-07
    • 2018-01-07
    • 2017-08-28
    • 2016-07-27
    • 1970-01-01
    • 2020-04-17
    相关资源
    最近更新 更多