WARN : Request method 'DELETE' not supported

本文记录了一次在SpringMVC+SpringSecurity项目中遇到的DELETE请求权限问题,详细描述了如何从Requestmethod 'DELETE' not supported的错误提示出发,逐步排查并最终定位到问题实质为权限不足的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目环境:springMVC+Spring security

由于工程是用restful风格请求,删除使用DELETE方式请求,但一直请求一个方法报错:

WARN : Request method 'DELETE' not supported

请求地址是:http://127.0.0.1:8090/kyxgcontrol/api/t/proLabelCata/9281

我一直以为是自己工程或代码的问题,弄了好久找不到问题,还百度到官方jdk中Request method 'DELETE' not supported的bug,官方bug地址:https://bugs.openjdk.java.net/browse/JDK-7157360

但上面也没说解决方案好像,英文不好,所以也不确定,但我其他方法用delete是可以的,说明我的项目是可以用delete请求的。

把工程日志调成debug模式,再次用delete请求http://127.0.0.1:8090/kyxgcontrol/api/t/proLabelCata/9281,发现debug日志中报错了,还是权限不够的错误。

这才发现原来是Spring security的错误,看了Spring security的配置才发现api/t/proLabelCata/9281上要权限的,错误日志如下:

2017-04-20 10:02:41 DEBUG: /api/t/proLabelCata/9281 at position 6 of 11 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter'
2017-04-20 10:02:41 DEBUG: /api/t/proLabelCata/9281 at position 7 of 11 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter'
2017-04-20 10:02:41 DEBUG: Populated SecurityContextHolder with anonymous token: 'org.springframework.security.authentication.AnonymousAuthenticationToken@6fab4e5e: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@fffe3f86: RemoteIpAddress: 127.0.0.1; SessionId: 3612CA04E356CA63E1BB358BD06DB81A; Granted Authorities: ROLE_ANONYMOUS'
2017-04-20 10:02:41 DEBUG: /api/t/proLabelCata/9281 at position 8 of 11 in additional filter chain; firing Filter: 'SessionManagementFilter'
2017-04-20 10:02:41 DEBUG: /api/t/proLabelCata/9281 at position 9 of 11 in additional filter chain; firing Filter: 'ExceptionTranslationFilter'
2017-04-20 10:02:41 DEBUG: /api/t/proLabelCata/9281 at position 10 of 11 in additional filter chain; firing Filter: 'CustomFilterSecurityInterceptor'
2017-04-20 10:02:41 DEBUG: Public object - authentication not attempted
2017-04-20 10:02:41 DEBUG: /api/t/proLabelCata/9281 at position 11 of 11 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'
2017-04-20 10:02:41 DEBUG: Checking match of request : '/api/t/prolabelcata/9281'; against '/api/**'
2017-04-20 10:02:41 DEBUG: Secure object: FilterInvocation: URL: /api/t/proLabelCata/9281; Attributes: [isAuthenticated()]
2017-04-20 10:02:41 DEBUG: Previously Authenticated: org.springframework.security.authentication.AnonymousAuthenticationToken@6fab4e5e: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@fffe3f86: RemoteIpAddress: 127.0.0.1; SessionId: 3612CA04E356CA63E1BB358BD06DB81A; Granted Authorities: ROLE_ANONYMOUS
2017-04-20 10:02:41 DEBUG: Voter: org.springframework.security.web.access.expression.WebExpressionVoter@532deba7, returned: -1
2017-04-20 10:02:41 DEBUG: Access is denied (user is anonymous); redirecting to authentication entry point
org.springframework.security.access.AccessDeniedException: Access is denied
    at org.springframework.security.access.vote.AffirmativeBased.decide(AffirmativeBased.java:83)
    at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:206)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:115)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

