@RunAs javax安全注释的用途是什么?

时间:2020-02-13 10:57:26

标签: java ejb java-security

我尝试了解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;
    }
}

我创建了一个领域并添加了两个用户

UserA-管理员, 用户B-访客

没有RunAs批注,UserA可以按预期访问方法,而UserB不能按预期访问方法。

添加RunAs批注后,两个用户都无法访问getDetails方法。

我认为UserB现在可以访问该方法,因为Proxy带有RunAs admin注释,并且我认为StudentService会将用户视为admin角色。但是实际上发生的是UserA也无法访问该方法。

有人可以向我解释RunAs注释的重要性吗?

2 个答案:

答案 0 :(得分:3)

通常,经过身份验证的用户通过某些操作将同步调用EJB方法。应用服务器将用户的安全上下文与调用一起传播,并且EJB容器可以使用此上下文来验证调用方是否与该方法的允许角色相关联。

但是,在某些情况下,可以在没有经过身份验证的用户参与的情况下调用EJB方法。这些情况包括:

  • 执行EJB计时器
  • 执行消息驱动bean(MDB)

例如,您可能已经定义了应该执行的定期服务:

@Stateless
public class StudentService {

     @Timeout
     @Schedule(...)
     @RunAs("admin")
     public void periodicCheck(Timer timer) {
         ...
     }

}

在此处使用javax.annotation.security.RunAs表示此方法应以“ admin”角色执行。

答案 1 :(得分:3)

@RunAs批注可以用于@Steve C指出的用例,也可以用于您正在描述的用例。

您的代码和您的假设是正确的。这是行不通的,因为默认情况下某些容器(例如,Wildfly)实现EJB方法权限的方式。如果根本不使用安全注释,则假定所有方法均为unchecked,就像它们在@PermitAll处注释一样。但是,当在部署中使用任何安全注释并且方法没有显式权限(在类级别或方法级别)时,Wildfly会将其视为具有@DenyAll。

因此,您的StudentService是正确的,但是在StudentServiceProxy中,getDetails方法没有任何方法权限。您应该使用@PermitAll(任何用户,未经身份验证的事件都可以执行),@ RolesAllowed({“ **”})(任何经过身份验证的用户都可以执行)或@RolesAllowed对其进行注释(在方法级别或类级别) ({{“ admin”,“ guest”})(具有admin或guest角色的用户执行该操作)。

如果使用wildfly / jboss,您还可以更改ejb3子系统的默认行为。检查:https://docs.jboss.org/author/display/WFLY/Securing+EJBs

相关问题