📄 ldapauthentication.java
字号:
/* * LDAPAuthentication.java * * Version: $Revision: 1.2 $ * * Date: $Date: 2006/03/10 21:16:38 $ * * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts * Institute of Technology. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of the Hewlett-Packard Company nor the name of the * Massachusetts Institute of Technology nor the names of their * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. */package org.dspace.eperson;import java.io.IOException;import java.sql.SQLException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.jsp.PageContext;import javax.servlet.jsp.jstl.fmt.LocaleSupport;import java.util.ArrayList;import java.util.Hashtable;import org.apache.log4j.Logger;import org.dspace.core.ConfigurationManager;import org.dspace.core.Context;import org.dspace.core.LogManager;import org.dspace.core.ConfigurationManager;import org.dspace.eperson.EPerson;import org.dspace.eperson.Group;import org.dspace.eperson.AuthenticationMethod;import org.dspace.eperson.AuthenticationManager;import org.dspace.authorize.AuthorizeException;import javax.naming.directory.*;import javax.naming.*;/** * This is UNTESTED, since I do not have LDAP servers available. * It was adpated from LDAPServlet and should replace it. * See the <code>AuthenticationMethod</code> interface for more details. * * As of August 2005 we need a volunteer to complete and test this * implementation. They should add themselves to the author tag below. * * @author Larry Stone * @version $Revision: 1.2 $ */public class LDAPAuthentication implements AuthenticationMethod { /** log4j category */ private static Logger log = Logger.getLogger(LDAPAuthentication.class); /** * Let a real auth method return true if it wants. */ public boolean canSelfRegister(Context context, HttpServletRequest request, String username) throws SQLException { // XXX might also want to check that username exists in LDAP. return ConfigurationManager.getBooleanProperty("webui.ldap.autoregister"); } /** * Nothing here, initialization is done when auto-registering. */ public void initEPerson(Context context, HttpServletRequest request, EPerson eperson) throws SQLException { // XXX should we try to initialize netid based on email addr, // XXX for eperson created by some other method?? } /** * Cannot change LDAP password through dspace, right? */ public boolean allowSetPassword(Context context, HttpServletRequest request, String username) throws SQLException { // XXX is this right? return false; } /* * This is an explicit method. */ public boolean isImplicit() { return false; } /* * Nothing here. */ public int[] getSpecialGroups(Context context, HttpServletRequest request) { return new int[0]; } /* * MIT policy on certs and groups, so always short-circuit. * * @return One of: * SUCCESS, BAD_CREDENTIALS, CERT_REQUIRED, NO_SUCH_USER, BAD_ARGS */ public int authenticate(Context context, String netid, String password, String realm, HttpServletRequest request) throws SQLException { log.info(LogManager.getHeader(context, "auth", "attempting trivial auth of user="+netid)); // Locate the eperson EPerson eperson = null; try { eperson = EPerson.findByNetid(context, netid.toLowerCase()); } catch (SQLException e) { } boolean loggedIn = false; SpeakerToLDAP ldap = new SpeakerToLDAP(log); // if they entered a netid that matches an eperson if (eperson != null) { // e-mail address corresponds to active account if (eperson.getRequireCertificate()) return CERT_REQUIRED; else if (!eperson.canLogIn()) return BAD_ARGS; { if (ldap.ldapAuthenticate(netid, password, context)) { log.info(LogManager .getHeader(context, "authenticate", "type=ldap")); return SUCCESS; } else return BAD_CREDENTIALS; } } // the user does not already exist so try and authenticate them // with ldap and create an eperson for them else { if (ldap.ldapAuthenticate(netid, password, context)) { // Register the new user automatically log.info(LogManager.getHeader(context, "autoregister", "netid=" + netid)); if ((ldap.ldapEmail!=null)&&(!ldap.ldapEmail.equals(""))) { try { eperson = EPerson.findByEmail(context, ldap.ldapEmail); if (eperson!=null) { log.info(LogManager.getHeader(context, "type=ldap-login", "type=ldap_but_already_email")); context.setIgnoreAuthorization(true); eperson.setNetid(netid); eperson.update(); context.commit(); context.setIgnoreAuthorization(false); context.setCurrentUser(eperson); return SUCCESS; } else { if (canSelfRegister(context, request, netid)) { // TEMPORARILY turn off authorisation try { context.setIgnoreAuthorization(true); eperson = EPerson.create(context);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -