java - 当 spring-cloud-starter-oauth2 验证 Token 时 RestTemplate 请求 UnknownHostException
问题描述
restTemplate 在使用服务名称时抛出 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 信息
解决方案
修改 ResourceServerConfig 文件
@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);
}
}
推荐阅读
- python - 在写入文件之前将内容保存在内存中
- html - url 中的 Slash(/) 返回 500 服务器错误,如何重定向?
- postgresql - 在 pgAdmin 4 中创建触发器函数
- oracle - 在 oracle 中使用 CTE 内的变量时遇到错误
- android - 如何在 kotlin 中编写此 Richpath 代码?
- node.js - 使用节点 12.16.3、npm 6.14.4 和 webpack 4.43.0 构建 JS 应用程序时出错
- docker - Dockerized Nginx:可以在不停止容器的情况下更新静态资产吗?
- r - R mapdist vs gmapsdistance
- sql - SQL 在非 O(N^2) 时间内使用滚动窗口构建功能
- java - 如何将 bottomNavigationView 中的 selectedItem 设置为空?