我开发了一个具有基本安全性的spring引导应用程序。我有两个端点,具有相同的路径和不同的http方法。当我在application.yml中包含默认密码/密码的基本安全性时,会对GET /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
}
}application.yml
spring:
security:
user:
name: admin
password: adminbuild.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'
}提前感谢
邮递员截图更新:


发布于 2020-03-29 05:47:04
这可能是Csrf配置问题。
您必须重写WebSecurityConfigurerAdapter
试试这个..。
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.cors();
http.authorizeRequests().anyRequest().fullyAuthenticated();
http.httpBasic();
}
}https://stackoverflow.com/questions/60910482
复制相似问题