在最新版本的Spring Security 6.0.1中,Config的写法是怎样的?

首先

我很久没有进行个人开发了,最近想用SpringSecurity来实现认证功能。但是我发现从SpringSecurity5.7之后,Config类的写法有了很大的变化。因此,我决定在这次开发中使用最新版本6.0.1来实现认证功能。然而,在开发过程中我遇到了一些困难,所以我想借这篇文章来复习一下。

顺便提一下,我是参考官方文档来进行实现的。

由於時間有限,本次只著重介紹Config類的變更點,省略了每個方法鏈的具體功能。在有時間的情況下,我想寫一篇有關從資料庫中獲取用戶信息進行登錄等連貫實現的文章。

开发环境 fā

    • Java17

 

    • postgreSQL 14.3

 

    • SpringBoot 3.0.2

 

    • SpringSecurity 6.0.1

 

    • Gradle

 

    Thymeleaf

所有的设置等

合作关系的定义
// 一部のみ記載
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'org.postgresql:postgresql'
annotationProcessor 'org.projectlombok:lombok'
设置DB连接信息

由于只需要登录功能,所以本次不需要安装PostgreSQL驱动程序,但是由于已安装了该驱动程序,如果不进行上述描述,会发生错误…

# postgres 接続設定
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=test
spring.datasource.password=test

Config类的实现

这篇文章的主要内容是关于以下内容的。以前的实现是继承了WebSecurityConfigurerAdapter,但现在可以通过Bean定义来定义SecurityFilterChain,所以可以进行以下描述。虽然方法链的写法和以前非常相似,但由于有一些细微的变化,所以我想写一些我个人理解的东西。

@Configuration
@EnableWebSecurity
@EnableMethodSecurity
public class SecurityConfig {
	@Bean
	public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
		http.formLogin(login -> login
				.loginPage("/login")
				.loginProcessingUrl("/login")
				.usernameParameter("username")  // デフォルトのため実際には記述不要 
				.passwordParameter("password")  // デフォルトのため実際には記述不要
				.defaultSuccessUrl("/")
				.failureUrl("/login?error")
				.permitAll()
		).logout(logout -> logout
				.logoutUrl("/logout")
				.logoutSuccessUrl("/login")
		).authorizeHttpRequests(authz -> authz
				.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
				.requestMatchers("/login").permitAll()
    			.requestMatchers("/regist").permitAll()
				.anyRequest().authenticated()
		);
		return http.build();
	}

	@Bean
	public PasswordEncoder passwordEncoder() {
		return PasswordEncoderFactories.createDelegatingPasswordEncoder();
	}
}

现在,我们只关注正在使用新的写作方式的部分。

■ 授权HttpRequests()

以前, 我们使用了 .authorizeRequests(),但由于其已被弃用,因此进行了更改。(参考)
可能情况是 .authorizeRequests() 很快会被删除。

· .请求匹配器()

这是 SpringSecurity6.0 中被删除的 .antMatchers() .mvcMatchers() 的替代方案。
另外,关于 requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll() 这部分,该句将HTML、CSS等静态资源设为SpringSecurity的保护例外。

在使用SpringBoot时,您可以使用`.requestMatchers(“/css/**”).permitAll()`这样的方式来指定每个静态资源的路径,这样可以方便地将其设置为统一的不需要保护的对象。

■ 密码编码器()

这里也稍作修改描述。
以前,我们使用 BCrypt 进行密码哈希化,如下所示。
然而,为考虑将来迁移的便利性等因素,建议采用下面的写法,以包括支持其他加密方式,如 noop 和 sha256,以便能更全面地满足要求。
当然,BCrypt 仍能正常工作。请查阅官方文档以获取更多详细信息。

@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}

最后

非常抱歉文章非常简洁,这里可能是重要的改变要点。
如果有错误或疏漏请务必指正。

由于最近完全没有机会接触认证功能,我对这样的更改本身并不知情。
我认为我们还在很多地方继续无意识地使用了不推荐的编码方式,
所以我希望能养成敏锐地更新到最新版本的习惯。

bannerAds