- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试从java docs了解@RunAs注释的使用但我不明白它的用途。谁能解释一下吗?
我的理解是,在某些情况下,如果具有不同角色的经过身份验证的用户想要访问仅允许具有特定角色的用户访问的ejb方法,那么调用者ejb可以将自己注释为要运行作为预期的角色并且可以访问ejb方法。
所以我写了下面的代码,但我的理解是错误的。
JAX-RS 类:
package com.jee.beginner.rest;
import java.security.Principal;
import javax.inject.Inject;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.groups.ConvertGroup;
import javax.validation.groups.Default;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;
import com.jee.beginner.custom.validation.Create;
import com.jee.beginner.custom.validation.Update;
import com.jee.beginner.domain.Student;
import com.jee.beginner.service.StudentService;
import com.jee.beginner.service.proxy.StudentServiceProxy;
@Path("student")
public class StudentResource {
@Inject
private Principal principal;
@Inject
private StudentService studentService;
@Inject
private StudentServiceProxy studentServiceProxy;
@GET
@Path("details/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Student getDetails(@PathParam("id") @Min(value = 2, message = "ID cannot be less than 2") int id,
@QueryParam("id") int qid, @Context UriInfo uriInfo) {
return studentServiceProxy.getDetails(id);
}
@POST
@Path("new")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Student addStudent(@Valid @ConvertGroup(from = Default.class, to = Create.class) final Student student) {
return studentService.addStudent(student);
}
@POST
@Path("update")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Student updateStudent(@Valid @ConvertGroup(from = Default.class, to = Update.class) final Student student) {
return student;
}
}
代理类。该类被注释为@RunAs("admin")
package com.jee.beginner.service.proxy;
import javax.annotation.security.RunAs;
import javax.ejb.Stateless;
import javax.inject.Inject;
import com.jee.beginner.domain.Student;
import com.jee.beginner.service.StudentService;
@RunAs("admin")
@Stateless
public class StudentServiceProxy {
@Inject
private StudentService studentService;
public Student getDetails(int id) {
return studentService.getDetails(id);
}
}
服务等级:
package com.jee.beginner.service;
import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import com.jee.beginner.domain.Student;
@Stateless
public class StudentService {
@Resource
private EJBContext context;
@RolesAllowed({ "admin", "guest" })
public Student addStudent(final Student student) {
System.out.println(context.isCallerInRole("admin"));
return student;
}
@RolesAllowed({ "admin" })
public Student getDetails(int id) {
Student student = new Student();
student.setId(id);
student.setName("noname");
return student;
}
}
我创建了一个领域并添加了两个用户
用户A - 管理员,用户B - 访客
如果没有 RunAs 注解,UserA 能够按预期访问该方法,而 UserB 无法按预期访问该方法。
当我添加 RunAs 注释后,两个用户都无法访问 getDetails 方法。
我认为 UserB 现在能够访问该方法,因为代理带有 RunAs admin 注释,并且我认为 StudentService 会将用户视为管理员角色。但事实上,UserA 也无法访问该方法。
谁能解释一下 RunAs 注释的重要性吗?
最佳答案
@RunAs 注释可用于 @Steve C 指出的用例,也可用于您所描述的用例。
您的代码和您的假设是正确的。它不起作用,因为某些容器(例如 Wildfly)默认实现 EJB 方法权限的方式。当根本不使用安全注释时,所有方法都被假定为未选中
,就好像它们使用@PermitAll进行注释一样。但是,当您的部署中使用任何安全注释并且方法没有显式权限(在类级别或方法级别)时,Wildfly 会将其视为具有 @DenyAll。
所以,您的 StudentService 是正确的,但在您的 StudentServiceProxy 中, getDetails 方法没有任何方法权限。您应该使用 @PermitAll (任何用户、未经身份验证的事件都可以执行它)、@RolesAllowed({ "**"}) (任何经过身份验证的用户都可以执行它)或 @RolesAllowed 注释它(在方法级别或类级别) ({ "admin", "guest"}) (具有管理员或访客角色的用户执行它)。
如果使用 wildfly/jboss,您还可以更改 ejb3 子系统的默认行为。检查:https://docs.jboss.org/author/display/WFLY/Securing+EJBs
关于java - @RunAs javax 安全注解有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60206182/
我正在尝试关注 this tutorial .我想我不是从使用可下载项目开始,而是从我之前做过的一个简单的“spring MVC - Maven - eclipse”项目开始。这个项目运行良好。 因此
我觉得 Java EE 6 规范有些困惑。有几组注释。 我们有javax.ejb注释如 @Stateful和 @Stateless用于创建 EJB。 还有一个@javax.annotation.Man
我正在开发一个依赖于“javax.lang”和“javax.annotation”的java项目。我安装了jre/jdk,类路径上还有很多其他javax.*,我每天都用Java进行开发。但是,这两个包
我正在尝试将一个值添加到 JsonValue 列表中。可以这样做吗? 一些背景知识,我正在从 Rest API 检索 Json 响应,在此 Json 中是一个如下名称列表: { “名称”:{名称1,名
我正在开发一个 JSF2、Icefaces 网络应用程序。我有以下看法: 当我保存上面的 时出现以下异常. Application caught instance of
我遇到了一个小问题,我的代码中有一个异常。 我有一个基本框架类: import java.awt.BorderLayout; import java.awt.Color; import java.aw
我正在使用 Apache Derby 并具有以下代码: DBConnectionFactory.java package edu.unsw.comp9321.jdbc; import java.sql
javax.mail 和 javax.mail-api 有什么区别? 我从 maven 存储库中找到了那些。 什么时候应该使用它们? javax.mail-api自带的软件包,但我无法使用,所以我下载
我是 Java 的新手,正在尝试进入 WebServices。我在某处找到了两个示例,但对可用选项感到困惑。 首先,带注解的javax.jws.WebService 似乎工作正常,但javax.xml
在升级了一些 Glassfish/Grizzly 依赖项之后(为了与最新版本的 Azure SDK IOT 设备客户端兼容),我开始出现错误,因为 com.google.common.EventBus
我收到了一个遗留 JSP 系统,其中 Eclipse 在每次出现以下代码时都会标记“导入 javax.event 无法解析”错误: 这是我的java版本: shakir@anduril:~$ jav
我使用以下代码获取连接到系统的 USB 设备的制造商代码。我添加了 jsr80-1.0.1 jar 。我收到以下错误 javax.usb.UsbException: Properties file j
我正在学习 EJB,当尝试使用 junit 测试它时,出现以下错误 cd.espoirmur.Ejb.InterfaceEjbLocal_80488159 Jun 03, 2016 10:33:58
我必须处理一个需要提供数据源作为参数的 API。问题是我从提供 EntityManager 或 PersistentContext 的上下文访问它,它们似乎没有通过其方法公开任何数据源。如何以编程方式
据我所知,Java ServletContext 和上下文对象在 Java EE 程序开发中很常见。但是,我不太确定它们之间有什么区别,尤其是上下文类的用法。 据我了解,ServletContext
我在一个简单的 tomcat jsp 项目中遇到了这个错误。我在谷歌上阅读的文章暗示我应该在我的项目中包含 servlet-api.jar。我就是这样做的,但没有用。有人对此有任何想法吗?我的 JRE
我想对我的实体进行一些 Bean 验证,以便我可以适本地映射错误以发送回客户端。 我知道 javax.validation.constraints 注释用于实现此目的。我的问题是,对于模式生成,我需要
想法:2018.3 jetty :9.4.11 我按照 IDEA 中给出的说明下载了 jrebel 文件夹 在 Debug模式下运行 Jetty 服务器时出现以下错误。 HTTP ERROR 500
我正在开发一个主要使用无状态 session bean (SLSB) 的 EJB3 应用程序。他们使用容器管理事务 (CMT)。 我希望 bean 知道事务(用于日志记录等)。我可以实现 javax.
我正在使用 OSGI 开发 Web 应用程序。我有一个我找不到的 Maven 配置错误。这是完整的源代码: http://uploading.com/files/8e5c9888/SH_27_test
我是一名优秀的程序员,十分优秀!