JAASLoginService.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:3k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: JAASLoginService.java,v 1.2 2005/10/10 18:02:49 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.auth;
  22. import javax.security.auth.callback.Callback;
  23. import javax.security.auth.callback.CallbackHandler;
  24. import javax.security.auth.callback.NameCallback;
  25. import javax.security.auth.callback.PasswordCallback;
  26. import javax.security.auth.login.AccountExpiredException;
  27. import javax.security.auth.login.CredentialExpiredException;
  28. import javax.security.auth.login.FailedLoginException;
  29. import javax.security.auth.login.LoginContext;
  30. import javax.security.auth.login.LoginException;
  31. /**
  32.  * <b>JAASLoginService</b> implements a <b>LoginService</b>
  33.  * that uses JAAS for authentication. <b>JAASLoginService</b> uses the 
  34.  * server name as name of the configuration for JAAS.
  35.  * 
  36.  * @author Bino George
  37.  */
  38. public class JAASLoginService extends LoginService {
  39. /**
  40.  * Constructor for <b>JAASLoginService</b>
  41.  * @param server server name that is also used for the JAAS config name
  42.  */
  43. public JAASLoginService(String server) {
  44. super(server);
  45. }
  46. /**
  47.  * @inheritDoc
  48.  *   
  49.  */
  50. public boolean authenticate(String name, char[] password, String server) {
  51. try {
  52. LoginContext loginContext = null;
  53. loginContext = new LoginContext(getServer(),
  54. new JAASCallbackHandler(name, password));
  55. loginContext.login();
  56. return true;
  57. } catch (AccountExpiredException e) {
  58. // TODO log
  59. e.printStackTrace();
  60. return false;
  61. } catch (CredentialExpiredException e) {
  62. // TODO log
  63. e.printStackTrace();
  64. return false;
  65. } catch (FailedLoginException e) {
  66. // TODO log
  67. e.printStackTrace();
  68. return false;
  69. } catch (LoginException e) {
  70. // TODO log
  71. e.printStackTrace();
  72. return false;
  73. } catch (Throwable e) {
  74. // TODO log
  75. e.printStackTrace();
  76. return false;
  77. }
  78. }
  79. class JAASCallbackHandler implements CallbackHandler {
  80. private String name;
  81. private char[] password;
  82. public JAASCallbackHandler(String name, char[] passwd) {
  83. this.name = name;
  84. this.password = passwd;
  85. }
  86. public void handle(Callback[] callbacks) throws java.io.IOException {
  87. for (int i = 0; i < callbacks.length; i++) {
  88. if (callbacks[i] instanceof NameCallback) {
  89. NameCallback cb = (NameCallback) callbacks[i];
  90. cb.setName(name);
  91. } else if (callbacks[i] instanceof PasswordCallback) {
  92. PasswordCallback cb = (PasswordCallback) callbacks[i];
  93. cb.setPassword(password);
  94. }
  95. }
  96. }
  97. }
  98. }