Spring Security 无法登陆,报错:There is no PasswordEncoder mapped for the id “null”

#6 · closed · 4 comments

View on GitHub ↗

fengerwoo

在第10章,集成Spring Security,书中220页的示例代码,点击登录后运行报错 There is no PasswordEncoder mapped for the id “null”<br/> 百度了一下发现这是因为Spring Security 版本在5.0后就要加个PasswordEncoder了 百度到的文章链接:[https://blog.csdn.net/canon_in_d_major/article/details/79675033](https://blog.csdn.net/canon_in_d_major/article/details/79675033)<br/> Github代码位置:spring-boot-enterprise-application-development/samples/security-in-action/src/main/java/com/waylau/spring/boot/blog/config/SecurityConfig.java 内 configureGlobal方法<br/> 修改后代码运行正常 <pre> <code> @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception{ auth.inMemoryAuthentication() //认证信息存储在内存中 .passwordEncoder(new BCryptPasswordEncoder()) .withUser("waylau").password(new BCryptPasswordEncoder().encode("123456")) .roles("ADMIN"); } </code> </pre>

Comments

waylau

@DreamboyA 第十章还不涉及密码加密的内容,这个第十四章会讲到。 你的版本是否与本书的不一致?

fengerwoo

@waylau 可能是吧!我的Gradle文件如下,只有Spring Boot版本不一致 是否可能是因为Spring Security未指定版本,但是现在最新版强制要求加密了? <pre> <code> buildscript { ext { springBootVersion = '2.0.2.RELEASE' } repositories { //mavenCentral() maven { url "https://repo.spring.io/snapshot" } maven { url "https://repo.spring.io/milestone" } maven { url "http://maven.aliyun.com/nexus/content/groups/public/" } } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' group = 'com.waylau.spring.boot.blog' version = '0.0.1-SNAPSHOT' sourceCompatibility = 1.8 repositories { //mavenCentral() maven { url "https://repo.spring.io/snapshot" } maven { url "https://repo.spring.io/milestone" } maven { url "http://maven.aliyun.com/nexus/content/groups/public/" } } dependencies { // 该依赖用于编译阶段 compile('org.springframework.boot:spring-boot-starter-web') // 添加 Thymeleaf 的依赖 compile('org.springframework.boot:spring-boot-starter-thymeleaf') // 添加 Spring Data JPA 的依赖 compile('org.springframework.boot:spring-boot-starter-data-jpa') // 添加 MySQL 连接驱动的依赖 compile('mysql:mysql-connector-java:6.0.5') // 添加 Spring Security 依赖 compile('org.springframework.boot:spring-boot-starter-security') // 添加Thymeleaf Spring Security 依赖 compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity4:3.0.2.RELEASE') // 该依赖用于测试阶段 testCompile('org.springframework.boot:spring-boot-starter-test') } </code> </pre>

waylau

spring-boot-starter-security 未指定版本,是会使用与 Spring Boot 版本对应的 Security 版本。 我看你的 Spring Boot 已经自行改为了 2.0.2.RELEASE,这个会导致 Security 版本 与本书的版本不一致。 建议先改为书籍中的指定的版本。

Starxyz

> @waylau 可能是吧!我的Gradle文件如下,只有Spring Boot版本不一致 > > 是否可能是因为Spring Security未指定版本,但是现在最新版强制要求加密了? > > ``` > > buildscript { > ext { > springBootVersion = '2.0.2.RELEASE' > } > repositories { > //mavenCentral() > maven { url "https://repo.spring.io/snapshot" } > maven { url "https://repo.spring.io/milestone" } > maven { url "http://maven.aliyun.com/nexus/content/groups/public/" } > } > dependencies { > classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") > } > } > > apply plugin: 'java' > apply plugin: 'eclipse' > apply plugin: 'org.springframework.boot' > apply plugin: 'io.spring.dependency-management' > > group = 'com.waylau.spring.boot.blog' > version = '0.0.1-SNAPSHOT' > sourceCompatibility = 1.8 > > repositories { > //mavenCentral() > maven { url "https://repo.spring.io/snapshot" } > maven { url "https://repo.spring.io/milestone" } > maven { url "http://maven.aliyun.com/nexus/content/groups/public/" } > } > > > dependencies { > > // 该依赖用于编译阶段 > compile('org.springframework.boot:spring-boot-starter-web') > > // 添加 Thymeleaf 的依赖 > compile('org.springframework.boot:spring-boot-starter-thymeleaf') > > // 添加 Spring Data JPA 的依赖 > compile('org.springframework.boot:spring-boot-starter-data-jpa') > > // 添加 MySQL 连接驱动的依赖 > compile('mysql:mysql-connector-java:6.0.5') > > // 添加 Spring Security 依赖 > compile('org.springframework.boot:spring-boot-starter-security') > > // 添加Thymeleaf Spring Security 依赖 > compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity4:3.0.2.RELEASE') > > > > // 该依赖用于测试阶段 > testCompile('org.springframework.boot:spring-boot-starter-test') > } > ``` 主要是密码的格式问题 https://stackoverflow.com/questions/49654143/spring-security-5-there-is-no-passwordencoder-mapped-for-the-id-null