Basic authentication with embedded tomcat(埋め込みTomcatでBASIC認証する)
<?xml version =' 1.0' encoding =' utf-8' ?>
<tomcat-users >
<role rolename =" admin" />
<user username =" user" password =" pass" roles =" admin" />
</tomcat-users >
public class EmbeddedTomcat
private static Tomcat tomcat = new Tomcat ();
private static final String AUTH_ROLE = "admin" ;
public void startTomcat () {
tomcat .setBaseDir ("tomcat" );
Context ctx = tomcat .addWebapp ("/" , "sample.war" );
tomcat .setHostname ("localhost" );
tomcat .setPort (8080 );
LoginConfig config = new LoginConfig ();
config .setAuthMethod ("BASIC" );
ctx .setLoginConfig (config );
ctx .addSecurityRole (AUTH_ROLE );
SecurityConstraint constraint = new SecurityConstraint ();
constraint .addAuthRole (AUTH_ROLE );
SecurityCollection collection = new SecurityCollection ();
collection .addPattern ("/*" );
constraint .addCollection (collection );
ctx .addConstraint (constraint );
String path = "/path/to/tomcat-users.xml" ;
MemoryRealm realm = new MemoryRealm ();
realm .setPathname (path );
tomcat .getEngine ().setRealm (realm );
tomcat .start ();
}
}
Very helpful. Thanks. Is there a way to also add access logs to embedded tomcat? (maybe through adding an AccessLogValve?)