【问题标题】:Spring MVC preauthorize Controller action not getting POST requestSpring MVC 预授权控制器操作未收到 POST 请求
【发布时间】:2015-09-10 09:34:03
【问题描述】:

Spring MVC 和 Ajax 方面的菜鸟。我正在尝试使用 jQuery Ajax 帖子将一些数据从视图发送到控制器,但它要么给我 405 handleHttpRequestMethodNotSupported 或 403 :-( 有人能指出我正确的方向吗?

这是我的控制器操作 preAuthorize

@PreAuthorize( "hasAuthority('Admin')" )
@RequestMapping( value = "/configSave", method = RequestMethod.POST )
public String saveConfiguration( @RequestParam String test )
{
    return "configSave";
}

以下是发送发布请求的页面中的代码。

...
<input type="button" value="Save" id="save" onclick="saveConfig()"/>
...
function saveConfig()
    {
        var testPost = { test: "testing"};

        $.ajax(
                {
                    url: "hello/configSave",
                    data: "{'test':'testing'}",
                    contentType: "application/x-www-form-urlencoded",
                    type: "post",
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader("Accept", "application/json");
                        xhr.setRequestHeader("Content-Type", "application/json");
                    },
                    success: function () {
                        alert("foo");
                    },
                    error: function() {
                        alert("bar");
                    }
                }
        );
    }

它所做的只是向我抛出一个带有bar 警报的 403(暂时,否则为 405)错误。

编辑: 令人惊讶的是,当我不执行 ajax 发布(我需要这样做)时,它会在控制器中调用 configSave 操作。只是真的对这种行为感到困惑!

【问题讨论】:

  • headers="Accept=application/json" 尝试在你的 requestMapping 中添加这个
  • @DejanBiljecki - 不,也不使用。

标签: jquery ajax spring-mvc http-status-code-405


【解决方案1】:

设法在 Spring 文档 thisthis 中找到了几个部分,其中说我在使用 Spring Security 时需要使用 CSRF 令牌来发送发布请求。经过大量测试和跟踪后,我发现以下代码对我有用。我希望它也对其他人有所帮助。欢迎任何更好的建议。

...<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>...

...<sec:csrfMetaTags />...//Within <head>

...var csrfToken = $("meta[name='_csrf']").attr("content");...//somewhere in page load

function saveConfig()
    {
        var test = { name: "testing" };

        $.ajax(
                {
                    url: "hello/configSave",
                    data: JSON.stringify( test ),
                    dataType: "text",
                    contentType: "application/json",
                    type: "post",
                    headers: {
                        'Accept': 'application/json',
                        'Content-Type': 'application/json',
                        'X-CSRF-TOKEN': csrfToken //adding this made it WORK!
                    },
                    success: function (msg) {
                        alert("Yippie! Saved");
                    },
                    error: function() {
                        alert("Save failed, please try again!");
                    }
                }
        );
    }

【讨论】:

    【解决方案2】:

    添加headers = "Accept=application/json"

    contentType: "application/x-www-form-urlencoded", 替换为contentType : "application/json",

    这里有很多活动部件。你为什么不把它分解并在更小的部分进行验证。在Spring authentication 之外创建类似的代码。一旦你有 Ajax 工作,那么你可以添加 Spring authentication 回来。 403405 无关。因为,这只是标题不匹配,其他是安全问题。

    【讨论】:

    • 不。也没有运气。 403 (Forbidden) 这次:-(
    • @KunalPatel 删除 -- @PreAuthorize( "hasAuthority('Admin')" ) 并尝试。禁止是一个更好的错误:)
    • 你在使用spring security吗?
    • @java_dude,如果我删除它,我仍然会得到相同的 403。Dejan,是的,我正在使用 Spring Security。
    • 我用一个示例项目对其进行了测试,在没有弹簧安全性的情况下它可以正常工作。它仍然在向我扔 403!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    相关资源
    最近更新 更多