यह सब आपके द्वारा उपयोग किए जा रहे जेएक्स-आरएस कार्यान्वयन पर निर्भर करता है। मैं एम्बेडेड Jetty पर Jersey का उपयोग कर रहा हूं।
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());
How to Configure Security with Embedded Jetty
देखें एक बार जब आप HttpServletRequest
में Principal
है, तो आप JAX-आरएस अनुरोध के संदर्भ में इन इंजेक्षन कर सकते हैं।
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
}
}