我正在使用基于我的客户表的 HTTP 身份验证。用户通过身份验证后,将调用静态 Web 服务。但是我如何在 Web 服务中访问 HTTP 身份验证(HttpRequest 的标头数据)?
我的代码如下所示:
@GET
@Path("{id}")
@Produces({"application/xml"})
public ObjectList read(@PathParam("id") Integer id) {
... //how to get here the HTTP-Username and Password?
}
为了得到Principal
及其作用,注入@ContextSecurityContext
在类主体或方法输入参数中。
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();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)