use of org.apache.catalina.realm.JAASRealm in project tomee by apache.
the class ConfigurationTest method autoConfig.
@Test
public void autoConfig() {
final Configuration configuration = new Configuration();
configuration.loadFromProperties(new PropertiesBuilder().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").p("realm", "org.apache.catalina.realm.JAASRealm").p("realm.appName", "app").p("realm.configFile", "configuration.jaas").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]);
}
use of org.apache.catalina.realm.JAASRealm in project tomee by apache.
the class Main method createConfiguration.
private static Configuration createConfiguration(final CommandLine args) {
final Configuration config = new Configuration();
if (args.hasOption(CONFIGURATION)) {
config.loadFrom(args.getOptionValue(CONFIGURATION));
}
config.setHttpPort(Integer.parseInt(args.getOptionValue(PORT, Integer.toString(config.getHttpPort()))));
config.setStopPort(Integer.parseInt(args.getOptionValue(SHUTDOWN, Integer.toString(config.getHttpsPort()))));
config.setDir(args.getOptionValue(DIRECTORY, config.getDir() == null ? new File(new File("."), "apache-tomee").getAbsolutePath() : config.getDir()));
if (args.hasOption(SERVER_XML)) {
config.setServerXml(args.getOptionValue(SERVER_XML));
}
if (args.hasOption(WEB_XML)) {
config.setWebXml(args.getOptionValue(WEB_XML));
}
if (args.hasOption(TOMEE_XML)) {
config.property("openejb.conf.file", args.getOptionValue(TOMEE_XML));
}
if (args.hasOption(SIMPLE_LOG)) {
config.property("openejb.jul.forceReload", "true");
}
if (args.hasOption(CLASSES_FILTER)) {
try {
config.classesFilter(Filter.class.cast(Thread.currentThread().getContextClassLoader().loadClass(args.getOptionValue(CLASSES_FILTER)).newInstance()));
} catch (final InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new IllegalArgumentException(e);
}
}
if (args.hasOption(PROPERTY)) {
for (final String opt : args.getOptionValues(PROPERTY)) {
final int sep = opt.indexOf('=');
if (sep > 0) {
config.property(opt.substring(0, sep), opt.substring(sep + 1));
} else {
config.property(opt, "true");
}
}
}
if (args.hasOption(JAAS_CONFIG)) {
final String jaas = args.getOptionValue(JAAS_CONFIG);
final File file = new File(jaas);
System.setProperty("java.security.auth.login.config", file.getAbsolutePath());
final JAASRealm realm = new JAASRealm() {
@Override
protected javax.security.auth.login.Configuration getConfig() {
try {
if (jaasConfigurationLoaded) {
return jaasConfiguration;
}
synchronized (this) {
if (configFile == null) {
jaasConfigurationLoaded = true;
return null;
}
configFile = file.getAbsolutePath();
final Class<?> sunConfigFile = Class.forName("com.sun.security.auth.login.ConfigFile");
final Constructor<?> constructor = sunConfigFile.getConstructor(URI.class);
javax.security.auth.login.Configuration config = javax.security.auth.login.Configuration.class.cast(constructor.newInstance(file.toURI()));
this.jaasConfiguration = config;
this.jaasConfigurationLoaded = true;
return this.jaasConfiguration;
}
} catch (final NoSuchMethodException | SecurityException | IllegalArgumentException | IllegalAccessException | InstantiationException | InvocationTargetException | ClassNotFoundException ex) {
throw new RuntimeException(ex);
}
}
};
realm.setAppName("application");
realm.setConfigFile(new File(jaas).getAbsolutePath());
config.setRealm(realm);
}
if (args.hasOption(BASIC)) {
config.loginConfig(new LoginConfigBuilder().basic().realmName(System.getProperty("tomee.embedded.main.basic.realm", "Security")));
config.securityConstaint(new SecurityConstaintBuilder().authConstraint(true).addAuthRole(System.getProperty("tomee.embedded.main.basic.role", "*")).addCollection("Basic", System.getProperty("tomee.embedded.main.basic.pattern", "/*")).displayName(System.getProperty("tomee.embedded.main.basic.display-name", "Basic security")));
}
if (args.hasOption(CACHE_WEB_RESOURCES)) {
config.setWebResourceCached(Boolean.parseBoolean(args.getOptionValue(CACHE_WEB_RESOURCES)));
}
if (args.hasOption(SSL_PROTOCOL)) {
config.setSslProtocol(args.getOptionValue(SSL_PROTOCOL));
}
if (args.hasOption(KEY_ALIAS)) {
config.setKeyAlias(args.getOptionValue(KEY_ALIAS));
}
if (args.hasOption(KEYSTORE_TYPE)) {
config.setKeystoreType(args.getOptionValue(KEYSTORE_TYPE));
}
if (args.hasOption(KEYSTORE_PASS)) {
config.setKeystorePass(simpleValue(args.getOptionValue(KEYSTORE_PASS)));
}
if (args.hasOption(KEYSTORE_FILE)) {
config.setKeystoreFile(args.getOptionValue(KEYSTORE_FILE));
}
if (args.hasOption(SSL)) {
config.setSsl(Boolean.parseBoolean(args.getOptionValue(SSL)));
}
if (args.hasOption(HTTPS_PORT)) {
config.setHttpsPort(Integer.parseInt(args.getOptionValue(HTTPS_PORT)));
}
if (args.hasOption(SKIP_HTTP)) {
config.setSkipHttp(Boolean.parseBoolean(args.getOptionValue(SKIP_HTTP)));
}
if (args.hasOption(QUICK_SESSION)) {
config.setQuickSession(Boolean.parseBoolean(args.getOptionValue(QUICK_SESSION)));
}
return config;
}
use of org.apache.catalina.realm.JAASRealm in project tomee by apache.
the class ConfTest method run.
@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"));
}
}
Aggregations