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

标签 spring routes spring-security dispatcher

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

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

Controller 允许对 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

最佳答案

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

示例:

@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

关于Spring 对每个未经授权的 HTTP POST 返回 HTTP 405,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18375989/

相关文章:

java - 从spring-boot-admin-server 1.4.6升级到1.5.2后的RestClientException

java - 认证请求到/j_spring_security_check 404错误

google-analytics - Google Analytics(分析)未知路线

java - Spring Security 使用什么来生成 OAuth2 token ?

spring-security - PKIX 路径验证失败 : java. security.cert.CertPathValidatorException:签名检查失败

java - 当角色是 JSON 对象时使用 hasAuthority

java - .war 的相对路径如何工作?

java - Thymeleaf:编辑表单的对象列表

c# - 路由 MVC ASP.Net 无法正常工作

angular - Ionic/Angular : Error: Cannot match any routes. URL 段