2017-04-20 10:02:41 DEBUG: DefaultSavedRequest added to Session: DefaultSavedRequest[http://127.0.0.1:8090/kyxgcontrol/api/t/proLabelCata/9281]
2017-04-20 10:02:41 DEBUG: Calling Authentication entry point.
2017-04-20 10:02:41 DEBUG: Redirecting to 'http://127.0.0.1:8090/kyxgcontrol/loginPage'
2017-04-20 10:02:41 DEBUG: SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
2017-04-20 10:02:41 DEBUG: SecurityContextHolder now cleared, as request processing completed
2017-04-20 10:02:41 DEBUG: Checking match of request : '/loginpage'; against '/login.jsp*'
2017-04-20 10:02:41 DEBUG: Checking match of request : '/loginpage'; against '/loginpage*'
2017-04-20 10:02:41 DEBUG: /loginPage has an empty filter list
2017-04-20 10:02:41 DEBUG: Using SessionFactory 'sessionFactory' for OpenSessionInViewFilter
2017-04-20 10:02:41 DEBUG: Returning cached instance of singleton bean 'sessionFactory'
2017-04-20 10:02:41 DEBUG: Opening Hibernate Session in OpenSessionInViewFilter
2017-04-20 10:02:41 DEBUG: Opened session at timestamp: 14926537617
2017-04-20 10:02:41 DEBUG: DispatcherServlet with name 'springMVC' processing DELETE request for [/kyxgcontrol/loginPage]
2017-04-20 10:02:41 DEBUG: Looking up handler method for path /loginPage
2017-04-20 10:02:41 DEBUG: Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'DELETE' not supported
2017-04-20 10:02:41 DEBUG: Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'DELETE' not supported
2017-04-20 10:02:41 DEBUG: Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'DELETE' not supported


登录完,再请这个地址,发现是可以请求的,根本不是Request method 'DELETE' not supported,而是权限问题引起的。记录一下,免得下次再把这个问题困住了

但是为什么报的是Request method 'DELETE' not supported,我还是不解,有人知道么?


### 解决Spring MVC中GET请求不被支持的警告问题 当遇到`method_not_allowed (Request method 'GET' not supported)`错误时,通常是因为服务器端未配置允许特定HTTP方法访问某个资源。以下是针对此问题的具体解决方案。 #### 配置OAuth2授权服务以支持GET请求 在Spring Security OAuth2环境中,默认情况下,某些端点可能仅支持POST请求而不支持GET请求。为了使这些端点能够接受GET请求,可以在`AuthorizationServerConfigurerAdapter`类中的`configure`方法里显式声明允许GET和POST两种方式: ```java @Configuration public class OAuthSecurityConfig extends AuthorizationServerConfigurerAdapter { @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { // 显式指定token endpoint可以接收GET和POST请求 endpoints.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST); // 设置其他必要的参数,比如token services等 endpoints.tokenServices(tokenServices()); } } ``` 上述代码片段通过调用`allowedTokenEndpointRequestMethods`函数来扩展默认行为[^1]。 #### 修改控制器映射逻辑 如果问题是由于自定义Controller引起的,则需检查对应Handler Method是否有正确的@RequestMapping注解以及其子类型(如@GetMapping或@PostMapping)。例如,假设存在如下路径处理程序只响应POST操作而忽略了GET需求: ```java @RestController @RequestMapping("/example") public class ExampleController { @PostMapping(value = "/action", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<String> performAction(@RequestBody MyDto dto){ ... } } ``` 此时应补充相应的GET handler以便满足客户端发起的不同类型的请求: ```java @RestController @RequestMapping("/example") public class ExampleController { @GetMapping(value = "/action", produces = MediaType.TEXT_PLAIN_VALUE) public String readAction(){ return "This is a GET response."; } @PostMapping(value = "/action", consumes=MediaType.APPLICATION_JSON_VALUE ,produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<String> writeAction(@RequestBody MyDto dto){ ... } } ``` 以上调整确保了同一URL地址既能服务于数据读取(即展示信息),也能用于提交表单或者上传文件等功能[^2]。 #### 测试修改后的API接口 完成编码更改之后,建议利用Postman这样的第三方测试工具验证新加入的功能是否正常运作。对于新增加的支持GET模式下的endpoint,请记得切换到恰当的方法选项卡并重新发送查询请求。 --- ###
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值