java - 具有默认安全性的 Spring Security 为 PUT 提供 401,为 GET 提供 200
问题描述
我开发了一个具有基本安全性的 Spring Boot 应用程序。我有两个端点具有相同的路径和不同的 http 方法。当我使用默认密码/使用 application.yml 中给出的密码包含基本安全性时,GET api/products/{id} 已通过身份验证,PUT api/products/{id} 给出 401 未授权。
我的代码有什么问题?我是否错过了任何了解 Spring 安全性的内容?我可能错过的春季安全链接会有帮助吗?
我的代码片段如下,
我的控制器
@RestController
@RequestMapping(value = "api")
public class ProductController {
@RequestMapping(value = "/products/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> getById(@PathVariable(value = "id") Integer id) {
//Implementation
}
@RequestMapping(value = "/products/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity updateById(@PathVariable(value = "id") Integer id,
@RequestBody UpdateProductRequest updateProductRequest) {
//Implementation
}
}
应用程序.yml
spring:
security:
user:
name: admin
password: admin
build.gradle 依赖项
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-openfeign', version: '2.2.0.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
implementation 'junit:junit:4.12'
}
提前致谢
解决方案
推荐阅读
- python - 确定值的分配 - Python
- uml - Plantuml:如何在同一级别上绘制 2 个序列消息?
- android - 使用 firebase Authentication 对 Android 进行身份验证并将令牌发送到后端 Django 进行验证
- sql-server - System.FormatException: 24111: 众所周知的文本 (WKT) 输入无效
- sapui5 - 找不到文件:sap-ui-cachebuster-info.json
- swift - 刚刚从 swift 3 迁移到 swift 5。如何修复以下错误?
- angular - 为什么阿波罗变异永远不会完成?
- sql - 如何在此表上插入此值?
- sql-server - 连接 Silk Central 和 SQL Server 时的 SID 是什么?
- android - 视图在 NestedScrollView 内不可滚动,无法移动到顶部或底部