我在一个从javax.ws.rs.core.Application扩展的类中有一个现有的代码
...
Context childContext = component.getContext().createChildContext();
JaxRsApplication application = new JaxRsApplication(childContext);
application.add(this);
application.setStatusService(new ErrorStatusService());
childContext.getAttributes().put("My Server", this);
...
ChallengeAuthenticator challengeGuard = new ChallengeAuthenticator(null, ChallengeScheme.HTTP_BASIC, "REST API Realm");
//Create in-memory users with roles
MemoryRealm realm = new MemoryRealm();
User user = new User("user", "user");
realm.getUsers().add(user);
realm.map(user, Role.get(null, "user"));
User owner = new User("admin", "admin");
realm.getUsers().add(owner);
realm.map(owner, Role.get(null, "admin"));
//Attach verifier to check authentication and enroler to determine roles
challengeGuard.setVerifier(realm.getVerifier());
challengeGuard.setEnroler(realm.getEnroler());
challengeGuard.setNext(application);
// Attach the application with HTTP basic authentication security
component.getDefaultHost().attach(challengeGuard);我的代码中没有web.xml。我想在我的代码中添加授权。这是:https://restlet.com/technical-resources/restlet-framework/guide/2.3/core/security/authorization不适用于我,因为我没有restlet资源。
如何在代码中实现jax的授权?
编辑1:现有代码使用restlet扩展:https://restlet.com/technical-resources/restlet-framework/guide/2.2/extensions/jaxrs
我在我的jax-rs资源类中尝试过这样的方法:
@GET
@Path("/")
public String getStatus() {
if (!securityContext.isUserInRole("admin")) {
throw new WebApplicationException(Response.Status.FORBIDDEN);
}
...
}但是,它抛出了403,甚至我也向admin用户登录。
编辑2:
当我在这里检查:https://restlet.com/technical-resources/restlet-framework/guide/2.2/extensions/jaxrs时,有一段代码:
this.setRoleChecker(...); // if needed这可能解决我的问题,但我不知道如何设置一个角色检查器。
PS: --我使用了Jerse1.9和RESTlet2.2.3。
发布于 2016-08-20 16:18:21
我可以让它这样运作:
申请课程:
...
application.setRoles(getRoles(application));
...
public static List<Role> getRoles(JaxRsApplication application) {
List<Role> roles = new ArrayList<>();
for (AuthorizationRoleEnum authorizationRole : AuthorizationRoleEnum.values()) {
roles.add(new Role(application, authorizationRole.toString()));
}
return roles;
}
...授权枚举:
public enum AuthorizationRoleEnum {
USER("user"),
ADMIN("admin");
private final String value;
AuthorizationRoleEnum(String value) {
this.value = value;
}
@Override
public String toString() {
return value;
}
}在我的资源课上:
...
@Context
SecurityContext securityContext;
...
allowOnlyAdmin(securityContext);
...
public void allowOnlyAdmin(SecurityContext securityContext) {
if (securityContext.getAuthenticationScheme() != null
&& !securityContext.isUserInRole(AuthorizationRoleEnum.ADMIN.toString())) {
throw new WebApplicationException(Response.status(Response.Status.FORBIDDEN)
.entity("User does not have required " + AuthorizationRoleEnum.ADMIN + " role!").build());
}
}
...发布于 2016-08-18 17:46:18
这并不是很清楚(至少对我来说:-)你想要达到什么目的。如果您有一个类是javax.ws.rs.core.Application的子类,您应该可以简单地添加@RolesAllowed("user")作为对资源类的注释,如https://jersey.java.net/documentation/latest/security.html所示
@Path("/")
@PermitAll
public class Resource {
@RolesAllowed("user")
@GET
public String get() { return "GET"; }
@RolesAllowed("admin")
@POST
public String post(String content) { return content; }
@Path("sub")
public SubResource getSubResource() {
return new SubResource();
}
}访问该资源将提示您输入凭据。如果这不起作用,那么您需要提供一个小的代码示例,它编译并且不执行您希望它做的事情。这样就更容易看出问题出在哪里,需要做些什么才能让它发挥作用。
发布于 2016-08-18 18:55:53
您需要实现您的RoleChecker 使用此接口。正如医生所说:
因为Restlet不支持它自己的角色检查机制(例如Servlet ),所以如果您需要在JAX应用程序中进行角色检查,就必须使用这个整数。此界面用于检查用户是否处于角色中。实现必须是线程保存。
因此,作为实现的一个示例,您可以这样做:
public class MyRoleChecker implements RoleChecker {
public boolean isInRole(Principal principal, String role) {
return principal.getRole().equals(role);
}
}编辑:另一方面,在使用新API时,需要在资源方法中实现SecurityContext并使用@Context注入它。然后根据用户名从存储区中获取角色列表。存储实现取决于您。请参阅此示例
@Priority(Priorities.AUTHENTICATION)
public class AuthFilterWithCustomSecurityContext implements ContainerRequestFilter {
@Context
UriInfo uriInfo;
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
String authHeaderVal = requestContext.getHeaderString("Auth-Token");
String subject = validateToken(authHeaderVal); //execute custom authentication
if (subject!=null) {
final SecurityContext securityContext = requestContext.getSecurityContext();
requestContext.setSecurityContext(new SecurityContext() {
@Override
public Principal getUserPrincipal() {
return new Principal() {
@Override
public String getName() {
return subject;
}
};
}
@Override
public boolean isUserInRole(String role) {
List<Role> roles = findUserRoles(subject);
return roles.contains(role);
}
@Override
public boolean isSecure() {
return uriInfo.getAbsolutePath().toString().startsWith("https");
}
@Override
public String getAuthenticationScheme() {
return "Token-Based-Auth-Scheme";
}
});
}
}
}https://stackoverflow.com/questions/38946816
复制相似问题