[英]Spring boot use Cloud gateway with Oauth2
我的問題是 Oauth2 的 Cloudgateway 安全性。 但是,Oauth2 的配置@EnableOAuth2Sso
會導致以下錯誤:
描述:
org.springframework.cloud.gateway.config.GatewayAutoConfiguration 中方法 modifyRequestBodyGatewayFilterFactory 的參數 0 需要一個無法找到的類型為“org.springframework.http.codec.ServerCodecConfigurer”的 bean。
行動:
考慮在您的配置中定義一個 'org.springframework.http.codec.ServerCodecConfigurer' 類型的 bean。
當我在 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 服務器,參考這個問題:
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')
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.