如何以编程方式在 OpenEJB 中指定我的用户/角色?

发布于 2024-12-08 16:19:27 字数 237 浏览 3 评论 0原文

在我正在从事的项目中,我们使用 OpenEJB 作为框架来测试我们的 EJB。我们以编程方式构造一个InitialContext,并使用它来获取事务管理器和各种EJB。

但是,我现在必须测试的 EJB 具有 @RolesAllowed 注释,因此 OpenEJB 拒绝获取该 EJB,认为我没有所需的权限。

我如何向 OpenEJB 指定此测试要模拟的用户以及与他关联的角色?

In the project I'm working on, we are using OpenEJB as a framework to test our EJB. We construct an InitialContext programatically, and use it to get the transaction manager and the various EJB.

However, the EJB I have to test now has the @RolesAllowed annotation, and so OpenEJB refuses to get that EJB, arguing I don't have the permissions required.

How can I specify to OpenEJB the user this test is supposed to simulate, and the role associated with him?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ㄖ落Θ余辉 2024-12-15 16:19:27

bkail 提到的 @RunAs 建议绝对是一个好方法。第二种不涉及内部类的方法是登录测试用例。

登录方法

当您引导 OpenEJB 时,请在 InitialContext 属性中指定用户/密码,如下所示:

public void testAsManager() throws Exception {
    Properties p = new Properties();
    p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
    p.put(Context.SECURITY_PRINCIPAL, "jane");
    p.put(Context.SECURITY_CREDENTIALS, "waterfall");

    InitialContext context = new InitialContext(p);

    try {
        movies.addMovie(new Movie("Quentin Tarantino", "Reservoir Dogs", 1992));
        movies.addMovie(new Movie("Joel Coen", "Fargo", 1996));
        movies.addMovie(new Movie("Joel Coen", "The Big Lebowski", 1998));

        List<Movie> list = movies.getMovies();
        assertEquals("List.size()", 3, list.size());

        for (Movie movie : list) {
            movies.deleteMovie(movie);
        }

        assertEquals("Movies.getMovies()", 0, movies.getMovies().size());
    } finally {
        context.close();
    }
}

然后也许以不同的用户身份再次测试:

public void testAsEmployee() throws Exception {
    Properties p = new Properties();
    p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
    p.put(Context.SECURITY_PRINCIPAL, "joe");
    p.put(Context.SECURITY_CREDENTIALS, "cool");

    InitialContext context = new InitialContext(p);

    try {
        movies.addMovie(new Movie("Quentin Tarantino", "Reservoir Dogs", 1992));
        movies.addMovie(new Movie("Joel Coen", "Fargo", 1996));
        movies.addMovie(new Movie("Joel Coen", "The Big Lebowski", 1998));

        List<Movie> list = movies.getMovies();
        assertEquals("List.size()", 3, list.size());

        for (Movie movie : list) {
            try {
                movies.deleteMovie(movie);
                fail("Employees should not be allowed to delete");
            } catch (EJBAccessException e) {
                // Good, Employees cannot delete things
            }
        }

        // The list should still be three movies long
        assertEquals("Movies.getMovies()", 3, movies.getMovies().size());
    } finally {
        context.close();
    }
}

测试用户和组

您可以通过添加 <测试用例的类路径中的 code>users.properties 和 groups.properties 文件。在 Maven 中,它位于以下位置:

  • src/test/resources/users.properties
  • src/test/resources/groups.properties

users.properties 文件可能如下所示

joe=cool
jane=waterfall

groups .properties 像这样

Manager=jane
Employee=jane,joe

The @RunAs suggestion bkail mentions is definitely a good way to go. The second approach that doesn't involve inner classes is to login in the testcase.

Login approach

When you bootstrap OpenEJB, specify the user/pass in the InitialContext properties as follows:

public void testAsManager() throws Exception {
    Properties p = new Properties();
    p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
    p.put(Context.SECURITY_PRINCIPAL, "jane");
    p.put(Context.SECURITY_CREDENTIALS, "waterfall");

    InitialContext context = new InitialContext(p);

    try {
        movies.addMovie(new Movie("Quentin Tarantino", "Reservoir Dogs", 1992));
        movies.addMovie(new Movie("Joel Coen", "Fargo", 1996));
        movies.addMovie(new Movie("Joel Coen", "The Big Lebowski", 1998));

        List<Movie> list = movies.getMovies();
        assertEquals("List.size()", 3, list.size());

        for (Movie movie : list) {
            movies.deleteMovie(movie);
        }

        assertEquals("Movies.getMovies()", 0, movies.getMovies().size());
    } finally {
        context.close();
    }
}

Then perhaps test again as a different user:

public void testAsEmployee() throws Exception {
    Properties p = new Properties();
    p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
    p.put(Context.SECURITY_PRINCIPAL, "joe");
    p.put(Context.SECURITY_CREDENTIALS, "cool");

    InitialContext context = new InitialContext(p);

    try {
        movies.addMovie(new Movie("Quentin Tarantino", "Reservoir Dogs", 1992));
        movies.addMovie(new Movie("Joel Coen", "Fargo", 1996));
        movies.addMovie(new Movie("Joel Coen", "The Big Lebowski", 1998));

        List<Movie> list = movies.getMovies();
        assertEquals("List.size()", 3, list.size());

        for (Movie movie : list) {
            try {
                movies.deleteMovie(movie);
                fail("Employees should not be allowed to delete");
            } catch (EJBAccessException e) {
                // Good, Employees cannot delete things
            }
        }

        // The list should still be three movies long
        assertEquals("Movies.getMovies()", 3, movies.getMovies().size());
    } finally {
        context.close();
    }
}

Test users and groups

You can configure test users and groups by putting a users.properties and groups.properties file in the classpath of the testcase. In maven that'd be at the following locations:

  • src/test/resources/users.properties
  • src/test/resources/groups.properties

The users.properties file might look like this

joe=cool
jane=waterfall

And groups.properties like so

Manager=jane
Employee=jane,joe
¢蛋碎的人ぎ生 2024-12-15 16:19:27

请参阅 OpenEJB 测试安全示例。基本上,您间接通过一个没有@RolesAllowed 的测试bean,但在调用第二个bean 之前使用@RunAs 来切换角色。

See the OpenEJB Testing Security Example. Basically, you indirect through a test bean that has no @RolesAllowed, but uses @RunAs to switch roles before calling the second bean.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文