Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java @不带web.xml的角色允许等_Java_Jax Rs_Web.xml_Ejb 3.1 - Fatal编程技术网

Java @不带web.xml的角色允许等

Java @不带web.xml的角色允许等,java,jax-rs,web.xml,ejb-3.1,Java,Jax Rs,Web.xml,Ejb 3.1,我目前正在开发一个相当模块化的应用程序,其中我们有许多jar,它们将被打包并粘在一个war文件中 这些jar文件中的一些文件有需要保护的REST资源。常用的方法是@RolesAllowed注释等 据我目前所知,这意味着战争中存在web.xml。这样,我们就必须在战争中实现特定于jar的信息(例如,上下文根),而不是在它所属的地方 像现在的大多数事情一样,有没有一种方法可以在没有web.xml的情况下以编程方式设置安全上下文等?您可以通过注册扩展自的REST配置类来限制对REST资源的访问 因此,

我目前正在开发一个相当模块化的应用程序,其中我们有许多jar,它们将被打包并粘在一个war文件中

这些jar文件中的一些文件有需要保护的REST资源。常用的方法是@RolesAllowed注释等

据我目前所知,这意味着战争中存在web.xml。这样,我们就必须在战争中实现特定于jar的信息(例如,上下文根),而不是在它所属的地方


像现在的大多数事情一样,有没有一种方法可以在没有web.xml的情况下以编程方式设置安全上下文等?

您可以通过注册扩展自的REST配置类来限制对REST资源的访问

因此,您可以在资源方法上使用应用程序角色,如下所示

import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.SecurityContext;

@Path("secured")
@PermitAll
public class SecuredResource {

  @GET
  @Path("user")
  @RolesAllowed("user")
  public String user(@Context SecurityContext sc) {
    boolean test = sc.isUserInRole("user");
    return (test) ? "true": "false";
  }

  @GET
  @Path("admin")
  @RolesAllowed("admin")
  public String admin(@Context SecurityContext sc) {
    boolean test = sc.isUserInRole("admin");
    return (test) ? "true": "false";
  }
}
Jersey文档在这里提供了有关使用注释保护REST资源的更多详细信息


我已经有一段时间没有使用JAX-RS了,但上次检查时,在使用基于注释的安全性时,web.xml不是可选的

有关详细信息,请参阅我的答案。

import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.SecurityContext;

@Path("secured")
@PermitAll
public class SecuredResource {

  @GET
  @Path("user")
  @RolesAllowed("user")
  public String user(@Context SecurityContext sc) {
    boolean test = sc.isUserInRole("user");
    return (test) ? "true": "false";
  }

  @GET
  @Path("admin")
  @RolesAllowed("admin")
  public String admin(@Context SecurityContext sc) {
    boolean test = sc.isUserInRole("admin");
    return (test) ? "true": "false";
  }
}