我正在构建一个使用RESTful API(Jersey)的AngularJS Web应用程序.
在服务器端,我使用的是Java Application Server(详见Glassfish 4).
我的设置如下:
/api/public/*
对于公共不受限制的访问(例如/ api/public/users/signup注册为新用户).允许任何用户访问此区域.无需登录/api/private/*
限制访问(例如/ api/private/account/{id}以检索某些帐户特定数据)在web.xml
<security-constraint>
<web-resource-collection>
<web-resource-name>PRIVATE REST API</web-resource-name>
<url-pattern>/private/*</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>
<description>Have to be a USER</description>
<role-name>USERS</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>userauth</realm-name>
</login-config>
<security-role>
<description/>
<role-name>USERS</role-name>
</security-role>
Run Code Online (Sandbox Code Playgroud)
此设置适用于我,但仅限于我在浏览器中手动调用URL.如果我未经过身份验证并处理安全区域,则浏览器会要求输入用户名和密码.如果提供的凭证有效,我可以从那里访问限制区域:好.
但是我如何使用AngularJS呢?要登录,有一个POST API调用:/api/public/users/login
您必须从表单(用户名和密码)提供凭据.
客户端代码是:
ctrls.controller('LoginController', function($scope, $http, $log, $location, UserService, RemoteServerHelperService) {
$scope.loginDataWrong = undefined;
$scope.login = function(credentials) {
$http.post(RemoteServerHelperService.buildURL('/public/users/login'), …
Run Code Online (Sandbox Code Playgroud) 我有一个包含多个模块的项目.
其中两个生成war文件.
一个war文件是一个REST应用程序,它提供了一些资源.
Other war文件是一个与REST后端通信的Angular JS Web应用程序(仅限静态内容).
出于演示目的,我想非常轻松地部署两个war文件 mvn jetty:run
出于开发目的,我想从我的IDE(例如Eclipse Servers View)部署它们.
当我通过启动服务器并将war文件复制到部署文件夹手动在单个Jetty服务器(v9.0.7.v20131107)上进行部署时,一切都会出现.
当mvn jetty:run
两个war文件启动jetty时,部署了,但不知何时REST资源没有部署.
我正在使用Jersey 2.手动部署时,我收到一条日志消息
Nov 14, 2013 10:44:37 PM org.glassfish.jersey.server.ApplicationHandler initialize
INFO: Initiating Jersey application, version Jersey: 2.4 2013-10-24 18:25:49...
但是,在开始时未显示此消息mvn jetty:run
.所以我认为泽西岛没有踢球.
对于依赖注入我使用spring.
这是/pom.xml中的父pom,带有jetty-maven-plugin配置
<project ...>
...
<build>
<plugins>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.0.7.v20131107</version>
<configuration>
<scanIntervalSeconds>2</scanIntervalSeconds>
<contextHandlers>
<contextHandler implementation="org.eclipse.jetty.webapp.WebAppContext">
<war>module1/target/module1-${project-version}.war</war>
<contextPath>/module1</contextPath>
</contextHandler>
<contextHandler implementation="org.eclipse.jetty.webapp.WebAppContext">
<war>module2/target/module2-${project.version}.war</war>
<contextPath>/module2/contextPath>
</contextHandler>
</contextHandlers>
</configuration>
</plugins>
</build>
...
</project>
Run Code Online (Sandbox Code Playgroud)
这是module1(REST模块)的pom
<parent>
...
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>module1</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId> …
Run Code Online (Sandbox Code Playgroud)