亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

憑據錯誤的 POST 請求返回 405 而不是 401

憑據錯誤的 POST 請求返回 405 而不是 401

大話西游666 2023-06-14 14:01:52
我正在嘗試在由 Spring Security 保護的 Spring 中創建新的 POST 端點。端點正在工作。如果我提供正確的數據和憑據,服務器會處理請求并返回 200。如果我提供的憑據不正確,我會得到 405,我預計會得到 401。我還嘗試將控制器的請求方法從 POST 更改為 GET,并將請求從 POST 更改為 GET,并且成功了!對于錯誤的憑據,a 收到 401,對于正確的憑據,收到 200。這是我的配置:控制器@Controller@RequestMapping(value = "/api/v1")@Secured(UserRoles.ROLE_USER)public class ApiController {? ? @RequestMapping(value = "/test", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)? ? @ResponseStatus(value = HttpStatus.OK)? ? @ResponseBody? ? public ResponseEntity<String> handleRequest(@RequestBody DataBody dataBody, Model model, Locale locale) {? ? ? ? try{? ? ? ? ? ? //do something? ? ? ? ? ? return new ResponseEntity<>("received", HttpStatus.OK);? ? ? ? } catch (SomeProblemException e) {? ? ? ? ? ? return new ResponseEntity<>(e.toString(), HttpStatus.BAD_REQUEST);? ? ? ? }? ? }HTTP 請求(郵遞員)POST /api/v1/test HTTP/1.1Host: localhost:8080Content-Type: application/jsonAuthorization: Basic bWFudGfmZjptYW50YQ==User-Agent: PostmanRuntime/7.15.2Accept: */*Host: localhost:8080{? ? "something": "data",? ? "somethingelse": "data"}HTTP 響應(郵遞員)Status 405WWW-Authenticate: Basic realm="Spring Security Application"Allow: GET我使用 Spring Security 3.2.10-RELEASE。我嘗試啟用 Spring Security 日志,但我失敗了。
查看完整描述

2 回答

?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

問題出在這里:


@Controller

public class ErrorController {

    @RequestMapping("/error")

    public String error(@RequestParam(value = "err", required = false) Integer paramErrorCode, Locale locale,

            ModelMap model, HttpServletRequest httpRequest) {

         // Do something   

     }

我有一個控制器,它處理錯誤屏幕,但它只支持 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   

     }

如果web.xml不確定是什么導致重定向


<error-page>

    <location>/error</location>

</error-page>

或 securitycontext.xml


<sec:access-denied-handler error-page="/error?err=403"/>


查看完整回答
反對 回復 2023-06-14
?
BIG陽

TA貢獻1859條經驗 獲得超6個贊

       METHOD_NOT_ALLOWED(405, "Method Not Allowed")

似乎您在測試時使用的是 GET 方法而不是 POST 方法,一旦您更改為 POSt 將獲得下一個異常是

       UNAUTHORIZED(401, "Unauthorized")


查看完整回答
反對 回復 2023-06-14
  • 2 回答
  • 0 關注
  • 144 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號