REST API 使用 Jersey 进行身份验证
REST API Authentication with Jersey
我正在尝试通过关注 this tutorial 对我当前的 Java Restful API(泽西岛)实施身份验证。我将所有新身份验证 类 放入 "auth" 包中。当我 运行 jquery 示例代码时,它说 "DemoBusinessRESTResourceProxy is an interface and cannot be instantiated"。所以我研究并决定将 Jersey 注释放在 DemoBusinessRESTResource 上并删除 DemoBusinessRESTResourceProxy:
@Local
@Path("access/")
@Stateless(name = "DemoBusinessRESTResource", mappedName = "ejb/DemoBusinessRESTResource")
public class DemoBusinessRESTResource {
private static final long serialVersionUID = -6663599014192066936L;
@POST
@Path("/login")
@Produces(MediaType.APPLICATION_JSON)
public Response login(@Context HttpHeaders httpHeaders,
@FormParam("username") String username,
@FormParam("password") String password) {
Authenticator authenticator = Authenticator.getInstance();
String serviceKey = httpHeaders
.getHeaderString(HTTPHeaderNames.SERVICE_KEY);
try {
String authToken = authenticator.login(serviceKey, username,
password);
JsonObjectBuilder jsonObjBuilder = Json.createObjectBuilder();
jsonObjBuilder.add("auth_token", authToken);
JsonObject jsonObj = jsonObjBuilder.build();
return getNoCacheResponseBuilder(Response.Status.OK).entity(
jsonObj.toString()).build();
} catch (final LoginException ex) {
JsonObjectBuilder jsonObjBuilder = Json.createObjectBuilder();
jsonObjBuilder.add("message",
"Problem matching service key, username and password");
JsonObject jsonObj = jsonObjBuilder.build();
return getNoCacheResponseBuilder(Response.Status.UNAUTHORIZED)
.entity(jsonObj.toString()).build();
}
}
@GET
@Path("/demo-get-method")
@Produces(MediaType.APPLICATION_JSON)
public Response demoGetMethod() {
JsonObjectBuilder jsonObjBuilder = Json.createObjectBuilder();
jsonObjBuilder.add("message", "Executed demoGetMethod");
JsonObject jsonObj = jsonObjBuilder.build();
return getNoCacheResponseBuilder(Response.Status.OK).entity(
jsonObj.toString()).build();
}
@POST
@Path("/demo-post-method")
@Produces(MediaType.APPLICATION_JSON)
public Response demoPostMethod() {
JsonObjectBuilder jsonObjBuilder = Json.createObjectBuilder();
jsonObjBuilder.add("message", "Executed demoPostMethod");
JsonObject jsonObj = jsonObjBuilder.build();
return getNoCacheResponseBuilder(Response.Status.ACCEPTED).entity(
jsonObj.toString()).build();
}
@POST
@Path("/logout")
public Response logout(@Context HttpHeaders httpHeaders) {
try {
Authenticator authenticator = Authenticator.getInstance();
String serviceKey = httpHeaders
.getHeaderString(HTTPHeaderNames.SERVICE_KEY);
String authToken = httpHeaders
.getHeaderString(HTTPHeaderNames.AUTH_TOKEN);
authenticator.logout(serviceKey, authToken);
return getNoCacheResponseBuilder(Response.Status.NO_CONTENT)
.build();
} catch (final GeneralSecurityException ex) {
return getNoCacheResponseBuilder(
Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
private Response.ResponseBuilder getNoCacheResponseBuilder(
Response.Status status) {
CacheControl cc = new CacheControl();
cc.setNoCache(true);
cc.setMaxAge(-1);
cc.setMustRevalidate(true);
return Response.status(status).cacheControl(cc);
}
}
现在,我收到此错误:
A system exception occurred during an invocation on EJB BusinessRESTResource, method: public javax.ws.rs.core.Response com.rest.auth.DemoBusinessRESTResource.login(javax.ws.rs.core.HttpHeaders,java.lang.String,java.lang.String), Caused by: java.lang.AbstractMethodError: com.sun.jersey.spi.container.ContainerRequest.getHeaderString(Ljava/lang/String;)Ljava/lang/String;
我是 Java WS 的新手,我完全迷路了。
我发现了问题。问题是 jar 版本文件。
我正在尝试通过关注 this tutorial 对我当前的 Java Restful API(泽西岛)实施身份验证。我将所有新身份验证 类 放入 "auth" 包中。当我 运行 jquery 示例代码时,它说 "DemoBusinessRESTResourceProxy is an interface and cannot be instantiated"。所以我研究并决定将 Jersey 注释放在 DemoBusinessRESTResource 上并删除 DemoBusinessRESTResourceProxy:
@Local
@Path("access/")
@Stateless(name = "DemoBusinessRESTResource", mappedName = "ejb/DemoBusinessRESTResource")
public class DemoBusinessRESTResource {
private static final long serialVersionUID = -6663599014192066936L;
@POST
@Path("/login")
@Produces(MediaType.APPLICATION_JSON)
public Response login(@Context HttpHeaders httpHeaders,
@FormParam("username") String username,
@FormParam("password") String password) {
Authenticator authenticator = Authenticator.getInstance();
String serviceKey = httpHeaders
.getHeaderString(HTTPHeaderNames.SERVICE_KEY);
try {
String authToken = authenticator.login(serviceKey, username,
password);
JsonObjectBuilder jsonObjBuilder = Json.createObjectBuilder();
jsonObjBuilder.add("auth_token", authToken);
JsonObject jsonObj = jsonObjBuilder.build();
return getNoCacheResponseBuilder(Response.Status.OK).entity(
jsonObj.toString()).build();
} catch (final LoginException ex) {
JsonObjectBuilder jsonObjBuilder = Json.createObjectBuilder();
jsonObjBuilder.add("message",
"Problem matching service key, username and password");
JsonObject jsonObj = jsonObjBuilder.build();
return getNoCacheResponseBuilder(Response.Status.UNAUTHORIZED)
.entity(jsonObj.toString()).build();
}
}
@GET
@Path("/demo-get-method")
@Produces(MediaType.APPLICATION_JSON)
public Response demoGetMethod() {
JsonObjectBuilder jsonObjBuilder = Json.createObjectBuilder();
jsonObjBuilder.add("message", "Executed demoGetMethod");
JsonObject jsonObj = jsonObjBuilder.build();
return getNoCacheResponseBuilder(Response.Status.OK).entity(
jsonObj.toString()).build();
}
@POST
@Path("/demo-post-method")
@Produces(MediaType.APPLICATION_JSON)
public Response demoPostMethod() {
JsonObjectBuilder jsonObjBuilder = Json.createObjectBuilder();
jsonObjBuilder.add("message", "Executed demoPostMethod");
JsonObject jsonObj = jsonObjBuilder.build();
return getNoCacheResponseBuilder(Response.Status.ACCEPTED).entity(
jsonObj.toString()).build();
}
@POST
@Path("/logout")
public Response logout(@Context HttpHeaders httpHeaders) {
try {
Authenticator authenticator = Authenticator.getInstance();
String serviceKey = httpHeaders
.getHeaderString(HTTPHeaderNames.SERVICE_KEY);
String authToken = httpHeaders
.getHeaderString(HTTPHeaderNames.AUTH_TOKEN);
authenticator.logout(serviceKey, authToken);
return getNoCacheResponseBuilder(Response.Status.NO_CONTENT)
.build();
} catch (final GeneralSecurityException ex) {
return getNoCacheResponseBuilder(
Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
private Response.ResponseBuilder getNoCacheResponseBuilder(
Response.Status status) {
CacheControl cc = new CacheControl();
cc.setNoCache(true);
cc.setMaxAge(-1);
cc.setMustRevalidate(true);
return Response.status(status).cacheControl(cc);
}
}
现在,我收到此错误:
A system exception occurred during an invocation on EJB BusinessRESTResource, method: public javax.ws.rs.core.Response com.rest.auth.DemoBusinessRESTResource.login(javax.ws.rs.core.HttpHeaders,java.lang.String,java.lang.String), Caused by: java.lang.AbstractMethodError: com.sun.jersey.spi.container.ContainerRequest.getHeaderString(Ljava/lang/String;)Ljava/lang/String;
我是 Java WS 的新手,我完全迷路了。
我发现了问题。问题是 jar 版本文件。