Spring Boot - 从 2.2.5 升级到 2.3.0 后验证停止工作

spring

1个回答

写回答

Nzigsba

2025-10-01 23:25

+ 关注

Spring
Spring

升级Spring Boot版本后验证停止工作的情况

最近,我将我的Spring Boot项目从2.2.5升级到了最新的2.3.0版本。然而,升级后我发现一些验证功能停止工作了。在这篇文章中,我将分享我遇到的问题,并提供一些解决方案。

在升级到2.3.0版本后,我注意到我的身份验证功能不再按预期工作。之前,我使用了Spring Security来实现身份验证和授权。在2.2.5版本中,我可以使用以下代码配置Spring Security:

Java

@Configuration

@EnableWebSecurity

public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override

protected void configure(HttpSecurity http) throws Exception {

http.authorizeRequests()

.antMatchers("/admin/<strong>").hasRole("ADMIN")

.antMatchers("/user/</strong>").hasAnyRole("ADMIN", "USER")

.anyRequest().authenticated()

.and()

.formLogin()

.and()

.logout()

.logoutSuccessUrl("/login?logout")

.permitAll();

}

@Autowired

public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

auth.inMemoryAuthentication()

.withUser("admin").password("{noop}admin").roles("ADMIN")

.and()

.withUser("user").password("{noop}user").roles("USER");

}

}

然而,当我将Spring Boot升级到2.3.0版本后,这段代码不再有效。在新的版本中,Spring Security进行了一些重大的更改。为了解决这个问题,我需要对代码进行一些调整。

解决方案一:使用新的Spring Security配置

Spring Boot 2.3.0中,Spring Security引入了一个新的默认配置。因此,我们可以简化我们的代码,不再需要显式配置WebSecurityConfigurerAdapter。我们可以使用以下代码来实现相同的身份验证和授权功能:

Java

@Configuration

public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override

protected void configure(HttpSecurity http) throws Exception {

http.authorizeRequests()

.antMatchers("/admin/<strong>").hasRole("ADMIN")

.antMatchers("/user/</strong>").hasAnyRole("ADMIN", "USER")

.anyRequest().authenticated()

.and()

.formLogin()

.and()

.logout()

.logoutSuccessUrl("/login?logout")

.permitAll();

}

@Override

protected void configure(AuthenticationManagerBuilder auth) throws Exception {

auth.inMemoryAuthentication()

.withUser("admin").password("{noop}admin").roles("ADMIN")

.and()

.withUser("user").password("{noop}user").roles("USER");

}

}

通过这种方式,我们可以更简洁地配置Spring Security,并且在升级到2.3.0版本后,身份验证功能将继续正常工作。

解决方案二:自定义用户认证服务

除了使用新的默认配置外,我们还可以自定义用户认证服务来解决验证停止工作的问题。通过自定义UserDetAIlsService,我们可以将用户信息存储在数据库或其他外部资源中。

首先,我们需要实现UserDetAIlsService接口,如下所示:

Java

@Service

public class UserDetAIlsServiceImpl implements UserDetAIlsService {

@Override

public UserDetAIls loadUserByUsername(String username) throws UsernameNotFoundException {

// 从数据库或其他外部资源中获取用户信息

User user = userRepository.findByUsername(username)

.orElseThrow(() -> new UsernameNotFoundException("User not found: " + username));

return org.Springframework.security.core.userdetAIls.User.builder()

.username(user.getUsername())

.password(user.getPassword())

.roles(user.getRoles().toArray(new String[0]))

.build();

}

}

然后,我们需要在SecurityConfig中使用自定义的用户认证服务:

Java

@Configuration

public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired

private UserDetAIlsService userDetAIlsService;

@Override

protected void configure(AuthenticationManagerBuilder auth) throws Exception {

auth.userDetAIlsService(userDetAIlsService);

}

// 其他配置代码...

}

通过这种方式,我们可以根据实际需求自定义用户认证服务,以满足不同的验证需求。

在升级Spring Boot版本后,我遇到了身份验证停止工作的问题。通过使用新的Spring Security配置或自定义用户认证服务,我能够解决这个问题。这个经验向我展示了升级版本后可能出现的问题,以及如何解决这些问题。希望这篇文章对你在升级Spring Boot版本时遇到类似问题时有所帮助。

举报有用(4分享收藏

Copyright © 2025 IZhiDa.com All Rights Reserved.

知答 版权所有 粤ICP备2023042255号