Spring 对每个未经授权的 HTTP POST 返回 HTTP 405

2023-12-04

Spring 似乎没有正确路由/授权 HTTP POST 请求。当我发送 HTTP POST 请求时,我总是收到“405 Method Not allowed”响应,并在日志文件中显示:

org.springframework.web.servlet.PageNotFound - 不支持请求方法“POST”

控制器允许对 URL 进行 POST:

@RequestMapping(value = "/mypostreq", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@Secured("ROLE_USER")
public String mypostreq(@RequestBody String callBody, HttpServletRequest req, HttpServletResponse res) { return ""; }

这是 web.xml 配置:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/application-context.xml</param-value>
</context-param>
<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
<servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/web-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

发送 HTTP GET 请求后,我会得到通常的 HTTP GET -- 401 -- HTTP GET + Authorize -- 200 OK。此时我使用 cookie 来授权进一步的请求。当使用 cookie 发送授权的 HTTP POST 时,一切都会按预期工作。基本上我应该得到对 POST 的 HTTP 401 响应而不是 405。

我怀疑这是路由,但无法确定具体是什么。

这是我的 web-context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
                           http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <mvc:annotation-driven/>
    <context:annotation-config/>
    <context:component-scan base-package="com.solution"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/error/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

和 application-context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:security="http://www.springframework.org/schema/security"
       xmlns:couchdb="http://www.ektorp.org/schema/couchdb"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
                           http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

    <security:http entry-point-ref="digestEntryPoint">
        <security:custom-filter ref="digestFilter" before="BASIC_AUTH_FILTER"/>
        <security:http-basic />
        <!-- Restricts anonymous access to all the pages -->
        <security:intercept-url pattern="/clientapi/content*/sound/**" access="" /> 
        <security:intercept-url pattern="/clientapi/currentcall" access="ROLE_USER" />
    </security:http>

    <security:authentication-manager>
        <security:authentication-provider ref="daoAuthenticationProvider"/>
    </security:authentication-manager>

    <bean id="daoAuthenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
        <property name="userDetailsService" ref="solutionUserDetailsService"/>
    </bean>

    <!-- Digest authentication -->
    <bean id="digestFilter" class="org.springframework.security.web.authentication.www.DigestAuthenticationFilter">
        <property name="userDetailsService" ref="solutionUserDetailsService"/>
        <property name="authenticationEntryPoint" ref="digestEntryPoint"/>
    </bean>

    <bean id="digestEntryPoint" class="org.springframework.security.web.authentication.www.DigestAuthenticationEntryPoint">
        <property name="realmName" value="Labs solution"/>
        <property name="key" value="asdfasdf"/>
        <property name="nonceValiditySeconds" value="10"/>
    </bean>

</beans>

请求/响应如下所示:

POST https://1.1.1.1/Solution-1.0/clientapi/currentcall HTTP/1.1
Accept: application/json
Content-Type: application/json
Host: 1.1.1.1
Content-Length: 82
Expect: 100-continue
Connection: Keep-Alive

{ "action" : "start" }



HTTP/1.1 405 Method Not Allowed
Server: SolutionServer
Date: Thu, 05 Sep 2013 08:03:36 GMT
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: JSESSIONID=B2A5F1FAC6E9JSNF4E62C4F9CBA24F38; Path=/Solution-1.0/; Secure; HttpOnly
WWW-Authenticate: Digest realm="Labs Solution", qop="auth", nonce="MTM3ODMMKS8yNjA3MDpjNDczNGJhYWJjYThkMGE4NTZkNmRiOGRjZWIwNDE5NQ=="
Allow: GET

0

我有类似的问题。问题出在控制器中,该控制器处理错误页面并且仅支持 GET 方法。当我将其更改为 GET 和 POST 时,它开始工作。

Example:

@Controller
public class ErrorController {
    @RequestMapping(value = "/error" method = {RequestMethod.GET, RequestMethod.POST})
    public String error(@RequestParam(value = "err", required = false) Integer paramErrorCode, Locale locale,
            ModelMap model, HttpServletRequest httpRequest) {
         // Do something   
     }

查看具体信息https://stackoverflow.com/a/57571936/1839027

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring 对每个未经授权的 HTTP POST 返回 HTTP 405 的相关文章

随机推荐

  • Android/Facebook:在墙上发布消息

    我想通过 Android 应用程序在我自己的墙上发布消息 我有方法 loginToFacebook 来登录 单击按钮时 如果用户已登录 我希望发布一条消息 我不太熟悉 facebook API 因此我查看了 facebook 开发人员文档和
  • Swift 中是否有比 u_long/UInt64 容量更大的数字类型?

    有没有比容量更大的类型u long or UInt64在斯威夫特 我有一个函数 它需要非常大的整数来识别 28 位的信用卡号码 func myFunc number What to put here body 什么类型合适 应该number
  • System.MissingMethodException:没有为此对象定义无参数构造函数

    我将 MVC 2 0 与 Html ListBoxFor 一起使用 如下所示
  • Azure AD B2C - 使用电子邮件或移动设备注册/登录

    我正在使用 Azure AD B2C 我们的产品团队向我们提出了一项偏好 要求 允许用户使用电子邮件地址或手机号码作为用户 ID 进行注册 登录 目前 内置流程仅允许电子邮件或用户名作为用户 ID 有有力的证据建议我们创建一个自定义策略以允
  • PushState 更改 - 相当于 Chrome 扩展 onHistoryStateUpdated

    我正在将 Chrome 扩展程序移植到 Firefox 扩展程序 由于其运行的网站的性质 我需要监视pushState Chrome 扩展有一个方便的方法来处理这个问题 chrome webNavigation onHistoryState
  • 如何使用 Pandas 转置将数据从一个数据帧添加到另一个数据帧?

    客观的 使用转置将一个数据帧填充到另一个数据帧 df pd DataFrame Attributes love family tech df T 产生以下输出 0 1 2 Attributes love family tech 其次 我有另
  • 在 iOS 6 中使用 SLRequest 与 Facebook

    我目前正在尝试使用 SLRequest 在 facebook 上发布状态 这是我的代码 ACAccountStore accountStore ACAccountStore alloc init ACAccountType accountT
  • 保存 html 画布图像

    我知道这个问题可能已经有了答案 但我还没有找到它 而且我的项目有一个截止日期 所以我制作了一个 html5 画布 我希望能够用一个 或多个 按钮做两件事 我希望用户能够通过单击 保存 按钮来保存他刚刚完成的操作 理想情况下我希望下载图像 而
  • C# 相当于 C 的 fread 文件 i/o

    谁能告诉我如何在 C NET 版本 2 中以直接方式将字节数组放入结构中 就像熟悉的fread正如在 C 中所发现的 到目前为止 我在读取字节流和自动填充结构方面还没有取得太大成功 我见过一些实现 其中托管代码中存在指针骗局 方法是使用un
  • CSS 和 Javascript 不渲染 404 错误 spring-boot

    这是我正在使用的路径的片段
  • 如何在 Excel 操作窗格中使用 WPF 控件?

    有几个资源解释了如何添加WinForms控件到 Excel 看这两个 http msdn microsoft com en us library vstudio e3zbk0hz 28v vs 100 29 aspx http www cl
  • 在 Win7 中使用 MinGW 中的 pthreads

    2015年1月20日 我在Win7中使用MinGW 尝试使用MinGW和Pthreads编译我的源代码 MinGW已经支持POSIX线程 在 MinGW安装管理器 中 我们可以安装pthreads开发包和pthreads lib 但是当我编
  • 在Python中,命令行参数无需导入?

    在Python中 是否可以在不导入sys 或任何其他模块 的情况下获取命令行参数 是的 如果您使用的是 Linux 如果您知道进程 ID 则可以读取它 proc pid cmdline文件 其中包含以 null 分隔的命令行参数列表 PRO
  • VBA Excel 变量对多个键/顺序进行排序

    如何同时对多个列 键 订单 进行排序 当我运行下面的代码时 它会在执行每一行时对数据重新排序 而不是在排序中对多列进行排序 将 密钥 或 订单 编号从 1 更改为 2 等没有帮助 谢谢 wsLast Row Cells Rows Count
  • javascript数组的总和返回所有数字的字符串连接[关闭]

    Closed 这个问题是无法重现或由拼写错误引起 目前不接受答案 我有一个由 ajax 获取的 php json encode 对象 我想做的就是对这个数组求和 这是我到目前为止所做的 var json parseJSON data var
  • 如何在Tensorflow中设置分层学习率?

    我想知道是否有一种方法可以像 Caffe 中那样对不同层使用不同的学习率 我正在尝试修改预先训练的模型并将其用于其他任务 我想要的是加快新添加层的训练速度 并将训练后的层保持在较低的学习率 以防止它们被扭曲 例如 我有一个 5 卷积层的预训
  • 当服务打开时,如何强制应用程序选择退出省电模式?

    预期的行为是应用程序在处于开启状态时将始终运行 当应用程序一段时间不活动时 某些手机会将应用程序置于后台模式 我希望应用程序始终运行 即使处于待机模式 待机模式意味着当我们按下主页按钮时 应用程序将进入后台 并且它将运行一段时间 我找到了以
  • 配置按键重复延迟以检测按键是否被按下

    我正在用 C 语言编写一个程序 它使用 ncurses 来检查是否按下了某个键 问题是按键重复有延迟 例如 如果我在终端中按住 a 键 则在重复输入 a 之前会有短暂的延迟 我希望能够知道它是否是从实际按下的位置按下的 在终端中如何暂时将此
  • opengles 在 iPhone 中显示人脸

    我需要将人类 2D 脸部制作为 3D 脸部 我用了这个链接加载 obj 文件并映射纹理 此示例仅适用于立方体和金字塔 我加载了人脸 obj 文件 This loads the obj file and can get the human f
  • Spring 对每个未经授权的 HTTP POST 返回 HTTP 405

    Spring 似乎没有正确路由 授权 HTTP POST 请求 当我发送 HTTP POST 请求时 我总是收到 405 Method Not allowed 响应 并在日志文件中显示 org springframework web ser