我正在使用基于我的客户表的HTTP身份验证。用户通过身份验证后,将调用restful风格的the服务。但是如何在But服务中访问HTTP-Authentication (HttpRequest的头数据)?我的代码如下所示:
@GET
@Path("{id}")
@Produces({"application/xml"})
public ObjectList read(@PathParam("id") Integer id) {
... //how to get here the HTTP-Username and Password?
}发布于 2012-01-25 23:31:59
为了获得上下文及其角色,在类体或方法输入参数中注入@ Principal SecurityContext。
import javax.ws.rs.core;
//
public ObjectList read(
@PathParam("id") Integer id,
@Context SecurityContext sc) {
String principalUserName = sc.getUserPrincipal().getName();
if (sc.isUserInRole("MyRole")) {
return new MyRoleResource();
} else {
return new MyDefaultRoleResource();
}
}发布于 2012-01-25 23:25:28
向您的方法添加更多参数,如下所示:
import javax.ws.rs.HeaderParam;
// ...
public ObjectList read(
@PathParam("id") Integer id,
@HeaderParam("user-agent") String userAgent,
@HeaderParam("X-auth-token") String authToken) ...https://stackoverflow.com/questions/9005124
复制相似问题