在 Spring Boot 中启用 HTTP 请求 POST [英] Enable HTTP Request POST in Spring Boot

查看:89
本文介绍了在 Spring Boot 中启用 HTTP 请求 POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Spring Boot,这里是 maven 依赖

I am using Spring boot, here the maven dependency

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

对于网页,我将文件放在 src/main/resources/static 中.那里有我的 html 文件、js 库(angular、jquery)和 css 文件.

For the web pages I am placing the files in src/main/resources/static. There I have my html files, js libraries (angular, jquery), and css files.

我正在尝试使用 Angular 进行 HTTP 请求 POST(我也有一个工作正常的 GET 请求)但我得到了这个

I am trying to make an HTTP Request POST with Angular (I also have a GET Request that is working fine) but I get this

POST http://localhost:8080/xxxx/12/addEntry 405 (Method Not Allowed) 

在响应头中

HTTP/1.1 405 Method Not Allowed
Server: Apache-Coyote/1.1
X-Application-Context: application
Allow: HEAD, GET
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 09 Jul 2014 13:04:05 GMT

我意识到在响应中允许没有 POST 方法.

I realize that in the Response the allow doesn't have the POST method.

控制器中的方法

@RequestMapping(value = "/xxxx/{uid}/addEntry", method = RequestMethod.POST)
@ResponseBody
public String createEntry(@PathVariable String uid, @RequestBody  String form) {
    System.out.println(form);
    return "index.html";
}

推荐答案

有时特别是在初始测试期间 Spring 的 csrf - Cross Site Request Forgery - 保护默认启动并阻止 POST 请求发生,临时解决方法是禁用csrf.这通常在您的网络安全配置类中完成,该类扩展了 WebSecurityConfigurerAdapter

Sometimes especially during initial testing Spring's csrf - Cross Site Request Forgery - protection kicks in by default and prevents POST requests from taking place, a temporary workaround is to disable csrf. This is typically done in your Web Security Config class which extends WebSecurityConfigurerAdapter

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf().disable();
    }
}

注意:这在 Spring boot version 2.0.0.RC1 上有效,如果 用作永久解决方法

Note: This works as on Spring boot version 2.0.0.RC1 and its best if this IS NOT be used as permanent work around

这篇关于在 Spring Boot 中启用 HTTP 请求 POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