"JBoss")-6ren"> "JBoss")-我将要实现一个由 Java EE 容器 (JBoss) 提供的 RESTful 服务支持的 AngularJS 应用程序。我设法使构建自动化,以便使用 Grunt.js 打包 Angular 应用程序-6ren">
gpt4 book ai didi

java - 在开发期间向 Java EE 验证 AngularJS ("grunt serve"< >"JBoss")

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:24:22 25 4
gpt4 key购买 nike

我将要实现一个由 Java EE 容器 (JBoss) 提供的 RESTful 服务支持的 AngularJS 应用程序。我设法使构建自动化,以便使用 Grunt.js 打包 Angular 应用程序(包括运行 Compass、usecss 等),然后将其包含到使用 maven 构建的 war 文件中。

对应用程序本身和 REST 服务的访问受到使用 web.xml 配置的基于表单的身份验证的保护,例如:

<security-constraint>
<web-resource-collection>
<web-resource-name>AngularApplication</web-resource-name>
<url-pattern>/index.html</url-pattern>
<url-pattern>/services/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>HEAD</http-method>
<http-method>PUT</http-method>
<http-method>OPTIONS</http-method>
<http-method>TRACE</http-method>
<http-method>DELETE</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>User</role-name>
</auth-constraint>
</security-constraint>
<security-role>
<description>User has to be authenticated.</description>
<role-name>User</role-name>
</security-role>

<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/error.jsp</form-error-page>
</form-login-config>
</login-config>

当我从部署的应用程序中运行应用程序时,一切正常 - 在使用登录表单进行身份验证后, Angular 应用程序加载并能够访问服务。

但对于开发来说,这有点太重了,因为查看应用程序更改(重建和重新部署所有内容)的循环会花费很长时间。所以对于服务器端,我可以使用 java 类的热交换,很好。对于 Angular ,我认为我可以使用通过“grunt serve”任务与 livereload 连接在一起的本地服务应用程序。

但我的问题从这里开始:由于应用程序不是从 Java 服务器加载的,所以我的服务调用没有成功,因为我没有通过身份验证。尝试使用 $http.post() 进行身份验证似乎是成功的(响应具有 HTTP-Status 200 并包含 header set-cookie JSESSIONIDSSO=.....; Path=/),但未应用此 cookie - 如果由于 XSRF 安全机制,我做对了。问题似乎是,在此设置中,我通过另一台主机提供的页面请求身份验证(本地主机:8080 与本地主机:9000)。我尝试在服务器端设置 HTTP header

    servletResponse.setHeader("Access-Control-Allow-Origin", "*");
servletResponse.setHeader("Access-Control-Allow-Credentials", "true");
servletResponse.setHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");

以及试图说服 Angular 接受电话,例如。

$httpProvider.defaults.useXDomain = true;

delete $httpProvider.defaults.headers.common['X-Requested-With'];

但这并没有帮助我继续。

此外,尝试从 cookie 切换到 URL 参数

<session-config>
<tracking-mode>URL</tracking-mode>
</session-config>

没有帮助。这使我无法登录并显示一条奇怪的错误消息:

"HTTP Status 408 - The time allowed for the login process has been exceeded. If you wish to continue you must either click back twice and re-click the link you requested or close and re-open your browser" 

所以我被困在这里了。我正在寻找一种易于处理的解决方案,以便在开发过程中使用“grunt 服务器”。您有什么建议是这里的最佳做法吗?我是否需要建立基于 token 的身份验证而不是基于表单的身份验证?这里的最佳做法是什么?

是的,我已经阅读了 How to proper authenticate an AngularJS client to the server 的帖子但是那种基本的身份验证方法似乎我会把整个这个切换到基本身份验证,只是开发模式的 b/c 和“grunt serve”。你们有什么建议?

最佳答案

我们在开发中遇到了完全相同的问题。只需将其添加到您的 Angular 应用程序中:

$httpProvider.defaults.withCredentials = true;

并且在您的服务器中,您必须指定来源而不是通配符,因为 allow-credentials 设置为 true。

servletResponse.setHeader("Access-Control-Allow-Origin", "http://localhost:9000");
servletResponse.setHeader("Access-Control-Allow-Credentials", "true");

如果您在端口 9000 上运行 grunt。

有了这个,应该正确设置 JSESSIONID。

关于java - 在开发期间向 Java EE 验证 AngularJS ("grunt serve"< >"JBoss"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24085365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com