Java 类org.apache.catalina.realm.JAASRealm 实例源码

项目:tomee    文件:ConfTest.java   
@Test
public void run() {
    try (final Container container = new Container(new Configuration().conf("ConfTest"))) {
        final StandardServer standardServer = TomcatHelper.getServer();
        final Realm engineRealm = standardServer.findServices()[0].getContainer().getRealm();
        assertTrue(String.valueOf(engineRealm), TomEERealm.class.isInstance(engineRealm));
        assertTrue(String.valueOf(engineRealm), JAASRealm.class.isInstance(TomEERealm.class.cast(engineRealm).getNestedRealms()[0]));
        final JAASRealm jaas = JAASRealm.class.cast(TomEERealm.class.cast(engineRealm).getNestedRealms()[0]);
        assertEquals("PropertiesLoginModule", jaas.getAppName());
        assertEquals("org.apache.openejb.core.security.jaas.UserPrincipal", jaas.getUserClassNames());
        assertEquals("org.apache.openejb.core.security.jaas.GroupPrincipal", jaas.getRoleClassNames());

        assertEquals("test", SystemInstance.get().getProperty("ConfTest.value"));
    }
}
项目:tomee    文件:ConfigurationTest.java   
@Test
public void autoConfig() {
    final Configuration configuration = new Configuration();
    configuration.loadFromProperties(new PropertiesBuilder()
            // plain params
            .p("http", "1234")
            .p("stop", "1235")
            .p("host", "here")
            .p("dir", "target/dirtmp")
            .p("quickSession", "false")
            .p("webResourceCached", "false")
            .p("withEjbRemote", "true")
            .p("deployOpenEjbApp", "true")
            .p("users.u1", "p1")
            .p("users.u2", "p2")
            .p("roles.admin", "u1,u2")
            .p("roles.simple", "u1")
            // more complex structures
            .p("realm", "org.apache.catalina.realm.JAASRealm")
            .p("realm.appName", "app")
            .p("realm.configFile", "configuration.jaas")
            // there we ensure our builders are xbean friendly
            .p("login", "")
            .p("login.realmName", "app")
            .p("login.authMethod", "BASIC")
            .p("securityConstraint", "")
            .p("securityConstraint.authConstraint", "true")
            .p("securityConstraint.authRole", "**")
            .p("securityConstraint.collection", "api:/api/*")
            .build());

    assertEquals(1234, configuration.getHttpPort());
    assertEquals(1235, configuration.getStopPort());
    assertEquals("target/dirtmp", configuration.getDir());
    assertFalse(configuration.isQuickSession());
    assertTrue(configuration.isWithEjbRemote());
    assertTrue(configuration.isDeployOpenEjbApp());
    assertEquals(new HashMap<String, String>() {{
        put("u1", "p1");
        put("u2", "p2");
    }}, configuration.getUsers());
    assertEquals(new HashMap<String, String>() {{
        put("admin", "u1,u2");
        put("simple", "u1");
    }}, configuration.getRoles());

    assertNotNull(configuration.getRealm());
    assertTrue(JAASRealm.class.isInstance(configuration.getRealm()));
    final JAASRealm realm = JAASRealm.class.cast(configuration.getRealm());
    assertEquals("app", realm.getAppName());
    assertEquals("configuration.jaas", realm.getConfigFile());

    assertNotNull(configuration.getLoginConfig());
    final LoginConfig loginConfig = configuration.getLoginConfig().build();
    assertEquals("app", loginConfig.getRealmName());
    assertEquals("BASIC", loginConfig.getAuthMethod());

    final Collection<SecurityConstaintBuilder> securityConstraints = configuration.getSecurityConstraints();
    assertNotNull(securityConstraints);
    assertEquals(1, securityConstraints.size());
    final SecurityConstraint constraint = securityConstraints.iterator().next().build();
    assertTrue(constraint.getAuthConstraint());
    assertTrue(constraint.getAuthenticatedUsers());
    assertEquals("/api/*", constraint.findCollection("api").findPatterns()[0]);
}