2019년 5월 12일 일요일

톰캣 서버에 크로스 도메인을 위한 설정 테스트(CORS)

html 파일


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>크로스 도메인 테스트</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
function crossdomaintest() {
    var data = {
        id : 'C0001',
        name : '모델1',
        disc : 'XXXX 건물 모델',
        date : '2019-05-05'
    };
    $.ajax({
        url : 'http://localhost:8080/gistools/tmp/crossdomaintest.ajax',
        type : 'POST',
        data : JSON.stringify(data),
        dataType : 'json',
        contentType : 'application/json; charset=UTF-8',
        success : function(result) {
            alert(result.message);
        },
        error : function(request,status,error) {
            alert("처리에 실패하였습니다.\ncode:"+request.status+"\n"+"error:"+error);
        },
        complete : function() {
        }
    });
}

$(document).ready(function() {
    $('#button1').click(function() {
        crossdomaintest();
    });
});

</script>
</head>
<body>
<pre>
<a href="#" id="button1">타 도메인에 ajax 요청</a>
</pre>
</body>
</html>


java Controller 파일


package egovframework.pst.tmp.web;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class CrossDomainTestController {

    private static final Logger LOGGER = LoggerFactory.getLogger(CrossDomainTestController.class);

    @RequestMapping(value="/tmp/crossdomaintest.ajax", method=RequestMethod.POST)
    public @ResponseBody Map crossdomaintest(@RequestBody Map param) throws Exception {
        LOGGER.debug("java 메시지 : 정상적으로 요청받음");
        Map result = new HashMap();
        result.put("message", "java 메시지 : 정상적으로 반환");
        return result;
    }

}


tomcat_home/conf/web.xml 파일 또는 WEB-INF/web.xml 파일


<web-app ....>
    <filter>
        <filter-name>CorsFilter</filter-name>
        <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
        <init-param>
            <param-name>cors.allowed.origins</param-name>
            <param-value>*</param-value>
        </init-param>
        <init-param>
            <param-name>cors.allowed.methods</param-name>
            <param-value>GET,POST,HEAD,OPTIONS,PUT,DELETE</param-value>
        </init-param>
        <init-param>
            <param-name>cors.allowed.headers</param-name>
            <param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
        </init-param>
        <init-param>
            <param-name>cors.exposed.headers</param-name>
            <param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
        </init-param>
        <init-param>
            <param-name>cors.support.credentials</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>cors.preflight.maxage</param-name>
            <param-value>10</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CorsFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>