Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2009-2015 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.springframework.security.kerberos.authentication.sun;
 
 
 
Implementation of org.springframework.security.kerberos.authentication.KerberosClient which uses the SUN JAAS login module, which is included in the SUN JRE, it will not work with an IBM JRE. The whole configuration is done in this class, no additional JAAS configuration is needed.

Author(s):
Mike Wiesner
Since:
1.0
 
 public class SunJaasKerberosClient implements KerberosClient {
 
     private boolean debug = false;
 
     private static final Log LOG = LogFactory.getLog(SunJaasKerberosClient.class);
 
     @Override
     public String login(String usernameString password) {
         .debug("Trying to authenticate " + username + " with Kerberos");
         String validatedUsername;
 
         try {
             LoginContext loginContext = new LoginContext(""nullnew KerberosClientCallbackHandler(usernamepassword),
                     new LoginConfig(this.));
             loginContext.login();
             if (.isDebugEnabled()) {
                 .debug("Kerberos authenticated user: "+loginContext.getSubject());
             }
             validatedUsername = loginContext.getSubject().getPrincipals().iterator().next().toString();
             loginContext.logout();
         } catch (LoginException e) {
             throw new BadCredentialsException("Kerberos authentication failed"e);
         }
         return validatedUsername;
 
     }
 
     public void setDebug(boolean debug) {
         this. = debug;
     }
 
     private static class LoginConfig extends Configuration {
         private boolean debug;
 
         public LoginConfig(boolean debug) {
             super();
             this. = debug;
         }
 
         @Override
         public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
             HashMap<StringStringoptions = new HashMap<StringString>();
             options.put("storeKey""true");
             if () {
                 options.put("debug""true");
             }
 
             return new AppConfigurationEntry[] { new AppConfigurationEntry("com.sun.security.auth.module.Krb5LoginModule",
                     ..options), };
         }
 
     }
 
     private static class KerberosClientCallbackHandler implements CallbackHandler {
         private String username;
        private String password;
        public KerberosClientCallbackHandler(String usernameString password) {
            this. = username;
            this. = password;
        }
        public void handle(Callback[] callbacksthrows IOExceptionUnsupportedCallbackException {
            for (Callback callback : callbacks) {
                if (callback instanceof NameCallback) {
                    NameCallback ncb = (NameCallbackcallback;
                    ncb.setName();
                } else if (callback instanceof PasswordCallback) {
                    PasswordCallback pwcb = (PasswordCallbackcallback;
                    pwcb.setPassword(.toCharArray());
                } else {
                    throw new UnsupportedCallbackException(callback"We got a " + callback.getClass().getCanonicalName()
                            + ", but only NameCallback and PasswordCallback is supported");
                }
            }
        }
    }
New to GrepCode? Check out our FAQ X