下面列出了javax.naming.directory.DirContext#getEnvironment ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testGetReadOnlyContext() throws NamingException {
DirContext ctx = null;
try {
ctx = tested.getReadOnlyContext();
assertThat(ctx).isNotNull();
Hashtable environment = ctx.getEnvironment();
assertThat(environment.containsKey(LdapContextSource.SUN_LDAP_POOLING_FLAG)).isFalse();
assertThat(environment.containsKey(Context.SECURITY_PRINCIPAL)).isTrue();
assertThat(environment.containsKey(Context.SECURITY_CREDENTIALS)).isTrue();
}
finally {
// Always clean up.
if (ctx != null) {
try {
ctx.close();
}
catch (Exception e) {
// Never mind this
}
}
}
}
@Test
public void testGetReadWriteContext() throws NamingException {
DirContext ctx = null;
try {
ctx = tested.getReadWriteContext();
assertThat(ctx).isNotNull();
// Double check to see that we are authenticated.
Hashtable environment = ctx.getEnvironment();
assertThat(environment.containsKey(LdapContextSource.SUN_LDAP_POOLING_FLAG)).isFalse();
assertThat(environment.containsKey(Context.SECURITY_PRINCIPAL)).isTrue();
assertThat(environment.containsKey(Context.SECURITY_CREDENTIALS)).isTrue();
}
finally {
// Always clean up.
if (ctx != null) {
try {
ctx.close();
}
catch (Exception e) {
// Never mind this
}
}
}
}
/**
* Create a DirContext using the supplied environment.
*
* @param environment the LDAP environment to use when creating the
* <code>DirContext</code>.
* @return a new DirContext implementation initialized with the supplied
* environment.
*/
protected DirContext createContext(Hashtable<String, Object> environment) {
DirContext ctx = null;
try {
ctx = getDirContextInstance(environment);
if (LOG.isInfoEnabled()) {
Hashtable<?, ?> ctxEnv = ctx.getEnvironment();
String ldapUrl = (String) ctxEnv.get(Context.PROVIDER_URL);
LOG.debug("Got Ldap context on server '" + ldapUrl + "'");
}
return ctx;
}
catch (NamingException e) {
closeContext(ctx);
throw LdapUtils.convertLdapException(e);
}
}
@Test
@Category(NoAdTest.class)
public void testGetContext() throws NamingException {
DirContext ctx = null;
try {
String expectedPrincipal = "cn=Some Person,ou=company1,ou=Sweden," + base;
String expectedCredentials = "password";
ctx = tested.getContext(expectedPrincipal, expectedCredentials);
assertThat(ctx).isNotNull();
// Double check to see that we are authenticated, and that we did not receive
// a connection eligible for connection pooling.
Hashtable environment = ctx.getEnvironment();
assertThat(environment.containsKey(LdapContextSource.SUN_LDAP_POOLING_FLAG)).isFalse();
assertThat(environment.get(Context.SECURITY_PRINCIPAL)).isEqualTo(expectedPrincipal);
assertThat(environment.get(Context.SECURITY_CREDENTIALS)).isEqualTo(expectedCredentials);
}
finally {
// Always clean up.
if (ctx != null) {
try {
ctx.close();
}
catch (Exception e) {
// Never mind this
}
}
}
}
/**
* Get the principal associated with the specified certificate.
* @param context The directory context
* @param username The user name
* @param gssCredential The credentials
* @return the Principal associated with the given certificate.
* @exception NamingException if a directory server error occurs
*/
protected synchronized Principal getPrincipal(DirContext context,
String username, GSSCredential gssCredential)
throws NamingException {
User user = null;
List<String> roles = null;
Hashtable<?, ?> preservedEnvironment = null;
try {
if (gssCredential != null && isUseDelegatedCredential()) {
// Preserve the current context environment parameters
preservedEnvironment = context.getEnvironment();
// Set up context
context.addToEnvironment(
Context.SECURITY_AUTHENTICATION, "GSSAPI");
context.addToEnvironment(
"javax.security.sasl.server.authentication", "true");
context.addToEnvironment(
"javax.security.sasl.qop", spnegoDelegationQop);
// Note: Subject already set in SPNEGO authenticator so no need
// for Subject.doAs() here
}
user = getUser(context, username);
if (user != null) {
roles = getRoles(context, user);
}
} finally {
restoreEnvironmentParameter(context,
Context.SECURITY_AUTHENTICATION, preservedEnvironment);
restoreEnvironmentParameter(context,
"javax.security.sasl.server.authentication", preservedEnvironment);
restoreEnvironmentParameter(context, "javax.security.sasl.qop",
preservedEnvironment);
}
if (user != null) {
return new GenericPrincipal(user.getUserName(), user.getPassword(),
roles, null, null, gssCredential);
}
return null;
}
/**
* Return the Principal associated with the given user name.
*/
protected synchronized Principal getPrincipal(DirContext context,
String username, GSSCredential gssCredential)
throws NamingException {
User user = null;
List<String> roles = null;
Hashtable<?, ?> preservedEnvironment = null;
try {
if (gssCredential != null && isUseDelegatedCredential()) {
// Preserve the current context environment parameters
preservedEnvironment = context.getEnvironment();
// Set up context
context.addToEnvironment(
Context.SECURITY_AUTHENTICATION, "GSSAPI");
context.addToEnvironment(
"javax.security.sasl.server.authentication", "true");
context.addToEnvironment(
"javax.security.sasl.qop", spnegoDelegationQop);
// Note: Subject already set in SPNEGO authenticator so no need
// for Subject.doAs() here
}
user = getUser(context, username);
if (user != null) {
roles = getRoles(context, user);
}
} finally {
restoreEnvironmentParameter(context,
Context.SECURITY_AUTHENTICATION, preservedEnvironment);
restoreEnvironmentParameter(context,
"javax.security.sasl.server.authentication", preservedEnvironment);
restoreEnvironmentParameter(context, "javax.security.sasl.qop",
preservedEnvironment);
}
if (user != null) {
return new GenericPrincipal(user.getUserName(), user.getPassword(),
roles, null, null, gssCredential);
}
return null;
}
/**
* Return the Principal associated with the given user name.
*/
protected synchronized Principal getPrincipal(DirContext context,
String username, GSSCredential gssCredential)
throws NamingException {
User user = null;
List<String> roles = null;
Hashtable<?, ?> preservedEnvironment = null;
try {
if (gssCredential != null && isUseDelegatedCredential()) {
// Preserve the current context environment parameters
preservedEnvironment = context.getEnvironment();
// Set up context
context.addToEnvironment(
Context.SECURITY_AUTHENTICATION, "GSSAPI");
context.addToEnvironment(
"javax.security.sasl.server.authentication", "true");
context.addToEnvironment(
"javax.security.sasl.qop", spnegoDelegationQop);
// Note: Subject already set in SPNEGO authenticator so no need
// for Subject.doAs() here
}
user = getUser(context, username);
if (user != null) {
roles = getRoles(context, user);
}
} finally {
restoreEnvironmentParameter(context,
Context.SECURITY_AUTHENTICATION, preservedEnvironment);
restoreEnvironmentParameter(context,
"javax.security.sasl.server.authentication", preservedEnvironment);
restoreEnvironmentParameter(context, "javax.security.sasl.qop",
preservedEnvironment);
}
if (user != null) {
return new GenericPrincipal(user.getUserName(), user.getPassword(),
roles, null, null, gssCredential);
}
return null;
}