我试图构建一个Spring Web关,它正在获取JWT并将令牌发送到所有底层服务。为此,我使用以下依赖项:
<!-- Spring Boot Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-jose</artifactId>
</dependency>
<!-- Spring Boot Dependencies -->
<!-- Spring Cloud Dependencies -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- Spring Cloud Dependencies -->
字符串
我将应用程序配置为Auth0:
spring:
cloud:
gateway:
routes:
- id: my-service
uri: http://localhost:8001/
predicates:
- Path=/comments
filters:
- TokenRelay= #to send the token to the underlying service
- RemoveRequestHeader=Cookie #remove cookies since underlying services don't need them
security:
oauth2:
resourceserver:
jwt:
issuer-uri: #my issuer-uri
audience: #my audience
型
我实现了受众验证器和jwt解码器,如here所述:
@Configuration
@ConditionalOnProperty(name = {"spring.security.oauth2.resourceserver.jwt.issuer-uri"})
public class AuthenticationOauth2Configuration {
@Value("${spring.security.oauth2.resourceserver.jwt.audience}")
private String audience;
@Value("${spring.security.oauth2.resourceserver.jwt.issuer-uri}")
private String issuer;
@Bean(name = "customJwtDecoder")
public JwtDecoder getJwtDecoder() {
final NimbusJwtDecoder jwtDecoder = (NimbusJwtDecoder) JwtDecoders.fromOidcIssuerLocation(issuer);
final OAuth2TokenValidator<Jwt> audienceValidator = new JwtAudienceValidator(audience);
final OAuth2TokenValidator<Jwt> issuer = JwtValidators.createDefaultWithIssuer(this.issuer);
final OAuth2TokenValidator<Jwt> audience = new DelegatingOAuth2TokenValidator<>(issuer, audienceValidator);
jwtDecoder.setJwtValidator(audience);
return jwtDecoder;
}
}
public class JwtAudienceValidator implements OAuth2TokenValidator<Jwt> {
private final String audience;
public JwtAudienceValidator(final String audience) {
this.audience = audience;
}
@Override
public OAuth2TokenValidatorResult validate(Jwt jwt) {
final OAuth2Error error = new OAuth2Error("invalid_token", "The required audience is missing", null);
if (jwt.getAudience().contains(audience)) {
return OAuth2TokenValidatorResult.success();
}
return OAuth2TokenValidatorResult.failure(error);
}
}
型
然而,当我启动网关服务im得到以下错误:
Caused by: reactor.core.Exceptions$ErrorCallbackNotImplemented: java.lang.IllegalArgumentException: Unable to find GatewayFilterFactory with name TokenRelay
Caused by: java.lang.IllegalArgumentException: Unable to find GatewayFilterFactory with name TokenRelay
型
我真的找不到任何关于如何解决这个问题的资源。
5条答案
按热度按时间ssgvzors1#
你需要org.springframework. Boot :spring-boot-starter-oauth2-client,正如这里所说的。但我不认为你需要它,只要你使用资源服务器。Gateway将在没有任何配置的情况下将您的头转发到下游,因此您将能够在那里找到授权头。
bnlyeluc2#
为了让spring cloud gateway将token传递给下游服务并验证token,您需要以下依赖项:
字符串
lpwwtiir3#
我给予一个例子来说明爱德华·卡奇罗夫所说的:
依赖性:
字符串
service application.yml:
型
服务安全配置:
型
网关应用程序.yml
型
网关安全配置:
型
0aydgbwb4#
我遇到了同样的问题,我需要一个OAuth2消费者充当客户端,并将传入令牌转发到传出的资源请求。
当我使用的是Spring Cloud Gateway嵌入式反向代理时,我可以要求它将OAuth2访问令牌转发到它正在代理的服务。因此,上面的SSO应用程序可以简单地像这样增强(使用TokenRelay Filter):
字符串
要为SpringCloudGateway启用此功能,请添加以下依赖项
我有这个pom.xml配置:
型
vulvrdjw5#
错误定位您的文件夹可能是此错误的另一个原因。确保您没有将过滤器放在
com.yourcompany.yourproejct
之外。