2016-02-27 105 views
0

我有一個Web服務,如:添加訪問控制允許來源以Web服務

@Controller 
@RequestMapping("/") 
public class ApplicationController { 

    @RequestMapping(value="/Changes", method = RequestMethod.GET) 
    public String inspect(ModelMap model) { 
     model.addAttribute("msg", "example"); 

     return "index"; 
    } 
} 
在鏈接

"localhost:8081/ChangesPDF/Changes?..."

我試圖讓這個web服務,通​​過露天的答覆中說在鏈接:"localhost:8080/share"。我有不同的端口,現在(當我有相同的端口,這個工作好),所以,用不同的端口,我得到在露天的錯誤:

跨來源請求阻止:同源策略不允許讀 遠程資源位於http://localhost:8081/ChangesPDF/Changes?(原因:缺少標題CORS'Access-Control-Allow-Origin')。

如何添加此標頭?

回答

1

您應該添加一個篩選器,將「Access-Control-Allow-Origin」設置爲接受域「localhost:8081」(全部爲*)。

最可能的是,你會發現你的答案在這裏cores-filter-not-working

更多的解釋:

創建一個過濾器類第一

public class CorsFilter implements Filter { 

    private static final Logger log = Logger.getAnonymousLogger(); 

    @Override 
    public void init(FilterConfig filterConfig) throws ServletException {} 

    @Override 
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { 
     HttpServletResponse response = (HttpServletResponse) servletResponse; 
     HttpServletRequest request = (HttpServletRequest) servletRequest; 

     // can be moved to properties 
     String[] allowDomain = {"localhost:8080","localhost:8081"};    


     String originHeader = request.getHeader("host"); 

     for(String domian : allowDomain){ 

      if(originHeader.endsWith(domian)) 

      response.setHeader("Access-Control-Allow-Origin", originHeader); 
      break; 
     } 
     filterChain.doFilter(servletRequest, servletResponse); 

    } 

    @Override 
    public void destroy() {} 
} 

添加映射到web.xml類

<filter> 
    <filter-name>cors</filter-name> 
    <filter-class>full name of your filter class here</filter-class> 
</filter> 

<filter-mapping> 
    <filter-name>cors</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

你應該正確定義w中的URL pattren eb.xml配置根據您的requirnment

+0

但在我的情況下,我該如何做到這一點?如果我使'response.setHeader'沒有生效,我不會返回'HttpServletResponse response'。 – PRVS

+0

編輯我的答案,包括如何設置過濾器 – PyThon

+0

url-pattern是哪個url? – PRVS

1

如果您在使用Spring 4.2+,你可以使用@CrossOrigin註釋:

@Controller 
@RequestMapping("/") 
public class ApplicationController { 

    @CrossOrigin 
    @RequestMapping(value="/Changes", method = RequestMethod.GET) 
    public String inspect(ModelMap model) { 
     model.addAttribute("msg", "example"); 

     return "index"; 
    } 
} 

否則,你需要在你的Spring應用程序註冊CORS過濾器。類似this

0

我認爲你的代碼可能需要使用Spring的@CrossOrigin註釋,例如

import org.springframework.web.bind.annotation.CrossOrigin; 

.... 

@Controller 
@RequestMapping("/") 
public class ApplicationController { 

    @CrossOrigin 
    @RequestMapping(value="/Changes", method = RequestMethod.GET) 
    public String inspect(ModelMap model) { 
     model.addAttribute("msg", "example"); 

     return "index"; 
    } 
} 

這將使所有起源,這可能會或可能不適合你的需求是不夠的。

僅供參考,我在this article找到了上述說明,並在Spring blog here中提到過。

希望這會有所幫助!

+0

我嘗試這個,但仍然是相同的錯誤。 – PRVS

+0

如果在整個控制器類上使用「@ CrossOrigin」註釋而不是單個方法,會發生什麼? – Castaglia

+0

同樣的錯誤:(我不明白 – PRVS

相關問題