on 2020 Mar 12 7:45 AM
Hi,
I have created the MystoreCorsFilter.java file in MystoreAddon. and configured the created filter in mystoreaddon-web-spring.xml file like below.
<bean id="mystoreCorsFilter">
<property name="configurationService" ref="configurationService"></property>
</bean>
added the above created filter in Storefront filter list.
<bean id="mystoreaddonFilterListMergeDirective"
depends-on="defaultStorefrontTenantDefaultFilterChainList" parent="listMergeDirective">
<property name="add" ref="mystoreCorsFilter"/>
</bean>
MystoreCorsFilter.java file:
public class MystoreCorsFilter extends OncePerRequestFilter{
private static final Logger logger = Logger.getLogger(MystoreCorsFilter.class.getName());
@Resource
private ConfigurationService configurationService;
@Override
protected void doFilterInternal(HttpServletRequest req, HttpServletResponse resp, FilterChain chain)
throws ServletException, IOException {
logger.info("MystoreCorsFilter is initiated....................");
HttpServletResponse response = (HttpServletResponse) resp;
response.setHeader("Access-Control-Allow-Origin", "https://localhost:9002");
response.setHeader("Access-Control-Allow-Credentials", "false");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
response.setHeader("Access-Control-Allow-Headers",
"Origin, Content-Type, X-Requested-With, accept, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization");
response.setHeader("Access-Control-Expose-Headers",
"Origin, Access-Control-Request-Method, Access-Control-Allow-Origin, Access-Control-Allow-Credentials");
response.setHeader("Access-Control-Max-Age", "4000");
chain.doFilter(req, resp);
}
public ConfigurationService getConfigurationService() {
return configurationService;
}
public void setConfigurationService(ConfigurationService configurationService) {
this.configurationService = configurationService;
}
}
To test the above code by using ajax call. But its working only in IE browser, not working in Chrome and Mozilla. Its throwing following error:
Access to XMLHttpRequest at 'https://localhost:9002/mystoreintegration/setup' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
CROS.html:27 POST https://localhost:9002/mystoreintegration/setup net::ERR_FAILED
If I miss anything Please help me on this. If sample code provided will be appreciated.
Thanks in advance.
Request clarification before answering.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.