【发布时间】:2018-02-01 08:33:45
【问题描述】:
我正在使用 Angular 4 和 JAX-RS 来提供休息服务。 Angular 部署在 tomcat 服务器中,JAX-RS REST 服务部署在 Websphere 8.5 App 服务器中。我正在尝试使用基本身份验证来保护其余服务。这是用于解决 CORS 问题和基本身份验证的 web.xml 部分
<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
<init-param>
<param-name>cors.allowGenericHttpRequests</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.allowOrigin</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowSubdomains</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.supportedMethods</param-name>
<param-value>GET, POST, PUT, DELETE, OPTIONS, HEAD </param-value>
</init-param>
<init-param>
<param-name>cors.supportedHeaders</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.supportsCredentials</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.maxAge</param-name>
<param-value>-1</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<security-role>
<description>default</description>
<role-name>default</role-name>
</security-role>
<security-constraint>
<display-name>DefaultConstraints</display-name>
<web-resource-collection>
<web-resource-name>all resources</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
<http-method>HEAD</http-method>
<http-method>TRACE</http-method>
<http-method>DELETE</http-method>
<http-method>OPTIONS</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>default</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
当 Angular 应用程序在 Java web 中使用上述配置从 chrome 浏览器调用 Rest 服务时,它会抛出错误,并在浏览器控制台中看到以下内容。
加载资源失败:服务器响应状态为 401(未授权)
无法加载http://localhost:9091/xxxxxxx/rest/v1/technologies:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'http://localhost:8080' 不允许访问。响应的 HTTP 状态代码为 401。
如果同一个 Angular 应用程序从 IE 调用其余服务,浏览器会弹出一个凭据弹出窗口,并且在成功登录后,Angular 能够获取并显示记录。
它适用于 IE,不适用于 chrome。
如果我删除安全约束、基本身份验证、角色并仅放置 CORS 过滤器,Angular 就能够从 chrome 和 IE 中的其余服务中获取记录。如果在安全约束时仅在 chrome 中失败。
请帮我解决。如果有任何错误,请告诉我。
谢谢, 哈里。
【问题讨论】:
标签: angular jakarta-ee jax-rs basic-authentication