I am giving simple steps to solve Phonegap access origin problem with angularjs
You have to create a filter at your java server side to allow cross domain requests (If your angularjs requests resources from any other java server):
A. Your java filter class will be look like as:
package com.abc.security.corsfilter.util;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class CORSFilter implements Filter {
public void destroy() {
}
public static String VALID_METHODS = "DELETE, HEAD, GET, OPTIONS, POST, PUT";
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException,
IOException {
HttpServletRequest httpReq = (HttpServletRequest) req;
HttpServletResponse httpResp = (HttpServletResponse) resp;
// No Origin header present means this is not a cross-domain request
String origin = httpReq.getHeader("Origin");
if (origin == null) {
// Return standard response if OPTIONS request w/o Origin header
if ("OPTIONS".equalsIgnoreCase(httpReq.getMethod())) {
httpResp.setHeader("Allow", VALID_METHODS);
httpResp.setStatus(200);
return;
}
} else {
// This is a cross-domain request, add headers allowing access
httpResp.setHeader("Access-Control-Allow-Origin", origin);
httpResp.setHeader("Access-Control-Allow-Methods", VALID_METHODS);
String headers = httpReq.getHeader("Access-Control-Request-Headers");
if (headers != null)
httpResp.setHeader("Access-Control-Allow-Headers", headers);
// Allow caching cross-domain permission
httpResp.setHeader("Access-Control-Max-Age", "3600");
}
// Pass request down the chain, except for OPTIONS
if (!"OPTIONS".equalsIgnoreCase(httpReq.getMethod())) {
chain.doFilter(req, resp);
}
}
public void init(FilterConfig config) throws ServletException {
}
}
B. Make entry of this filter in web.xml. Remember entry of this filter should be first, if you have any other filters entry in your web.xml
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>com.certain.security.corsfilter.util.CORSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
*******Now we are going to make changes in code of your phonegap application.******
Your phonegap activity class should look like this:
package com.example.hello;
import android.os.Bundle;
import org.apache.cordova.*;
import android.webkit.CookieManager;
public class HelloWorld extends CordovaActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.init();
CookieManager.setAcceptFileSchemeCookies(true); // this must be in this class
// Set by <content src="index.html" /> in config.xml
super.loadUrl(Config.getStartUrl());
//super.loadUrl("file:///index.html");
}
}
You have to create a filter at your java server side to allow cross domain requests (If your angularjs requests resources from any other java server):
A. Your java filter class will be look like as:
package com.abc.security.corsfilter.util;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class CORSFilter implements Filter {
public void destroy() {
}
public static String VALID_METHODS = "DELETE, HEAD, GET, OPTIONS, POST, PUT";
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException,
IOException {
HttpServletRequest httpReq = (HttpServletRequest) req;
HttpServletResponse httpResp = (HttpServletResponse) resp;
// No Origin header present means this is not a cross-domain request
String origin = httpReq.getHeader("Origin");
if (origin == null) {
// Return standard response if OPTIONS request w/o Origin header
if ("OPTIONS".equalsIgnoreCase(httpReq.getMethod())) {
httpResp.setHeader("Allow", VALID_METHODS);
httpResp.setStatus(200);
return;
}
} else {
// This is a cross-domain request, add headers allowing access
httpResp.setHeader("Access-Control-Allow-Origin", origin);
httpResp.setHeader("Access-Control-Allow-Methods", VALID_METHODS);
String headers = httpReq.getHeader("Access-Control-Request-Headers");
if (headers != null)
httpResp.setHeader("Access-Control-Allow-Headers", headers);
// Allow caching cross-domain permission
httpResp.setHeader("Access-Control-Max-Age", "3600");
}
// Pass request down the chain, except for OPTIONS
if (!"OPTIONS".equalsIgnoreCase(httpReq.getMethod())) {
chain.doFilter(req, resp);
}
}
public void init(FilterConfig config) throws ServletException {
}
}
B. Make entry of this filter in web.xml. Remember entry of this filter should be first, if you have any other filters entry in your web.xml
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>com.certain.security.corsfilter.util.CORSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
*******Now we are going to make changes in code of your phonegap application.******
Your phonegap activity class should look like this:
package com.example.hello;
import android.os.Bundle;
import org.apache.cordova.*;
import android.webkit.CookieManager;
public class HelloWorld extends CordovaActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.init();
CookieManager.setAcceptFileSchemeCookies(true); // this must be in this class
// Set by <content src="index.html" /> in config.xml
super.loadUrl(Config.getStartUrl());
//super.loadUrl("file:///index.html");
}
}
Changes in /usr/local/src/node-v0.10.29/hello/config.xml
use <access origin="*" />
No comments:
Post a Comment