我正在使用 Spring Boot 2.7.1 和spring-boot-starter-batch. 该批处理需要 2 个不同的WebClientAPI 来调用具有不同身份验证系统的 2 个不同的 API,我通过标准 Spring Boot 属性(spring.security.oauth2.client 等)进行配置。
它运行良好,但我意识到批处理在运行时正在侦听端口 8080,因为我已经导入了spring-boot-starter-web,这可以WebClient通过注入ClientRegistrationRepository. 这不是一个主要问题,但它阻止我并行启动批处理两次,因为端口已被使用......所以我想禁用网络服务器部分。
问题是,当我通过属性、代码或依赖项(通过删除spring-boot-starter-web)禁用网络服务器时,批处理不再启动,因为ClientRegistrationRepository不再加载,因为我需要
a bean of type 'org.springframework.security.oauth2.client.registration.ClientRegistrationRepository' that could not be found
Run Code Online (Sandbox Code Playgroud)
这是因为 Spring 有一个条件OAuth2ClientAutoConfiguration:
@AutoConfiguration(before = SecurityAutoConfiguration.class)
@ConditionalOnClass({ EnableWebSecurity.class, ClientRegistration.class })
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@Import({ OAuth2ClientRegistrationRepositoryConfiguration.class, OAuth2WebSecurityConfiguration.class })
public class OAuth2ClientAutoConfiguration {
}
Run Code Online (Sandbox Code Playgroud)
由于应用程序的类型不是SERVLET, but NONE,因此不会启用此功能。
我尝试过“强制加载”它:
@ImportAutoConfiguration(OAuth2ClientAutoConfiguration.class)
Run Code Online (Sandbox Code Playgroud)
但它不起作用。
查看源代码,我发现OAuth2ClientAutoConfiguration实际上加载了 2 个配置类,但它们不是公共的,所以我无法直接导入它们:
@Import({ OAuth2ClientRegistrationRepositoryConfiguration.class, OAuth2WebSecurityConfiguration.class })
Run Code Online (Sandbox Code Playgroud)
一定有一个技巧可以实现这一点..但是它是什么?
server.port=8095使用 Spring Boot,您可以通过在您的application.properties或任何自定义的 env 属性文件(即 等)application-dev.properties中application-prod.properties设置属性来自定义 Web 应用程序的端口。
使用这些属性,您的 Web 应用程序将侦听您选择的端口,并且不会再与其他内容发生冲突。如果您需要并行运行多个实例,那么使用一系列端口运行它们可能会很棒(@see /sf/answers/3981887031/。您也可以添加侦听器(@see https://stackoverflow .com/a/51876661/390462 ).或者至少具有不同server.port值的批处理应用程序的多个实例。
如果您想禁用网络服务器,最好的选择是使用属性:spring.main.web-application-type=none。
我刚刚AuthorizedClientServiceOAuth2AuthorizedClientManager在您的服务层中发现了一些有趣的东西(@see /sf/answers/4212141601/
| 归档时间: |
|
| 查看次数: |
1616 次 |
| 最近记录: |