在开发过程中将AngularJS认证到Java EE (grunt server " "JBoss&

Authenticate AngularJS to Java EE during development ("grunt serve"<>"JBoss")

本文关键字:quot grunt server JBoss EE Java 过程中 开发过程 开发 AngularJS 认证      更新时间:2023-09-26

我要实现一个由Java EE容器(JBoss)提供RESTful服务支持的AngularJS应用程序。我设法自动化构建,以便angular应用程序被Grunt.js打包(包括运行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"任务将其与肝脏负载连接在一起。

但是这里我的问题开始了:由于应用程序不是从java服务器加载的,我的服务调用不成功,因为我没有经过身份验证。尝试使用$http.post()进行身份验证似乎是成功的(响应具有http状态200并包含标头set-cookie JSESSIONIDSSO=.....;Path=/),但是由于XSRF安全机制的原因,不会应用此cookie。问题似乎是,在这个设置中,我通过另一个主机(localhost:8080 vs. localhost:9000)提供的页面请求身份验证。尝试在服务器端设置http报头

    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服务器"。你有什么建议吗?最好的做法是什么?我是否需要建立基于令牌的身份验证而不是基于FORM的身份验证?这里的最佳实践是什么?

是的,我已经阅读了如何正确认证AngularJS客户端到服务器的线程,但基本的认证方法似乎像我将整个转换为基本认证,只是b/c的开发模式与"grunt服务"。你们有什么建议?

我们在开发过程中也遇到了同样的问题。只要把它添加到你的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应该被正确设置。

标准CORS请求默认情况下不发送或设置任何cookie。为了将cookie作为请求的一部分包括进来,您需要将XMLHttpRequest的.withCredentials属性设置为true:

xhr.withCredentials = true;

.withCredentials属性将在请求中包含来自远程域的任何cookie,并且它还将设置来自远程域的任何cookie。注意,这些cookie仍然遵循同源策略,因此JavaScript代码不能从document访问cookie。Cookie或响应头。它们只能被远端域控制。

http://www.html5rocks.com/en/tutorials/cors/