Jakarta ee Struts 2访问请求.isUserInRole

Jakarta ee Struts 2访问请求.isUserInRole,jakarta-ee,authentication,struts2,jboss,roles,Jakarta Ee,Authentication,Struts2,Jboss,Roles,我正在JBossAS7服务器上使用Struts 2和Rest服务开发一个小型Java EE应用程序 我对REST和Web内容使用FORM auth,我对REST没有任何问题,但在Web上,我必须区分“manager”和“admin”角色,以便向管理员用户显示一些额外的选项 我想做一些类似的事情: 您可以通过用于呈现JSP的操作来实现它。当您在action Struts2中实现它时,它会将对象注入到您的操作中。确保已应用于操作 public class MyAction extends Actio

我正在JBossAS7服务器上使用Struts 2和Rest服务开发一个小型Java EE应用程序

我对REST和Web内容使用FORM auth,我对REST没有任何问题,但在Web上,我必须区分
“manager”
“admin”
角色,以便向管理员用户显示一些额外的选项

我想做一些类似的事情:

您可以通过用于呈现JSP的操作来实现它。当您在action Struts2中实现它时,它会将对象注入到您的操作中。确保已应用于操作

public class MyAction extends ActionSupport implements PrincipalAware {

  protected PrincipalProxy principal;

  public void setPrincipalProxy(PrincipalProxy principalProxy) {
    this.principal = principalProxy;
  }

  public PrincipalProxy getPrincipal() {
    return principal;
  }
}
然后您可以在JSP页面上的
valueStack
中找到该对象,并执行如下操作

<s:if test="principal.isUserInRole('admin')">


您也可以通过

访问此信息。我通过以下方式解决了此问题:

进口:

<jsp:directive.page import="com.opensymphony.xwork2.util.ValueStack"/>
<jsp:directive.page import="com.opensymphony.xwork2.ActionContext"/>
<jsp:directive.page import="org.apache.struts2.ServletActionContext"/>

然后将请求与:

<jsp:scriptlet><![CDATA[
ValueStack stack = ActionContext.getContext().getValueStack();
 stack.set("httpServletRequest", ServletActionContext.getRequest());
]]></jsp:scriptlet>

以及检查角色:

<s:if test="httpServletRequest.isUserInRole('admin')">


使用它的问题是我需要实现Principalware并在所有操作中注入PrincipalProxy,因为我需要检查所有JSP中的角色。这是一个有效的选项,但我认为这将使我的代码在操作中更加混乱。@Pablo,只需在PabloBaseAction中执行,并使其他操作扩展它,而不是直接扩展ActionSupport。。。没有混乱,只是一个好问题,但非常丑陋的解决方案。