1
votes

I have written a servlet Filter in which I am trying to get the value of custom header=samlRequest, From rest client/postman chrome plugin i am getting the value of samlRequest, but using ajax call i have provide samlRequest key and its value but in java i am getting the only key samlRequest in "Access-Control-Request-Headers", how to get value of samlRequest.

$.ajax({
             url: "http://indlin500.corp.test.com:31000/test/Portal/index.html",
             type: "GET",
             headers: { 'samlRequest': 'eJzVWVlz4roSfs6/oJhHJvECBkMRTsk2+2ow68stY8u2wBuWjQ2//srsyUlmJufOramTSjm41f '},
             beforeSend: function(xhr){xhr.setRequestHeader('samlRequest', 'eJzVWVlz4roSfs6/oJhHJvECBkMRTsk2+2ow68stY8u2wBuWjQ2//srsyUlmJufOramTSjm41f ');},
             success: function() { alert('Success!' + authHeader); }
    });

FirewalledRequest[ weblogic.servlet.internal.ServletRequestImpl@4f839843[ OPTIONS /testsOSS/Portal/index.html HTTP/1.1 Connection: keep-alive Access-Control-Request-Method: GET Origin: http://10.19.121.17:7001 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36 Access-Control-Request-Headers: authorization, samlrequest, x-partnerkey Accept: / DNT: 1 Referer: http://10.19.121.17:7001/authentication-uxf-login-0.0.1-SNAPSHOT/customLoginPage.html Accept-Encoding: gzip, deflate, sdch Accept-Language: en-US,en;q=0.8 ]]

Using chrome rest client plugin I am getting samlRequest in the header with value i have provided.

FirewalledRequest[ weblogic.servlet.internal.ServletRequestImpl@24d746a[ GET /testOSS/Portal/index.html HTTP/1.1 Connection: keep-alive Cache-Control: no-cache samlRequest: eJzVWVlz4roSfs6/oJhHJvECBkMRTsk2+2ow68stY8u2wBuWjQ2//srsyUlmJufOramTSjm41f User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36 Postman-Token: ce7765d0-94b5-a1e1-d6ba-2abada3dfdd6 Accept: / DNT: 1 Accept-Encoding: gzip, deflate, sdch Accept-Language: en-US,en;q=0.8

]]

1
You need to cast your request as HttpServletRequest and you will have getHeader methods in it. docs.oracle.com/javaee/7/api/javax/servlet/http/…neomega

1 Answers

1
votes

When you make a cross-origin request from browser, the browser makes a preflight request before the actual request. It is an OPTIONS request, unlike the intended GET request.

Try adding a CORS filter before the actual filter which will reply OK for any OPTIONS request. A sample code can be found here :

https://amodernstory.com/2014/12/27/using-cors-headers-with-java-example/

by the way, you don't need to set header in request twice. You can remove the beforeSend block from the ajax request.