SpringBoot3.3.0升級(jí)方案
本文介紹了由SpringBoot2升級(jí)到SpringBoot3.3.0升級(jí)方案,新版本的升級(jí)可以解決舊版本存在的部分漏洞問(wèn)題。
一、jdk17下載安裝
1、下載
官網(wǎng)下載地址
Java Archive Downloads - Java SE 17



Jdk17下載后,可不設(shè)置系統(tǒng)變量java_home,僅在idea的指定項(xiàng)目中設(shè)置即可。
2、Jdk17項(xiàng)目環(huán)境設(shè)置
a).File-->Settings-->Build,Execution,Deployment-->Compiler-->Java Compiler

b).File-->Project Settings-->modules
source和Dependencies均設(shè)置為jdk17



c).File-->Plateform Settings-->SDKS

d).啟動(dòng)類Edit Configuration-->Run/Debug Configurations

二、依賴升級(jí)
主要依賴升級(jí)和替換引入
Java17 && Spring3.3.0 && mybatis-plus3.5.6 && Spring Security6.3.0 && Swagger3 && jakarta &&maven3.6
1、Java17依賴升級(jí)
<properties> <java.version>17</java.version> <mybatis-plus.version>3.5.6</mybatis-plus.version> <flowable.version>7.0.0</flowable.version> </properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>2、SpringBoot3.3.0依賴升級(jí)
<!-- SpringBoot的依賴配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.3.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>3、mybatis-plus3.5.6依賴升級(jí)
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.3</version>
</dependency><dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-core</artifactId>
<version>3.5.6</version>
<scope>compile</scope>
</dependency>4、SpringSecurity6.3.0依賴升級(jí)
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>6.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>6.3.0</version>
</dependency>5、Swagger.3.0依賴升級(jí)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>6.1.8</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency><!-- openAPI包,替換 Swagger 的 SpringFox -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>6、jakarta包替換
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
</dependency>7、其他
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>版本查看:
- mvn -version
- java -version
三、Swagger3.0升級(jí)(OpenAPI)
1、配置文件
OpenAPIConfig.java
package com.inspur.web.core.config;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author: Inspur
* @datetime: 2024/3/26
* @desc:
*/
@Configuration
public class OpenAPIConfig {
@Bean
public OpenAPI openAPI() {
return new OpenAPI()
.info(new Info()
.title("接口文檔標(biāo)題")
.description("SpringBoot3 集成 Swagger3接口文檔")
.version("v1"))
.externalDocs(new ExternalDocumentation()
.description("項(xiàng)目API文檔")
.url("/"));
}
}2、使用示例
SwaggerController.java
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
/**
* @author: zjl
* @datetime: 2024/3/26
* @desc:
*/
@Tag(name = "控制器:測(cè)試Swagger3", description = "描述:測(cè)試Swagger3")
@RestController
public class SwaggerController {
@Operation(summary = "測(cè)試Swagger3注解方法Get")
@Parameters({@Parameter(name = "id",description = "編碼"),
@Parameter(name = "headerValue",description = "header傳送內(nèi)容")})
@ApiResponses({
@ApiResponse(responseCode = "200", description = "請(qǐng)求成功"),
@ApiResponse(responseCode = "400", description = "請(qǐng)求參數(shù)沒(méi)填好"),
@ApiResponse(responseCode = "401", description = "沒(méi)有權(quán)限"),
@ApiResponse(responseCode = "403", description = "禁止訪問(wèn)"),
@ApiResponse(responseCode = "404", description = "請(qǐng)求路徑?jīng)]有或頁(yè)面跳轉(zhuǎn)路徑不對(duì)")
})
@GetMapping(value = "/swagger/student")
public Object getStudent(@RequestParam @Parameter(example = "2") String id,
@RequestHeader @Parameter(example = "2") String headerValue){
return id;
}
@Operation(summary = "測(cè)試Swagger3注解方法Post")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "請(qǐng)求成功"),
@ApiResponse(responseCode = "400", description = "請(qǐng)求參數(shù)沒(méi)填好"),
@ApiResponse(responseCode = "401", description = "沒(méi)有權(quán)限"),
@ApiResponse(responseCode = "403", description = "禁止訪問(wèn)"),
@ApiResponse(responseCode = "404", description = "請(qǐng)求路徑?jīng)]有或頁(yè)面跳轉(zhuǎn)路徑不對(duì)")
})
@PostMapping(value = "/swagger/student", produces = "application/json")
public SwaggerApiModel updateStudent(@RequestBody SwaggerApiModel model){
return model;
}
/**
* swagger 不暴漏該 api,通過(guò)@Hidden隱藏
* 但是仍然可以訪問(wèn)
* @return
*/
@Hidden
@GetMapping(value = "/swagger/hiddenApi")
public String hiddenApi(){
return "hiddenApi";
}
/**
* swagger 暴漏該 api,沒(méi)有配置@Hidden會(huì)展示
* @return
*/
@GetMapping(value = "/swagger/noHiddenApi")
public String noHiddenApi(){
return "noHiddenApi";
}
}3、swagger2和swagger3主要區(qū)別

