ng build --prod允许缺少原点(404禁止)

时间:2017-08-10 11:54:27

标签: angular tomcat cors

我在我的应用程序之外调用REST APIS,我总是

  

允许来源'标头出现在请求的资源上(403 Forbidden)。

我配置了我的tomcat服务器以允许来源。

  

ng serve --proxy-config proxy.conf.json

它工作正常,但在

之后
  

ng build --prod

我遇到了这个问题。那么为什么它使用ng服务而不是ng build?这在我的服务器端不是问题,否则它不适用于ng服务器或http请求等。我可以在这做什么?

的web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0" metadata-complete="true">

  <display-name>Apache TomEE</display-name>

  <servlet>
    <servlet-name>LoaderServlet</servlet-name>
    <servlet-class>org.apache.tomee.loader.LoaderServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet>
    <servlet-name>ServerServlet</servlet-name>
    <servlet-class>org.apache.openejb.server.httpd.ServerServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServerServlet</servlet-name>
    <url-pattern>/ejb/*</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>InstallerServlet</servlet-name>
    <servlet-class>org.apache.tomee.webapp.installer.InstallerServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>InstallerServlet</servlet-name>
    <url-pattern>/installer</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

  <security-constraint>
    <web-resource-collection>
      <web-resource-name>ServerServlet</web-resource-name>
      <url-pattern>/ejb/*</url-pattern>
    </web-resource-collection>
<!--<auth-constraint>
    <role-name>developers</role-name>
</auth-constraint>-->
  </security-constraint>
  <security-constraint>
    <web-resource-collection>
      <web-resource-name>Static Content</web-resource-name>
      <url-pattern>/webjars/*</url-pattern>
    </web-resource-collection>
  </security-constraint>
  <security-constraint>
    <web-resource-collection>
      <web-resource-name>Application</web-resource-name>
      <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
      <role-name>tomee-admin</role-name>
    </auth-constraint>
  </security-constraint>

 <login-config>
    <auth-method>BASIC</auth-method>
  </login-config>
  <security-role>
    <role-name>tomee-admin</role-name>
  </security-role>

</web-app>

1 个答案:

答案 0 :(得分:0)

我的web.xml文件。

    <?xml version='1.0' encoding='UTF-8' ?> 
    <!-- was: <?xml version="1.0" encoding="ISO-8859-1"?> -->
    <web-app 
       xmlns="http://java.sun.com/xml/ns/javaee" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
       version="3.0">
        <servlet>
            <servlet-name>TestStatusServlet</servlet-name>
            <servlet-class>com.qnium.yuuwallet.TestStatusServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>TestStatusServlet</servlet-name>
            <url-pattern>/test-status/*</url-pattern>
        </servlet-mapping>

    <!-- Enable for Jetty -->
        <filter>
           <filter-name>cors-filter-jetty</filter-name>
           <filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
           <init-param>
               <param-name>allowedOrigins</param-name>
               <param-value>*</param-value>
           </init-param>
           <init-param>
               <param-name>allowedMethods</param-name>
               <param-value>GET,POST,HEAD</param-value>
           </init-param>
           <init-param>
               <param-name>allowedHeaders</param-name>
               <param-value>X-Requested-With,Content-Type,Accept,Origin</param-value>
           </init-param>
       </filter>
       <filter-mapping>
           <filter-name>cors-filter-jetty</filter-name>
           <url-pattern>/*</url-pattern>
       </filter-mapping>

    </web-app>