-
Notifications
You must be signed in to change notification settings - Fork 539
/
TomcatAuthenticationContainerDiscovery.java
58 lines (47 loc) · 2.21 KB
/
TomcatAuthenticationContainerDiscovery.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package io.hawt.web.tomcat;
import java.lang.management.ManagementFactory;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import io.hawt.web.AuthenticationConfiguration;
import io.hawt.web.AuthenticationContainerDiscovery;
import io.hawt.web.AuthenticationHelpers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* To use Apache Tomcat using its conf/tomcat-users.xml for authentication.
* <p/>
* To use this, then the {@link io.hawt.web.AuthenticationConfiguration#getRealm()} must be empty or "*". Otherwise
* if an explicit configured realm has been set, then regular JAAS authentication is in use.
*/
public class TomcatAuthenticationContainerDiscovery implements AuthenticationContainerDiscovery {
private static final transient Logger LOG = LoggerFactory.getLogger(TomcatAuthenticationContainerDiscovery.class);
@Override
public String getContainerName() {
return "Apache Tomcat";
}
@Override
public boolean canAuthenticate(AuthenticationConfiguration configuration) {
if (!AuthenticationHelpers.isEmptyOrAllRealm(configuration.getRealm())) {
LOG.debug("Realm explicit configured {}. {} userdata authentication integration not in use.", configuration.getRealm(), getContainerName());
return false;
}
try {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
boolean isTomcat = server.isRegistered(new ObjectName("Catalina:type=Server"));
if (!isTomcat) {
isTomcat = server.isRegistered(new ObjectName("Tomcat:type=Server"));
}
LOG.debug("Checked for {} in JMX for {} -> {}", getContainerName(), isTomcat);
if (isTomcat) {
configuration.setConfiguration(new TomcatLoginContextConfiguration());
configuration.setRolePrincipalClasses(TomcatPrincipal.class.getName());
}
return isTomcat;
} catch (MalformedObjectNameException e) {
// ignore
LOG.warn("Error checking in JMX for " + getContainerName() + ". This exception is ignored.", e);
}
return false;
}
}