Bu, kullandığınız JAX-RS uygulamasına bağlıdır. Embed Jetty üzerinde Jersey kullanıyorum. Eğer HttpServletRequest
yılında Principal
sahip olduktan sonra
SecurityHandler sh = new SecurityHandler();
// the UserRealm is the collection of users, and a mechanism to determine if
// provided credentials are valid
sh.setUserRealm(new MyUserRealm());
// the Authenticator is a strategy for extracting authentication credentials
// from the request. BasicAuthenticator uses HTTP Basic Auth
sh.setAuthenticator(new BasicAuthenticator());
JAX-RS istek bağlamı içinde bu enjekte edebilir,
How to Configure Security with Embedded Jetty bakınız.
public abstract class AbstractResource {
private Principal principal;
@Context
public void setSecurityContext(SecurityContext context) {
principal = context.getUserPrincipal();
}
protected Principal getPrincipal() {
return principal;
}
}
@Path("/some/path")
public class MyResource extends AbstractResource {
@GET
public Object get() {
Principal user = this.getPrincipal();
// etc
}
}