java - Spring Boot 启动器安全性未通过身份验证
问题描述
我是 Spring Boot 安全性的新手,并遵循本教程:
https://www.baeldung.com/spring-security-jdbc-authentication
我正在使用 POSTMAN 进行测试。
我在授权中使用了 Type = Basic Auth -> Type
用户名/密码 = admin/12345
我尝试了一切,但总是得到以下响应:
{
"timestamp": "2019-10-11T16:03:23.463+0000",
"status": 401,
"error": "Unauthorized",
"message": "Unauthorized",
"path": "/api/user"
}
网址之一:
http://localhost:8080/api/user
这是我的安全配置:
package com.spr.security;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.builders.WebSecurity;
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;
import org.springframework.security.crypto.password.PasswordEncoder;
import com.spr.util.Constants;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
@Autowired
private DataSource dataSource;
@Autowired
private PasswordEncoder passwordEncoder;
protected void configure(HttpSecurity http) throws Exception
{
http.httpBasic()
.and()
.authorizeRequests()
.antMatchers("/api/**").hasRole("USER")
.antMatchers("/admin/**").hasRole("ADMIN")
.and()
.csrf().disable()
.headers().frameOptions().disable();
}
@Autowired
protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception
{
auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(passwordEncoder);
/*
* By default spring security assumes `users` table for storing users
* and `authorities` table for storing roles
*/
}
@Bean
public PasswordEncoder passwordEncoder()
{
return new BCryptPasswordEncoder();
}
}
也试过:
auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(new BCryptPasswordEncoder());
我使用实体创建了下表。
users
id int AUTO_INCREMENT (PK)
username UNIQUE varchar(256)
email varchar(256)
authorities
username varchar(256)
authority varchar(256)
每张表都有一条记录
在用户中:
username = admin
password = $2y$10$llcw8Cbuww90KW1dYB6Rn.98iM0JyTiC1VBT1WveVKz99VqbhFLpG
email = abc@test.com
密码是 bcrypt-generator.com 上散列的 12345,强度为 10
在当局:
username = admin
authority = ROLE_USER
我也试过权限=用户
我的 pom.xml 中有以下依赖项
<!-- Spring data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- spring security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- for jdbc authentication -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
我的 application.properties 文件
## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url=jdbc:mysql://localhost:3306/sprboot?useSSL=false&serverTimezone=UTC
spring.datasource.username=spr
spring.datasource.password=boot
## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto=validate
spring.jackson.serialization.fail-on-empty-beans=false
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration
logging.level.org.springframework.security=DEBUG
如果没有 Spring Security,我所有的路径、控制器、jpa 等都可以正常工作。
我在这里做错了什么?
需要更多信息吗?
编辑
有没有办法在日志窗口(控制台)中查看 Spring Security 身份验证 sql 我在 application.properties 中添加了以下内容,但没有显示生成的 sql
spring.jpa.show-sql=true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type=TRACE
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
logging.level.org.springframework.security=DEBUG
我正在使用 mysql 数据库
解决方案
有两个问题,这就是我解决的方法:
将hasRole()更改为hasAuthority()
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.httpBasic()
.and()
.authorizeRequests()
.antMatchers("/api/**").hasAuthority("USER")
.antMatchers("/admin/**").hasAuthority("ADMIN")
.and()
.csrf().disable()
.headers().frameOptions().disable();
}
我在另一个关于堆栈溢出的链接中发现 Spring Security 中存在一个错误,并且 bcrypted 密码应该以$2a...开头,而不是$2b...或$2y...
推荐阅读
- mapbox - 根据数据字段对 mapbox studio poi 图标进行排序
- android - 由第三方通过“共享列表”调用。如何防止创建多个 Activity 实例?
- python - 当 input() 返回错误时如何在 Python 3 中获取用户输入
- c# - Microsoft Bot Emulator 未直观显示消息
- ios - 如何在 Swift 中使用 drive_API v3 更新/重命名 Google Drive 的文件?
- python - 如何在 discord.py 中获得反应表情符号?
- javascript - 错误:uncaughtException:未定义原始
- azure-storage - 找不到将存储的访问策略关联到 SAS 的位置
- android - 在 VS App Center 中创建的本机应用程序在启动时崩溃(Android)
- listview - Delphi Firemonkey TListview - 标题不显示是否重复