我正在尝试将Spring boot 1.5应用程序移植到Spring Boot 2
现在我无法获得OAuth2访问令牌.
这是我成功使用Spring Boot 1.5的代码:
public static String loginAndGetAccessToken(String username, String password, int port) {
ResourceOwnerPasswordResourceDetails resourceDetails = new ResourceOwnerPasswordResourceDetails();
resourceDetails.setUsername(username);
resourceDetails.setPassword(password);
resourceDetails.setAccessTokenUri(String.format("http://localhost:%d/api/oauth/token", port));
resourceDetails.setClientId("clientapp");
resourceDetails.setClientSecret("123456");
resourceDetails.setGrantType("password");
resourceDetails.setScope(Arrays.asList("read", "write"));
DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();
OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails, clientContext);
restTemplate.setMessageConverters(Arrays.asList(new MappingJackson2HttpMessageConverter()));
return restTemplate.getAccessToken().toString();
}
Run Code Online (Sandbox Code Playgroud)
它失败并出现以下异常:
java.lang.IllegalStateException: An OAuth 2 access token must be obtained or an exception thrown.
at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainAccessToken(AccessTokenProviderChain.java:124)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.acquireAccessToken(OAuth2RestTemplate.java:221)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.getAccessToken(OAuth2RestTemplate.java:173)
Run Code Online (Sandbox Code Playgroud)
看起来http://localhost:%d/api/oauth/token端点现在是安全的,无法访问
这是我的配置:
OAuth2ServerConfig
@Configuration
public class OAuth2ServerConfig {
public …Run Code Online (Sandbox Code Playgroud) 我正在使用Spring Boot 2 M3执行器.默认情况下,运行状况端点映射到/application/health.
/health?根据当前的doc(5.0.0.RELEASE),Spring Webflux在使用带注释的控制器时支持验证:
默认情况下,如果类路径上存在Bean Validation - 例如Hibernate Validator,则LocalValidatorFactoryBean将注册为全局Validator,以便与@Valid和@Controller方法参数上的Validated一起使用.
但是没有说明如何使用功能端点自动化它.实际上,文档中唯一的输入处理示例并不验证任何内容:
public Mono<ServerResponse> createPerson(ServerRequest request) {
Mono<Person> person = request.bodyToMono(Person.class);
return ServerResponse.ok().build(repository.savePerson(person));
}
Run Code Online (Sandbox Code Playgroud)
我们应该手动执行此操作还是有一些自动方法可以执行此操作?
在我的微服务中,我必须从地方获取数据。有些 URL 是固定的,但有些不是。如果我的基本 URL 发生变化,我是否需要一次又一次地创建 Webclient。如果不是,那么下面的方法是正确的来创建 Web 客户端。WebClient.create(); 后来每当我打电话时都会一次又一次地更改 URI。根据我的理解,创建 WebClient 必须是一些繁重的操作。
ReactorClientHttpConnector connector = new ReactorClientHttpConnector(
options -> options.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, requestTimeout).compression(true)
.afterNettyContextInit(ctx -> ctx.addHandlerLast(new ReadTimeoutHandler(readTimeout, TimeUnit.MILLISECONDS))));
return WebClient.builder()
.clientConnector(connector)
.baseUrl(hostURL)
.build();
Run Code Online (Sandbox Code Playgroud) 将我的项目从 Spring Boot 2.7 升级到 3.0 后,我得到
找不到标志
由于 Lombok 生成的代码而导致编译器错误。
有什么方法可以让它协同工作 - Spring Boot 3 和 Lombok 注释。
从 Spring Boot 2.7.2 升级到 Spring Boot 3.0.0-SNAPSHOT。
错误
Caused by: java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
Run Code Online (Sandbox Code Playgroud)
完整日志https://gist.github.com/donhuvy/4eb218437077e58f996937ea255fb359
如何修复它?
看起来 SocketUtils 现在作为 Spring Boot 3 升级的一部分已被弃用。在运行控制器测试时,我收到以下错误。有人遇到过并且是如何解决的吗?
main] o.s.test.context.TestContextManager : Caught exception while invoking 'afterTestClass' callback on TestExecutionListener [org.springframework.cloud.contract.wiremock.WireMockTestExecutionListener@4362d7df] for test class [class com.example.springboot.HelloControllerTest] java.lang.NoClassDefFoundError: org/springframework/util/SocketUtils
它可以通过 Spring boot 3.0.0、Java 17、spring-cloud-contract-wiremock依赖项的简单设置来重现。
以下是 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.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-boot-complete</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-complete</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- tag::actuator[] -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> …Run Code Online (Sandbox Code Playgroud) 我的代码可以在 Spring 2.x 上正常工作。Spring 2.x的源代码
\n\n文件CustomFilter.java
package com.example.security;\n\nimport jakarta.servlet.FilterChain;\nimport jakarta.servlet.ServletException;\nimport jakarta.servlet.ServletRequest;\nimport jakarta.servlet.ServletResponse;\nimport org.springframework.web.filter.GenericFilterBean;\n\nimport java.io.IOException;\n\npublic class CustomFilter extends GenericFilterBean {\n\n @Override\n public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {\n chain.doFilter(request, response);\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n文件AuthEntryPointJwt.java
package com.example.security.jwt;\n\nimport com.fasterxml.jackson.databind.ObjectMapper;\nimport jakarta.servlet.http.HttpServletRequest;\nimport jakarta.servlet.http.HttpServletResponse;\nimport org.slf4j.Logger;\nimport org.slf4j.LoggerFactory;\nimport org.springframework.http.MediaType;\nimport org.springframework.security.core.AuthenticationException;\nimport org.springframework.security.web.AuthenticationEntryPoint;\nimport org.springframework.stereotype.Component;\n\nimport java.io.IOException;\nimport java.util.HashMap;\nimport java.util.Map;\n\n@Component\npublic class AuthEntryPointJwt implements AuthenticationEntryPoint {\n\n private static final Logger logger = LoggerFactory.getLogger(AuthEntryPointJwt.class);\n\n @Override\n public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException …Run Code Online (Sandbox Code Playgroud) 在 Spring Boot 3 中,它们已发生更改,默认情况下不再忽略尾部斜杠。例如,如果我有一个 GET 资源,/users并且我导航到,/users/那么 Spring Boot webflux 现在将响应 404。
您可以通过实现WebFluxConfigurer并重写该configurePathMatching方法来更改此设置:
@Override
public void configurePathMatching(PathMatchConfigurer configurer) {
configurer.setUseTrailingSlashMatch();
}
Run Code Online (Sandbox Code Playgroud)
然而,setUseTrailingSlashMatch它已被弃用,文档说可以使用PathPatternParser.setMatchOptionalTrailingSeparator(boolean)。但是,我不明白您实际如何/在哪里配置它。
那么问题来了,我该如何设置PathPatternParser.setMatchOptionalTrailingSeparator(boolean)呢?
我正在迁移到 SpringBoot 3.0.1 并将“hibernate-envers”版本更新为“6.1.6.Final”。我的数据库是 PostgreSQL 13.6。Hibernate 配置为创建数据库模式:
spring.jpa.hibernate.ddl-auto:create
启动应用程序后,我收到以下错误:
pim 2022-12-27 12:00:13,715 WARN C#c7b942ec-33b4-4749-b113-22cbb2946a8d [http-nio-9637-exec-1] SqlExceptionHelper/133 - SQL Error: 0, SQLState: 42P01
pim 2022-12-27 12:00:13,715 ERROR C#c7b942ec-33b4-4749-b113-22cbb2946a8d [http-nio-9637-exec-1] SqlExceptionHelper/138 - ERROR: relation "revinfo_seq" does not exist
Position: 16
Run Code Online (Sandbox Code Playgroud)
revinfo 表如下所示:
create table revinfo
(
revision bigint not null
primary key,
client_id varchar(255),
correlation_id varchar(255),
origin varchar(255),
request_id varchar(255),
revision_timestamp bigint not null,
timestamp_utc timestamp with time zone,
user_name varchar(255)
);
Run Code Online (Sandbox Code Playgroud)
序列“revinfo_seq”不存在,但在带有 envers 的旧数据库结构中
5.6.8.Final
Run Code Online (Sandbox Code Playgroud)
而 SpringBoot 2.6.6 它也不存在,没有任何问题。我缺少什么?
我尝试切换参数
org.hibernate.envers.use_revision_entity_with_native_id …Run Code Online (Sandbox Code Playgroud)