Java Spring启动程序安全性未验证

Java Spring启动程序安全性未验证,java,spring,spring-boot,spring-mvc,spring-security,Java,Spring,Spring Boot,Spring Mvc,Spring Security,我是spring boot security的新手,并遵循本教程: 我用邮递员来测试 我在授权->类型中使用了Type=Basic Auth 用户名/密码=admin/12345 我什么都试过了,但总是得到以下回应: { "timestamp": "2019-10-11T16:03:23.463+0000", "status": 401, "error": "Unauthorized", "message": "Unauthorized", "path"

我是spring boot security的新手,并遵循本教程:

我用邮递员来测试

我在授权->类型中使用了Type=Basic Auth

用户名/密码=admin/12345

我什么都试过了,但总是得到以下回应:

{
    "timestamp": "2019-10-11T16:03:23.463+0000",
    "status": 401,
    "error": "Unauthorized",
    "message": "Unauthorized",
    "path": "/api/user"
}    
其中一个URL:

以下是我的安全配置:

package com.spr.security;
导入javax.sql.DataSource;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.context.annotation.Bean;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
导入org.springframework.security.config.annotation.web.builders.HttpSecurity;
导入org.springframework.security.config.annotation.web.builders.WebSecurity;
导入org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
导入org.springframework.security.config.annotation.web.configuration.websecurityConfigureAdapter;
导入org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
导入org.springframework.security.crypto.password.PasswordEncoder;
导入com.spr.util.Constants;
@配置
@启用Web安全性
公共类SecurityConfig扩展了WebSecurity配置适配器
{
@自动连线
私有数据源;
@自动连线
专用密码编码器;
受保护的无效配置(HttpSecurity http)引发异常
{
http.httpBasic()
.及()
.授权请求()
.antMatchers(“/api/**”).hasRole(“用户”)
.antMatchers(“/admin/**”).hasRole(“admin”)
.及()
.csrf().disable()
.headers().frameOptions().disable();
}
@自动连线
受保护的无效配置全局(AuthenticationManagerBuilder auth)引发异常
{
auth.jdbccauthentication().dataSource(dataSource).passwordEncoder(passwordEncoder);
/*
*默认情况下,spring security采用“users”表来存储用户
*和用于存储角色的“权限”表
*/
}
@豆子
公共密码编码器PasswordEncoder()
{
返回新的BCryptPasswordEncoder();
}
}
还尝试:

auth.jdbccauthentication().dataSource(dataSource).passwordEncoder(新的BCryptPasswordEncoder());
我使用实体创建了以下表

用户
id int自动增量(PK)
用户名唯一varchar(256)
电子邮件varchar(256)
当局
用户名varchar(256)
授权varchar(256)
每个表中只有一条记录

在用户中:

username = admin
password = $2y$10$llcw8Cbuww90KW1dYB6Rn.98iM0JyTiC1VBT1WveVKz99VqbhFLpG
email = abc@test.com
密码在bcrypt-generator.com上以10的强度散列为12345

有关当局:

username = admin
authority = ROLE_USER
我还尝试了authority=USER

我的pom.xml中有以下依赖项


org.springframework.boot
spring引导启动器数据jpa
org.springframework.boot
弹簧启动安全
org.springframework.boot
弹簧靴启动器jdbc
我的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安全性,我的所有路径、控制器、jpa等都可以正常工作

我做错了什么

需要更多的信息吗

编辑

有没有办法在日志窗口(控制台)中查看spring安全身份验证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()


我在stack overflow上的另一个链接中发现,Spring Security中有一个bug,被破解的密码应该以$2a…开头,而不是以$2b…$2y…开头。有两个问题,我就是这样解决的:

hasRole()更改为hasAuthority()

我在stack overflow上的另一个链接中发现,Spring Security中有一个bug,被破解的密码应该以$2a…开头,而不是以$2b…$2y…

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();
    }