这是我参与 8 月更文挑战的第 6 天,活动详情查看: 8月更文挑战
1. 测试Spring Security时报错
控制台报错:
Circular view path [index]: would dispatch back to the current handler URL [/index] again.
网页报错500
刚开始以为是Security版本不匹配的问题,换了几个版本后,问题没有解决,
上网查找了下原因,才发现是我导入的thymeleaf依赖与springboot版本不兼容。
将原本springboot版本2.5.3降成2.5.2后,就成功了。
以下是我在pom.xml中,springboot与thymeleaf的配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- Thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
</dependencies>
2. 当用springSecurity做用户注册验证时报错:
原代码:
package com.tjm.config;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//链式编程
//授权
@Override
protected void configure(HttpSecurity http) throws Exception {
//首页所有人可以访问,功能页只有对应有权限的人才能访问
//请求授权的规则
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
//没有权限默认到登录页,需要开启登录的页面
http.formLogin();
}
//认证
//密码编码: PasswordEncoder
//.and().withUser()认证多个用户
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//这些数据正常从数据库中获得
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("tjm").password("123456").roles("vip2","vip3")
.and()
.withUser("root").password("123456").roles("vip1","vip2","vip3")
.and()
.withUser("guest").password("123456").roles("vip1");
}
}
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Aug 06 22:28:08 CST 2021
There was an unexpected error (type=Internal Server Error, status=500).
可以看到控制台在报错:
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
这是由于Spring Security 5.0+ 新增了很多的加密方法,当你直接输入密码时,Spring Security认为这不安全,必须要对密码进行编码后,才作为参数输入。
上述代码修改为:
//认证
//密码编码: PasswordEncoder
//.and().withUser()认证多个用户
//在Spring Security 5.0+ 新增了很多的加密方法
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//这些数据正常从数据库中获得
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("tjm").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
.and()
.withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")
.and()
.withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
}
除了BCryptPasswordEncoder这种编码方式外,也可以选择其他的编码方式。