Spring Boot API Gateway仅对指定路由启用Azure AD的配置问题
Spring Boot API Gateway 部分路由启用Azure AD认证配置异常问题
我需要在Spring Boot API Gateway中仅对部分路由启用Azure AD认证,但添加官方文档示例配置后应用启动失败,未添加该配置时应用可正常运行。
首次尝试的配置代码(AadOAuth2ResourceServerSecurityConfig.java)
// AadOAuth2ResourceServerSecurityConfig.java @Configuration(proxyBeanMethods = false) @EnableWebSecurity @EnableMethodSecurity public class AadOAuth2ResourceServerSecurityConfig { @Bean public SecurityFilterChain apiFilterChain(HttpSecurity http) throws Exception { http.apply(AadResourceServerHttpSecurityConfigurer.aadResourceServer()) .and() .authorizeHttpRequests() .anyRequest().authenticated(); return http.build(); } }
首次启动报错信息
Caused by: java.lang.ClassNotFoundException: jakarta.servlet.Filter
添加jakarta.servlet-api依赖后的新报错
*************************** APPLICATION FAILED TO START *************************** Description: Parameter 0 of method setFilterChains in org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration required a bean of type 'com.azure.spring.cloud.autoconfigure.implementation.aad.configuration.properties.AadResourceServerProperties' that could not be found. Action: Consider defining a bean of type 'com.azure.spring.cloud.autoconfigure.implementation.aad.configuration.properties.AadResourceServerProperties' in your configuration. Process finished with exit code 1
我的pom.xml配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.github.stoyank7</groupId> <artifactId>gateway</artifactId> <version>0.0.1-SNAPSHOT</version> <name>gateway</name> <description>gateway</description> <properties> <java.version>17</java.version> <spring-cloud.version>2022.0.2</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-test</artifactId> <scope>test</scope> </dependency> <!-- Azure--> <dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-starter-active-directory</artifactId> <version>5.0.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-resource-server</artifactId> </dependency> <dependency> <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> <version>5.0.0</version> <scope>provided</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
我的application.yml配置
spring: main: allow-bean-definition-overriding: true cloud: azure: active-directory: enabled: true credential: client-id: xxxx app-id-uri: xxxx
需要排除Azure AD认证的路由代码
@Bean public RouteLocator buildUserRouter(RouteLocatorBuilder builder) { return builder .routes() .route(p -> p .path("/user/**") .filters(f -> f.rewritePath("/user/(?<segment>.*)", "/${segment}")) .uri(userServiceUrl)) .build(); }
请问是我的配置存在错误,还是官方文档示例有问题?我已经参考了相关Stack Overflow回答,但问题仍未解决。
内容的提问来源于stack exchange,提问作者S. Kostadinov
相关产品推荐
相关产品推荐

