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
  * 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.
Implementation of 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.

Mike Wiesner
 public class SunJaasKerberosClient implements KerberosClient {
     private boolean debug = false;
     private static final Log LOG = LogFactory.getLog(SunJaasKerberosClient.class);
     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.));
             if (.isDebugEnabled()) {
                 .debug("Kerberos authenticated user: "+loginContext.getSubject());
             validatedUsername = loginContext.getSubject().getPrincipals().iterator().next().toString();
         } 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) {
             this. = debug;
         public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
             HashMap<StringStringoptions = new HashMap<StringString>();
             if () {
             return new AppConfigurationEntry[] { new AppConfigurationEntry("",
                     ..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;
                } else if (callback instanceof PasswordCallback) {
                    PasswordCallback pwcb = (PasswordCallbackcallback;
                } 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