LinkedNestBlog

$.post call response 403 http status on Chrome browser

by Dev Jinwon

$.post call response 403 http status on Chrome browser




ISSUE : 문제

프로세스 진행 중 xxx url 호출 시

- Chrome에서는 403 http status 코드를 응답받음.


- IE에서는 200 http status 코드를 응답받음.

Cause : 원인
- jsp에서 $.post call의 사용
   - RequestMethod 는 POST 방식인데 Parameter는 Get 방식처럼 queryString형태로 보낸다.

      - Body영역은 Empty 상태로 sending 되는 것이다.
      - POST방식이 사용되었으나 위와 같이 backend의 xxx API를 호출할 때에는 GET 방식으로 파라미터 전송을 하도록 되어 있다.

      - Page영역의 script부분에서 $.ajax 혹은 $.post로 POST 타입의 call을 하려면

        1. parameter를 body영역에 설정해서 보냈어야 했고
        2. 위 Controller에서 GET 타입으로 전송하는 것이 아니라 POST 타입으로 전송하도록 'getReturnBaseAsCommonPostRequest' 메서드를 사용했어야 했다.
      - 그럼 왜 getReturnBaseAsCommonGetRequest는 POST 방식 전송에서 사용하면 안될까
  • xxx.java의 yyyyy Method를 보면 'UrlParseToJsonBean.getJsonResultFromUrl(strUrl, request)'를 호출하도록 되어 있다.
public Map<String, Object> yyyyy(HttpServletRequest request, String apiName) throws JsonParseException, JsonMappingException, IOException, JSONException {
  String strUrl = API_JSON_URL + apiName;

  String result = UrlParseToJsonBean.getJsonResultFromUrl(strUrl, request);
  @SuppressWarnings("unchecked")
  HashMap<String, Object> canUse = new ObjectMapper().readValue(result, HashMap.class);
  return canUse;
}
  • UrlParseToJsonBean의 getJsonResultFromUrl를 확인해보면 Parameter를 request객체의 getQueryString()을 통해서 가져온다.
    • query string은 body와는 다르기 때문에 front 영역에서 body에 파라미터를 설정해서 call을 한다고 해도
      query string에 parameter가 전달되지 않기 때문에 empty값이 API로 전달된다.
public static String getJsonResultFromUrl(String strUrl, HttpServletRequest request) throws IOException {

  String param = "?" + request.getQueryString() + "&clientIP="
  		+ UtilRequest.getRemoteAddr(request) + "&path=" + ProjectConstant.PATH;
  logger.info("[UrlParseToJsonBean.getJsonResultFromUrl] strUrl : "+strUrl+", queryString : " + request.getQueryString());

  return util.zzzzz(strUrl + param);
}

결국 API 에서는 파라미터를 전달받지 못하는 상황이 발생되므로 제대로 process를 진행할 수 없게 된다.

Solution : 해결방법

1. front 영역에서만 처리하려면(근본적인 해결책은 아님)
  • $.post를 $.ajax로 변경
  • 기존과 동일하게 url 뒤에 파라미터를 query String 형태로 설정
  • data attribute는 설정하지 않는다.
  • contentType을 다음과 같이 설정한다.
    contentType : 'application/x-www-form-urlencoded; charset=UTF-8'
    

2. 전체적으로 처리하려면
  • $.post call에 사용하는 파라미터를 body 영역에 설정한다.
  • controller의 관련 method에서 API 전송시 사용되는 method 들을 확인 후, POST 방식 전송의 method로 변경한다.
    (해당 내용은 다른 페이지에서 동일한 메서드를 호출하는 부분이 있는지 사이드이펙트 체크 후 진행해야 한다.)
  • 사용자 정보를 전달하는 부분이기 때문에 query string보다는 body영역에 parameter를 지정하여 전송하는 것이 바람직하며, 또 우리는 그렇게 해야 한다.

Ref.] https://thermidor.tistory.com/1531 






$.post call response 403 http status on Chrome browser · LinkedNest Blog