四、SpringSecurity6
1、攔截器變化

extends HandlerInterceptorAdapter
==>
implements HandlerInterceptor
自定義攔截器
implements WebMvcConfigurer
==>
extends WebMvcConfigurationSupport
跨域配置eg:ResourceConfig.java:
addAllowedOrigin ==>
addAllowedOriginPattern
@Configuration
public class ResourcesConfig implements WebMvcConfigurer
{
@Bean
public CorsFilter corsFilter()
{
// 設(shè)置訪問(wèn)源地址
// config.addAllowedOrigin("*");
config.addAllowedOriginPattern("*");
}
}2、過(guò)濾器變化
antMatchers ==> requestMatchers
匹配地址時(shí) “**”==> “*”
示例:
Spring2:
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception
{
return super.authenticationManagerBean();
}
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception
{
httpSecurity
// CSRF禁用,因?yàn)椴皇褂胹ession
.csrf().disable()
// 認(rèn)證失敗處理類
.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
// 基于token,所以不需要session
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
// 過(guò)濾請(qǐng)求
.authorizeRequests()
// 對(duì)于登錄login 注冊(cè)register 驗(yàn)證碼captchaImage 允許匿名訪問(wèn)
.antMatchers("/login","/loginApp", "/appLogin","/register", "/captchaImage","/factory/getPublicKey").anonymous()
.antMatchers(
HttpMethod.GET,
"/",
"/*.html",
"/**/*.html",
"/**/*.css",
"/**/*.js",
"/profile/**"
).permitAll()
.antMatchers("/common/download**").anonymous()
// 除上面外的所有請(qǐng)求全部需要鑒權(quán)認(rèn)證
.anyRequest().authenticated()
.and()
.headers().frameOptions().disable();
httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler);
// 添加JWT filter
httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
// 添加CORS filter
httpSecurity.addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class);
httpSecurity.addFilterBefore(corsFilter, LogoutFilter.class);
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception
{
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
}
}
/**
* 強(qiáng)散列哈希加密實(shí)現(xiàn)
*/
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder()
{
return new BCryptPasswordEncoder();
}
}Spring3:
@Configuration
@EnableWebSecurity
@AllArgsConstructor
@EnableMethodSecurity
public class SecurityConfig
{
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
// CSRF禁用,因?yàn)椴皇褂胹ession
.csrf().disable()
// 禁用HTTP響應(yīng)標(biāo)頭
.headers().cacheControl().disable().and()
// 認(rèn)證失敗處理類
.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
// 基于token,所以不需要session
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
// 過(guò)濾請(qǐng)求
.authorizeRequests()
// 對(duì)于登錄login 注冊(cè)register 驗(yàn)證碼captchaImage 允許匿名訪問(wèn)
// // 對(duì)于登錄login 注冊(cè)register 驗(yàn)證碼captchaImage 允許匿名訪問(wèn)
.requestMatchers("/login","/loginApp", "/appLogin","/register", "/captchaImage","/factory/getPublicKey").anonymous()
.requestMatchers(
HttpMethod.GET,
"/",
"/*.html",
"/*/*.html",
"/*/*.css",
"/*/*.js",
"/profile/**"
).permitAll()
.requestMatchers("/common/download**").anonymous()
// 除上面外的所有請(qǐng)求全部需要鑒權(quán)認(rèn)證
.anyRequest().authenticated()
.and()
.headers().frameOptions().disable();
// 添加Logout filter
http.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler);
// 添加JWT filter
http.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
// 添加CORS filter
http.addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class);
http.addFilterBefore(corsFilter, LogoutFilter.class);
}
/**
* 強(qiáng)散列哈希加密實(shí)現(xiàn)
*/
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder()
{
return new BCryptPasswordEncoder();
}
}五、Maven3.6
六、javax替換 Jakarta
批量替換:
javax.persistence.* -> jakarta.persistence.*
javax.validation.* -> jakarta.validation.*
javax.servlet.* -> jakarta.servlet.*
javax.annotation.* -> jakarta.annotation.*
javax.transaction.* -> jakarta.transaction.*
import javax. ==> import jakarta.
或者使用idea工具:Refactor==>Migrate

