public
Created

An example of integrating Guice and Shiro

  • Download Gist
Guice - Shiro Example.java
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;
 
import javax.naming.NamingException;
 
import org.apache.shiro.authc.pam.AuthenticationStrategy;
import org.apache.shiro.authc.pam.FirstSuccessfulStrategy;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.realm.activedirectory.ActiveDirectoryRealm;
import org.apache.shiro.realm.ldap.DefaultLdapContextFactory;
import org.apache.shiro.realm.ldap.LdapContextFactory;
 
import com.google.inject.AbstractModule;
import com.google.inject.Inject;
import com.google.inject.Module;
import com.google.inject.Provider;
import com.google.inject.ProvisionException;
import com.google.inject.name.Named;
import com.google.inject.name.Names;
 
public class ActiveDirectoryAuthenticationModule {
public final class ActiveDirectoryAuthenticationModuleBuilder {
private final Set<String> domainControllers = new TreeSet<String>();
private final Set<String> principalSuffixes = new TreeSet<String>();
private boolean usingSSL = false;
private int port = -1;
private long sessionTimeout = -1;
private String domain = null;
private String searchBase = null;
private String systemUser = null;
private String systemUserPassword = null;
private ActiveDirectoryAuthenticationModuleBuilder() {}
public ActiveDirectoryAuthenticationModuleBuilder overPort(int port) {
if (port < 1 || port > 65536)
throw new IllegalArgumentException(String.format("%d is not a valid port number", port));
this.port = port;
return this;
}
public ActiveDirectoryAuthenticationModuleBuilder usingSSL() {
usingSSL = true;
return this;
}
public ActiveDirectoryAuthenticationModuleBuilder usingSystemUser(String systemUser) {
if (systemUser == null || systemUser.isEmpty())
throw new IllegalArgumentException("System user cannot be null or empty");
this.systemUser = systemUser;
return this;
}
public ActiveDirectoryAuthenticationModuleBuilder withSystemUserPassword(String systemUserPassword) {
if (systemUserPassword == null || systemUserPassword.isEmpty())
throw new IllegalArgumentException("System user password cannot be null or empty");
this.systemUserPassword = systemUserPassword;
return this;
}
public ActiveDirectoryAuthenticationModuleBuilder withDomainController(String domainController) {
if (domainController == null || domainController.isEmpty())
throw new IllegalArgumentException("Domain controller cannot be null or empty");
domainControllers.add(domainController);
return this;
}
public ActiveDirectoryAuthenticationModuleBuilder withPrincipalSuffix(String suffix) {
if (suffix == null || suffix.isEmpty())
throw new IllegalArgumentException("Principal suffix cannot be null or empty");
if (!suffix.startsWith("@"))
suffix = "@" + suffix;
principalSuffixes.add(suffix);
return this;
}
public ActiveDirectoryAuthenticationModuleBuilder withSearchBase(String searchBase) {
if (searchBase == null || searchBase.isEmpty())
throw new IllegalArgumentException("Search base cannot be null or empty");
this.searchBase = searchBase;
return this;
}
public ActiveDirectoryAuthenticationModuleBuilder withSessionTimeout(int sessionTimeoutInMinutes) {
// 2562047788015 * 3600000 ~= Long.MAX_VALUE -- prevents value overruns
if (sessionTimeoutInMinutes < 1 || sessionTimeoutInMinutes > 2562047788015L) {
sessionTimeout = -1;
return this;
}
sessionTimeout = 3600000 * sessionTimeoutInMinutes;
return this;
}
public ActiveDirectoryAuthenticationModuleBuilder withSessionTimeoutInHours(int sessionTimeoutInHours) {
return withSessionTimeout(60 * sessionTimeoutInHours);
}
public Module buildModule() {
return new AbstractModule() {
@Override
protected void configure() {
if (systemUser == null || systemUser.isEmpty())
if (systemUserPassword == null || systemUserPassword.isEmpty())
throw new IllegalStateException("System user and password must be supplied to be able to connect to Active Directory");
else
throw new IllegalStateException("System user must be supplied in order to connect to Active Directory");
if (systemUserPassword == null || systemUserPassword.isEmpty())
throw new IllegalStateException("System user password must be supplied in order to connect to Active Directory");
bind(SecurityManager.class).toProvider(new Provider<SecurityManager>() {
@Inject private Provider<ActiveDirectoryRealm> realmProvider;
@Inject private Provider<AuthenticationStrategy> authenticationStrategyProvider;
@Inject private Provider<DefaultLdapContextFactory> ldapContextFactoryProvider;
@Override
public SecurityManager get() {
Collection<Realm> realms = new LinkedHashSet<Realm>();
if (!principalSuffixes.isEmpty())
for (String upnSuffix : principalSuffixes) {
DefaultLdapContextFactory factory = ldapContextFactoryProvider.get();
factory.setPrincipalSuffix(upnSuffix);
ActiveDirectoryRealm realm = realmProvider.get();
realm.setPrincipalSuffix(upnSuffix);
realm.setLdapContextFactory(factory);
realms.add(realm);
}
else {
LdapContextFactory factory = ldapContextFactoryProvider.get();
ActiveDirectoryRealm realm = realmProvider.get();
realm.setLdapContextFactory(factory);
realms.add(realm);
}
DefaultSecurityManager securityManager = new DefaultSecurityManager(realms);
securityManager.setAuthenticationStrategy(authenticationStrategyProvider.get());
if (sessionTimeout > 0)
securityManager.setGlobalSessionTimeout(216000000);
return securityManager;
}
});
bind(ActiveDirectoryRealm.class).toProvider(new Provider<ActiveDirectoryRealm>() {
@Inject @Named("realmUrl") String url;
@Override
public ActiveDirectoryRealm get() {
ActiveDirectoryRealm realm = new ActiveDirectoryRealm();
if (url != null && !url.isEmpty()) realm.setUrl(url);
if (searchBase != null && !searchBase.isEmpty()) realm.setSearchBase(searchBase);
realm.setSystemUsername(systemUser);
realm.setSystemPassword(systemUserPassword);
return realm;
}
});
bind(DefaultLdapContextFactory.class).toProvider(new Provider<DefaultLdapContextFactory>() {
@Inject @Named("realmUrl") String url;
@Override
public DefaultLdapContextFactory get() {
DefaultLdapContextFactory factory = new DefaultLdapContextFactory();
if (url != null && !url.isEmpty()) factory.setUrl(url);
if (searchBase != null && !searchBase.isEmpty()) factory.setSearchBase(searchBase);
factory.setSystemUsername(systemUser);
factory.setSystemPassword(systemUserPassword);
return factory;
}
});
StringBuilder realmUrl = new StringBuilder();
for (String domainController : domainControllers)
realmUrl.append(String.format("%s://%s:%d ", usingSSL ? "ldaps" : "ldap", domainController, port > 0 ? port : usingSSL ? 636 : 389));
bindConstant().annotatedWith(Names.named("realmUrl")).to(realmUrl.toString());
}
};
}
}
private ActiveDirectoryAuthenticationModule() {}
private ActiveDirectoryAuthenticationModuleBuilder get() {
return new ActiveDirectoryAuthenticationModuleBuilder();
}
 
public static ActiveDirectoryAuthenticationModuleBuilder configure() {
return new ActiveDirectoryAuthenticationModule().get();
};
 
}

Basically, to use, configure the realm, e.g.,

 Module adModule = ActiveDirectoryAuthenticationModule.configure()
    .withDomainController("dc1.fabrikam.com")
    .withSystemUser("administrator@fabrikam.com")
    .withSystemUserPassword("secure")
    .buildModule();

And wherever you need it, inject a SecurityManager.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.