升级 Spring Boot 到 3.x 版本
# 升级 Spring Boot 到 3.x 版本详细步骤
Spring Boot 3.x 版本要求使用 Java 17 或更高版本,并且对项目中的一些依赖和代码进行了重要更改。
# 1. 修改 pom.xml
文件
首先,更新 pom.xml
文件中 Java 版本和 Spring Boot 版本,添加必要的依赖。
<!-- 设置 Java 版本为 17 -->
<java.version>17</java.version>
<!-- 新增必要依赖版本 -->
<properties>
<mybatis-spring-boot.version>3.0.3</mybatis-spring-boot.version>
<mysql.version>8.2.0</mysql.version>
<jaxb-api.version>2.3.1</jaxb-api.version>
<jakarta.version>6.0.0</jakarta.version>
<springdoc.version>2.5.0</springdoc.version>
</properties>
<!-- 更新 Spring Boot 版本 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.3.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 添加新的依赖配置 -->
<dependencies>
<!-- MyBatis Spring Boot Starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot.version}</version>
</dependency>
<!-- MySQL 驱动 -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- JAXB API -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>${jaxb-api.version}</version>
</dependency>
<!-- Jakarta Servlet API -->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>${jakarta.version}</version>
</dependency>
</dependencies>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 2. 更新 ruoyi-admin/pom.xml
文件中的 MySQL 依赖
将 ruoyi-admin/pom.xml
中的 MySQL 驱动版本更新为新的版本。
<!-- Mysql 驱动包 -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>${mysql.version}</version>
</dependency>
1
2
3
4
5
6
2
3
4
5
6
# 3. 更新 ruoyi-common/pom.xml
文件中的 Servlet 依赖
将 ruoyi-common/pom.xml
文件中的 javax.servlet
依赖替换为 jakarta.servlet
依赖。
<!-- Servlet 包 -->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>${jakarta.version}</version>
</dependency>
1
2
3
4
5
6
2
3
4
5
6
# 4. Java EE 转换为 Jakarta EE
Spring Boot 3.0 将所有底层依赖项从 Java EE 迁移到了 Jakarta EE。这需要将代码中所有 javax.xxxx
替换为 jakarta.xxxx
,例如:
import javax.servlet.*; // 替换为
import jakarta.servlet.*;
1
2
2
注意:某些 Java EE 原生方法如 javax.imageio.ImageIO
等无需替换。
如果嫌麻烦,可以使用 IntelliJ IDEA 的自动转换工具。
# 5. 修改 PermitAllUrlProperties.java
为了支持 @Anonymous 注解 path_pattern_parser 解析方式,对 PermitAllUrlProperties.java
进行如下修改:
package com.ruoyi.framework.config.properties;
@Configuration
public class PermitAllUrlProperties implements InitializingBean, ApplicationContextAware {
// 其他代码省略...
@Override
public void afterPropertiesSet() {
RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
map.keySet().forEach(info -> {
HandlerMethod handlerMethod = map.get(info);
// 获取方法上的注解,替代 path variable 为 *
Anonymous method = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), Anonymous.class);
Optional.ofNullable(method).ifPresent(anonymous ->
Objects.requireNonNull(info.getPathPatternsCondition().getPatternValues())
.forEach(url -> urls.add(RegExUtils.replaceAll(url, PATTERN, ASTERISK)))
);
// 获取类上的注解,替代 path variable 为 *
Anonymous controller = AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), Anonymous.class);
Optional.ofNullable(controller).ifPresent(anonymous ->
Objects.requireNonNull(info.getPathPatternsCondition().getPatternValues())
.forEach(url -> urls.add(RegExUtils.replaceAll(url, PATTERN, ASTERISK)))
);
});
}
// 其他代码省略...
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 6. 修改 SecurityConfig.java
支持 Spring Security 6 的新配置方式。更新后的 SecurityConfig.java
如下:
package com.ruoyi.framework.config;
// 其他 import 省略...
@Configuration
@EnableMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfig {
@Autowired
private UserDetailsService userDetailsService;
@Autowired
private AuthenticationEntryPointImpl unauthorizedHandler;
@Autowired
private LogoutSuccessHandlerImpl logoutSuccessHandler;
@Autowired
private JwtAuthenticationTokenFilter authenticationTokenFilter;
@Autowired
private CorsFilter corsFilter;
@Autowired
private PermitAllUrlProperties permitAllUrl;
@Bean
public AuthenticationManager authenticationManager() {
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
daoAuthenticationProvider.setUserDetailsService(userDetailsService);
daoAuthenticationProvider.setPasswordEncoder(bCryptPasswordEncoder());
return new ProviderManager(daoAuthenticationProvider);
}
@Bean
protected SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
return httpSecurity
.csrf(csrf -> csrf.disable())
.headers(headers -> headers.cacheControl(cache -> cache.disable()).frameOptions(options -> options.sameOrigin()))
.exceptionHandling(exception -> exception.authenticationEntryPoint(unauthorizedHandler))
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(requests -> {
permitAllUrl.getUrls().forEach(url -> requests.requestMatchers(url).permitAll());
requests.requestMatchers("/login", "/register", "/captchaImage").permitAll()
.requestMatchers(HttpMethod.GET, "/", "/*.html", "/**.html", "/**.css", "/**.js", "/profile/**").permitAll()
.requestMatchers("/swagger-ui.html", "/v3/api-docs/**", "/swagger-ui/**", "/druid/**").permitAll()
.anyRequest().authenticated();
})
.logout(logout -> logout.logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler))
.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class)
.addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class)
.addFilterBefore(corsFilter, LogoutFilter.class)
.build();
}
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 7. 修改 application.yml
的 Redis 配置
在 Spring Boot 3.x 中,将 Redis 配置从 spring.redis
改为 spring.data.redis
。
spring:
data:
redis:
# 其他配置...
1
2
3
4
2
3
4
# 8. 下载 Spring Boot 3.x 分支
若依提供了兼容 Spring Boot 3.x 的分支,下载并参考使用:
编辑此页 (opens new window)
上次更新: 2024/12/28, 18:32:08