public interface SecurityContext {
public Principal getUserPrincipal();
public boolean isUserInRole(String role);
public boolean isSecure();
public String getAuthenticationScheme();
}
public interface SecurityContext {
public Principal getUserPrincipal();
public boolean isUserInRole(String role);
public boolean isSecure();
public String getAuthenticationScheme();
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow
@Path("test")
public class SecurityContextResource {
@Context
SecurityContext securityContext;
@GET
@Produces("text/plain")
public String get() {
if (!securityContext.isUserInRole("admin")) {
throw new WebApplicationException(Response.serverError().status(HttpResponseCodes.SC_UNAUTHORIZED)
.entity("User " + securityContext.getUserPrincipal().getName() + " is not authorized").build());
}
return "Good user " + securityContext.getUserPrincipal().getName();
}
}
@Path("test")
public class SecurityContextResource {
@Context
SecurityContext securityContext;
@GET
@Produces("text/plain")
public String get() {
if (!securityContext.isUserInRole("admin")) {
throw new WebApplicationException(Response.serverError().status(HttpResponseCodes.SC_UNAUTHORIZED)
.entity("User " + securityContext.getUserPrincipal().getName() + " is not authorized").build());
}
return "Good user " + securityContext.getUserPrincipal().getName();
}
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow