从2.x到3.x:Spring Boot升级遇到的问题!
- 1.关于redis报错
- 2.关于servlet报错
- 2.关于Spring Security报错
报错内容采集
1.关于redis报错
报错内容:Property ‘spring.redis.host’ is Deprecated: Use ‘spring.data.redis.host’ instead.”、“Property ‘spring.redis.password’ is Deprecated: Use ‘spring.data.redis.password’ instead.
把“spring.redis”替换成“spring.data.redis”即可。
在application.yml文件里
javascript">spring.data.redis.host=127.0.0.1
spring.data.redis.port=6379
在application.yml文件里
javascript"> web:resources:add-mappings: falsedata:redis:host: 127.0.0.1port: 6379
javascript">datasource:// druid: 去除druidurl: jdbc:mysql://127.0.0.1:3306/supervision_dev?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8&allowMultiQueries=trueusername: rootpassword: admin
2.关于servlet报错
报错内容:The import javax.servlet cannot be resolved
把javax.servlet 替换为 jakarta.servlet
如:
javascript">import jakarta.servlet.ReadListener;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;
2.关于Spring Security报错
报错内容:找不到类org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
几乎是大家都会用去的WebSecurityConfigurerAdapter被删除了,原先继承这个的类现在无需继承任何类,只需要带上@Configuration注解。
原本配置WebSecurity和HttpSecurity的configure方法变为普通的@Bean方法,分别返回WebSecurityCustomizer和SecurityFilterChain。
原先的方法authorizeRequests变为authorizeHttpRequests、方法antMatchers变为requestMatchers。
修改自Spring Security官方博客的例子:
以前的写法
javascript">@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {@Overridepublic void configure(WebSecurity web) {web.ignoring().antMatchers("/ignore1", "/ignore2");}@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().anyRequest().authenticated().and().httpBasic(withDefaults());}
}
现在的写法
javascript">@Configuration
// 不再继承于WebSecurityConfigurerAdapter
public class SecurityConfiguration {@Beanpublic WebSecurityCustomizer webSecurityCustomizer() {// WebSecurityCustomizer是一个类似于Consumer<WebSecurity>的接口,函数接受一个WebSecurity类型的变量,无返回值// 此处使用lambda实现WebSecurityCustomizer接口,web变量的类型WebSecurity,箭头后面可以对其进行操作// 使用requestMatchers()代替antMatchers()return (web) -> web.ignoring().requestMatchers("/ignore1", "/ignore2");}@Beanpublic SecurityFilterChain filterChain(HttpSecurity http) throws Exception {http//使用authorizeHttpRequests()代替authorizeRequests().authorizeHttpRequests((authz) -> authz//这种写法被称为Lambda DSL,代替原来的and()链式操作.anyRequest().authenticated()).httpBasic(withDefaults());// 需要进行build(),返回SecurityFilterChainreturn http.build();}
}