我一直在研究AngularJS项目,该项目必须将AJAX调用发送到restfull webservice.这个web服务在另一个域上,所以我不得不在服务器上启用cors.我通过设置这些标题来做到这一点:
cresp.getHttpHeaders().putSingle("Access-Control-Allow-Origin", "http://localhost:8000");
cresp.getHttpHeaders().putSingle("Access-Control-Allow-Credentials", "true");
cresp.getHttpHeaders().putSingle("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
cresp.getHttpHeaders().putSingle("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");
Run Code Online (Sandbox Code Playgroud)
我能够将AngularJS的AJAX请求发送到后端但是当我尝试获取会话属性时我遇到了问题.我相信这是因为sessionid cookie没有发送到后端.
通过将withCredentials设置为true,我能够在jQuery中解决这个问题.
$("#login").click(function() {
$.ajax({
url: "http://localhost:8080/api/login",
data : '{"identifier" : "admin", "password" : "admin"}',
contentType : 'application/json',
type : 'POST',
xhrFields: {
withCredentials: true
},
success: function(data) {
console.log(data);
},
error: function(data) {
console.log(data);
}
})
});
$("#check").click(function() {
$.ajax({
url: "http://localhost:8080/api/ping",
method: "GET",
xhrFields: {
withCredentials: true
},
success: function(data) {
console.log(data);
}
})
});
Run Code Online (Sandbox Code Playgroud)
我面临的问题是我无法使用$ http服务在AngularJS中使用它.我试过这样的:
$http.post("http://localhost:8080/api/login", $scope.credentials, {withCredentials : true}). …Run Code Online (Sandbox Code Playgroud) 最近我一直在玩AngularJS和Java EE 6.我在Jersey上构建了一个Web服务,并在Glassfish上部署了该项目.因为我需要某种身份验证和OAuth实现或者JDBCRealm看起来有点过分,所以我决定只在用户成功登录时创建一个会话.
@POST
@Path("/login")
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})
public Response login(LoginDAO loginData, @Context HttpServletRequest req) {
req.getSession().invalidate();
loginData.setPassword(PasswordGenerator.hash(loginData.getPassword()));
User foundUser = database.login(loginData);
if(foundUser == null) {
return Response.status(Status.CONFLICT).build();
}
req.getSession(true).setAttribute("username", foundUser.getUsername());
return Response.ok().build();
}
@GET
@Path("/ping")
public Response ping(@Context HttpServletRequest req) {
if(req.getSession().getAttribute("username") == null) {
return Response.ok("no session with an username attribute has been set").build();
}
return Response.ok(req.getSession(true).getAttribute("username")).build();
}
Run Code Online (Sandbox Code Playgroud)
这似乎工作正常,如果我从Postman发布/登录或从glassfish上部署的基本jQuery网页发送,我确实得到了正确的用户名并且已经放置了会话.如果我然后发送一个GET请求到/ ping我确实得到了我登录的用户名.
我在一个需要登录的node.js网络服务器上部署了一个AngularJS应用程序.因为这个服务器在另一个端口上,它在另一个域上,我不得不经历启用cors的痛苦.我这样做是通过构建一个容器响应过滤器来设置响应头.
public class CrossOriginResourceSharingFilter implements ContainerResponseFilter {
@Override
public ContainerResponse filter(ContainerRequest creq, ContainerResponse cresp) {
cresp.getHttpHeaders().putSingle("Access-Control-Allow-Origin", "http://localhost:8000");
cresp.getHttpHeaders().putSingle("Access-Control-Allow-Credentials", "true"); …Run Code Online (Sandbox Code Playgroud) 今天我有了构建一个非常简单的Web应用程序的想法,该应用程序将由REST后端提供支持.因为我想要一个非常轻量级的服务器,所以我开始关注Jetty.因为我想尝试另一个JAX-RS实现而不是Jersey我看了RestEasy.我认为这两个很容易实现.我错了...
我导入了基本的Jetty服务器和servlet依赖项,因为我认为这是基本(仅限REST)Jetty服务器的唯一服务器要求(我总是尝试使用webapp依赖项;这给出了相同的错误).
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.0.0.RC0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.0.0.RC0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<version>9.0.0.RC0</version>
<scope>compile</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
然后我导入了基本的RestEasy依赖项.
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.1.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>3.0.1.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>async-http-servlet-3.0</artifactId>
<version>3.0.1.Final</version>
<scope>compile</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
主要方法:
public class ExampleActivator {
public static void main(String args[]) throws Exception {
Server server = new Server(8080);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
context.setContextPath("/");
ServletHolder h = new ServletHolder(new HttpServlet30Dispatcher());
h.setInitParameter("javax.ws.rs.Application", "packages.ExampleResources");
context.addServlet(h, "/*");
server.setHandler(context);
server.start();
server.join();
}
}
Run Code Online (Sandbox Code Playgroud)
ExampleResources:
public …Run Code Online (Sandbox Code Playgroud) 亲爱的stackoverflow读者,
我目前正在开发一个应用程序,它需要根据其坐标加载特定项目.示例:我想要距离坐标x或距离它15公里范围内的所有商店.
我一直在Java EE 6中构建这个Web应用程序,并使用Hibernate 3.3将数据持久化到我的Postgres数据库.经过一番研究,我发现Hibernate-Spatial是实现目标的可能性.我安装了Postgis并运行了Hibernate-Spatial.
我正在使用Hibernate 3.3,Hibernate-Spatial 1.0,PostgreSQL 9.2和postgis-jdbc 1.3.3.
我创建了一个具有位置字段的实体:
@Column(columnDefinition = "Geometry", nullable = true)
@Type(type = "org.hibernatespatial.GeometryUserType")
private Point location;
Run Code Online (Sandbox Code Playgroud)
最初我以为我可以创建一个Circle并基本上查询位于圆圈中的每个点.但这似乎比我想象的更难.我开始认为我在使用Hibernate-Spatial时做出了错误的选择.
我的问题是,是否可以通过使用Hibernate-Spatial查询特定边界内的每个点(从那里坐标x和y km).
我也对解决我的要求的其他可能性感兴趣.我一直在考虑以下可能性:
任何人都可以提供一个例子,说明我如何在Hibernate-Spatial中实现我的愿望,或者我如何以另一种方式实现我的要求.
我有大约10个Spring-MVC项目,它们部署在家里的Wildfly服务器上.这些项目已经运行了很长一段时间,并且一直是私有的.
然而,我的一些朋友已经请求访问,我愿意给他们.我打算通过使用AngularJS构建Web应用程序来实现这一目标.该应用程序将通过调用RESTful端点与Spring-MVC项目进行通信.但是,有些人可能会访问一部分服务.我希望人们注册并使用这些详细信息登录或使用OpenID登录.
这导致必须向这些项目添加身份验证和授权.可以使用Spring Security完成.但是,我不想在每个服务中实现此逻辑.
是否可以创建额外服务并让人们登录或注册此服务?让其他服务使用此服务检查身份验证状态?
哪种Spring-Security安全模型可以智能使用(OAuth 1.0,OAuth 2.0,基本身份验证等)?
有没有其他方法来实现我的要求?
给定一个打字稿文件,如:
export interface Service {
execute(): Promise<number>;
}
Run Code Online (Sandbox Code Playgroud)
如何获取所有方法及其返回类型的列表。
目前我正在尝试使用打字稿编译器 API 来实现这一点。但是我在使用“泛型”时遇到了困难。
到目前为止,我有以下代码,但我不知道如何获得 Promise 的“数字”类型。
let program = ts.createProgram(['./something.ts'], {});
let typeChecker = program.getTypeChecker();
for (const sourceFile of program.getSourceFiles()) {
sourceFile.forEachChild((node: ts.Node) => {
// I filter to make sure I've the InterfaceDeclaration.
var interfaceDeclaration: ts.InterfaceDeclaration = node;
interfaceDeclaration.forEachChild((child) => {
// I make sure it is a Method Signature.
var method : ts.MethodSignature = child;
var signature = typeChecker.getSignatureFromDeclaration(method);
var returnType = typeChecker.getReturnTypeOfSignature(signature);
var parameters = method.parameters;
console.log("name: " …Run Code Online (Sandbox Code Playgroud)