【问题标题】:Can't call EJB 3 security methods without @PermitAll没有 @PermitAll 就不能调用 EJB 3 安全方法
【发布时间】:2016-01-08 18:06:51
【问题描述】:

我有一个 EJB 3.0 会话 Bean,包括一个“Hello World”远程接口和一个“HelloWorldBean”会话Bean。现在我尝试为它们添加一些安全性。这是我的 SessionBean:

@Stateful
@SecurityDomain("other")
@DeclareRoles("ejb")
@PermitAll
public class HelloWorldBean implements HelloWorld {
@Resource
private SessionContext context;

/**
 * Default constructor.
 */
public HelloWorldBean() {
    // TODO Auto-generated constructor stub
}


@Override
@PermitAll
public String sayHello() throws Exception {
    String s = this.toString();
    s = s + " Security: "+context.isCallerInRole("ejb") + " "+context.getCallerPrincipal().getName();
    return s;
}
}

我使用@PermitAll 对应用程序进行了一般测试,它可以正常工作。如果我离开上面的注释,我会得到一个异常,告诉我不允许调用方法sayHello()getCallerPrincipal().getName() 方法总是返回“匿名”。下面的类是我的独立客户端。

public class EjbAccess3_1_v2 {


    public static void main(String[] args) throws Exception {

        String providerUrl = "remote://localhost:4447";
        final String appName = "NewApplication";
        final String moduleName = "EJB3.1";
        final String distinctName = "";
        final String beanName = "HelloWorldBean";
        final String viewClassName = HelloWorld.class.getName();
        final String ejbPath= appName + "/" + moduleName +  "/" + beanName + "!" + viewClassName;        

        Properties environment = new Properties();
        environment.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
        environment.put(Context.PROVIDER_URL, providerUrl);
        environment.put("jboss.naming.client.ejb.context", true);
        environment.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
        environment.put(Context.SECURITY_PRINCIPAL, "ejb");
        environment.put(Context.SECURITY_CREDENTIALS, "password");
        InitialContext initialContext = new InitialContext(environment); 
        HelloWorld remote = (HelloWorld) initialContext.lookup(ejbPath);

        System.out.println("EJB V3.1 v2");
        System.out.println(remote.sayHello());

    }
}

我使用 JBoss 6 作为应用程序服务器并创建了一个应用程序用户“ejb”。只要我不使用任何安全性或在我想要调用的 SessionBean 类和方法中使用@PermitAll,客户端就可以工作。希望有人能帮帮我,谢谢。

【问题讨论】:

    标签: java security jakarta-ee jboss ejb


    【解决方案1】:

    您有一个名为“ejb”的用户和一个名为“ejb”的角色。如果用户和角色尚未相互映射,则需要map them 1st somehow

    如果已经映射,则需要使用注解

    @RolesAllowed({"ejb"})
    

    关于here等类或方法。

    【讨论】:

    • 感谢您的回答。我尝试创建一个角色“ejb”,但没有成功,所以我将用户“ejb”映射到角色“管理员”,之后我尝试声明安全性,如您的第二个链接所示,但它没有工作。我用“ejb”和“管理员”尝试了 DeclareRoles 和 RolesAllowed。结果是一样的,我不允许调用该方法。我还将配置文件(来自您的第二个链接)作为 jboss.xml 添加到我的 META-INF 文件夹中,但没有任何变化
    • 我现在又测试了一些案例,至少取得了一点成功。现在 Principal 的 getName() 方法返回“ejb”而不是匿名,但我仍然不允许调用该方法。
    猜你喜欢
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 2014-02-08
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多