gpt4 book ai didi

java - Spring Boot 将云网关与 Oauth2 结合使用

转载 作者:行者123 更新时间:2023-11-29 08:27:08 25 4
gpt4 key购买 nike

我的问题是 Oauth2 的 Cloudgateway 安全性。但是,Oauth2的配置@EnableOAuth2Sso会导致如下错误:

Description:

Parameter 0 of method modifyRequestBodyGatewayFilterFactory in org.springframework.cloud.gateway.config.GatewayAutoConfiguration required a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' in your configuration.

当我对 Eureka 上的 Zuul 代理执行相同操作时,一切正常。请帮我解决这个问题。

这是 Cloudgateway 项目,我正在努力使其成为 Oauth2 客户端:

配置:

@Configuration
@EnableOAuth2Sso
public class UiSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
public void configure(HttpSecurity http) throws Exception {
http.antMatcher("/**")
.authorizeRequests()
.antMatchers("/", "/login**")
.permitAll()
.anyRequest()
.authenticated();
}
}

应用程序.yml:

server:
port: 8080
servlet:
session:
cookie:
name: UISESSION
security:
oauth2:
client:
clientId: SampleClientId
clientSecret: secret
accessTokenUri: http://localhost:8085/auth/oauth/token
userAuthorizationUri: http://localhost:8085/auth/oauth/authorize
resource:
userInfoUri: http://localhost:8085/auth/principal
spring:
application:
name: gateway
cloud:
gateway:
discovery:
locator:
enabled: false

routes:
- id: microservice1WelcomeRoute
uri: http://localhost:8083/view/welcome
predicates:
- Path=/microservice1/welcome

我通过授权码模型使用 Oauth2 服务器,引用这个 question :

最佳答案

Spring Cloud Gateway 依赖于 Spring Webflux(使用 Netty Web Server),Spring Cloud OAuth2 依赖于 Spring Boot Web(使用 Tomcat Web Server)...两个 Web 服务器不能同时使用!

依赖图(只有重要的):

1)

* org.springframework.cloud:spring-cloud-starter-gateway:2.0.1.RELEASE
|-* org.springframework.boot:spring-boot-starter-webflux:2.0.5.RELEASE
|-* org.springframework.boot:spring-boot-starter-reactor-netty:2.0.5.RELEASE

2)

* org.springframework.cloud:spring-cloud-starter-oauth2:2.0.0.RELEASE
|-* org.springframework.cloud:spring-cloud-starter-security:2.0.0.RELEASE
|-*org.springframework.cloud:spring-cloud-security:2.0.0.RELEASE
|-*org.springframework.boot:spring-boot-starter-web:2.0.5.RELEASE
|-*org.springframework.boot:spring-boot-starter-tomcat:2.0.5.RELEASE

总而言之,如果您排除 Tomcat 依赖项,它可能会起作用...

例如(对于gradle)

dependencies {
// ...
implementation('org.springframework.cloud:spring-cloud-starter-gateway')
implementation('org.springframework.cloud:spring-cloud-starter-oauth2') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
}
// ...
testImplementation('org.springframework.boot:spring-boot-starter-test')
}

关于java - Spring Boot 将云网关与 Oauth2 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51644755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com