首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有默认安全性的Spring安全为PUT提供401,GET为200

具有默认安全性的Spring安全为PUT提供401,GET为200
EN

Stack Overflow用户
提问于 2020-03-29 05:22:34
回答 2查看 379关注 0票数 0

我开发了一个具有基本安全性的spring引导应用程序。我有两个端点,具有相同的路径和不同的http方法。当我在application.yml中包含默认密码/密码的基本安全性时,会对GET /products/{id}进行身份验证,PUT api/products/{id}给401未经授权。

我的代码有什么问题?我错过了什么了解Spring安全的东西吗?任何链接,我可能错过了春天的安全将是有帮助的?

我的代码片段如下,

我的控制器

代码语言:javascript
复制
@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

代码语言:javascript
复制
spring:
  security:
    user:
      name: admin
      password: admin

build.gradle依赖关系

代码语言:javascript
复制
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'
}

提前感谢

邮递员截图更新:

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-03-29 05:47:04

这可能是Csrf配置问题。

您必须重写WebSecurityConfigurerAdapter

试试这个..。

代码语言:javascript
复制
@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();
  }
}
票数 0
EN

Stack Overflow用户

发布于 2020-03-29 05:42:10

您的问题是跨站点请求伪造(CSRF)。参见问题及其答案。

有关CSRF的更多信息,请参见站点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60910482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档