七、controller請(qǐng)求地址問(wèn)題
對(duì)于GetMapping方法,@PathVariable(“roleId”) 需要注明變量名
public AjaxResult getInfo(@PathVariable Long roleId)
==>
public AjaxResult getInfo(@PathVariable("roleId") Long roleId)
八、配置文件修改
# swagger3
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
# 升級(jí)后可能導(dǎo)致不支持Bean的注入依賴,可以在配置文件解決
main:
allow-circular-references: true #允許循環(huán)依賴到此這篇關(guān)于SpringBoot3.3.0升級(jí)方案的文章就介紹到這了,更多相關(guān)SpringBoot3.3.0升級(jí)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatisPlus @TableName表名注解的基本操作
@TableName注解是MyBatis-Plus中用于指定實(shí)體類與數(shù)據(jù)庫(kù)表映射關(guān)系的重要工具,通過(guò)使用@TableName注解,可以明確地指定表名,支持多級(jí)表名和動(dòng)態(tài)表名,本文給大家介紹MyBatisPlus @TableName表名注解的相關(guān)操作,感興趣的朋友跟隨小編一起看看吧2025-09-09
JavaWeb學(xué)習(xí)筆記之Filter和Listener
這篇文章主要給大家介紹了關(guān)于JavaWeb學(xué)習(xí)筆記之Filter和Listener的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
SpringBoot整合SpringCloud的過(guò)程詳解
SpringCloud專注于為典型的用例和擴(kuò)展機(jī)制提供良好的開(kāi)箱即用體驗(yàn),在介紹SpringCloud默認(rèn)使用的注冊(cè)中心前,先給大家介紹下分布式系統(tǒng)的三個(gè)指標(biāo),具體內(nèi)容詳情跟隨小編一起看看吧2021-09-09
Intellij無(wú)法創(chuàng)建java文件解決方案
這篇文章主要介紹了Intellij無(wú)法創(chuàng)建java文件解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
SpringBoot讀取自定義配置文件方式(properties,yaml)
這篇文章主要介紹了SpringBoot讀取自定義配置文件方式(properties,yaml),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
springboot使用jasypt加密庫(kù)實(shí)現(xiàn)數(shù)據(jù)庫(kù)加解密示例代碼
這篇文章主要給大家介紹了關(guān)于springboot使用jasypt加密庫(kù)實(shí)現(xiàn)數(shù)據(jù)庫(kù)加解密的相關(guān)資料,Jasypt是一個(gè)用于配置文件加密的Java庫(kù),它可以用來(lái)加密和解密配置文件中的敏感信息,如數(shù)據(jù)庫(kù)密碼、API?密鑰等,需要的朋友可以參考下2024-04-04

