简体   繁体   English

@RunAs注释在Servlet上的使用

[英]@RunAs annotation use on Servlet

I have a simple question about @RunAs. 我有一个关于@RunAs的简单问题。

Besides an EJB, can I use @RunAs on a Servlet to propagate security role? 除了EJB,我可以在Servlet上使用@RunAs来传播安全角色吗? for example as: 例如:

I have a public servlet: 我有一个公共servlet:

@WebServlet(name = "RunAsServlet", urlPatterns = "/runAs")
@RunAs("Admin")
public class RunAsServlet extends HttpServlet {

@EJB
private MyEjb ejb;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.getWriter().write(ejb.doSomething());
    }
}

And the an EJB is protected under role "Admin": 并且EJB受角色“ Admin”保护:

@Stateless
@RolesAllowed("Admin")
public class MyEjb {

@Resource
private SessionContext sessionContext;

public String doSomething() {

In this case, I should be able to call the RunAsServlet since the @RunAs is on it, it shall have the proper role "Admin" to call the EJB protected method from web request, Am I right about this example? 在这种情况下,由于@RunAs在上面,因此我应该能够调用RunAsServlet,它应具有适当的角色“ Admin”以从Web请求中调用EJB保护的方法,我对这个示例正确吗? Or I need other more configuration? 还是我需要其他更多配置?

Thanks, 谢谢,

You need to map specific user to the roleOnServlet role and this user must be also in Admin role. 您需要将特定用户映射到roleOnServlet角色,并且该用户也必须是Admin角色。 Either it is a typo or you have 2 different roles roleOnServlet != Admin . 是错字,还是您有两个不同的角色roleOnServlet != Admin Usually one use the same role name in servlet as in EJB that must be called. 通常,在servlet中使用与必须调用的EJB中相同的角色名称。 As in your case although servlet will be running as user in roleOnServlet role, he doesn't necessary have to be in the Admin role at the same time, which will lead to error. 与您的情况一样,尽管servlet将以roleOnServlet角色的用户roleOnServlet运行,但他不必同时处于Admin角色,这将导致错误。

@RunAs is not changing the current user to have that role, but is using separate user, which must be one of the users mapped to the roleOnServlet role. @RunAs不会将当前用户更改为具有该角色,而是使用单独的用户,该用户必须是映射到roleOnServlet角色的用户之一。

Mapping of the user to runAs role is container specific, so if you need help in that, add information what server are you using. 用户到runAs角色的映射是特定于容器的,因此,如果您需要帮助,请添加信息您正在使用的服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM