⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wikisession.java

📁 jspwiki source code,jspwiki source code
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*    JSPWiki - a JSP-based WikiWiki clone.    Licensed to the Apache Software Foundation (ASF) under one    or more contributor license agreements.  See the NOTICE file    distributed with this work for additional information    regarding copyright ownership.  The ASF licenses this file    to you 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 com.ecyrd.jspwiki;import java.security.AccessControlException;import java.security.Principal;import java.security.PrivilegedAction;import java.util.*;import javax.security.auth.Subject;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import org.apache.log4j.Logger;import com.ecyrd.jspwiki.auth.*;import com.ecyrd.jspwiki.auth.authorize.Group;import com.ecyrd.jspwiki.auth.authorize.GroupManager;import com.ecyrd.jspwiki.auth.authorize.Role;import com.ecyrd.jspwiki.auth.user.UserDatabase;import com.ecyrd.jspwiki.auth.user.UserProfile;import com.ecyrd.jspwiki.event.WikiEvent;import com.ecyrd.jspwiki.event.WikiEventListener;import com.ecyrd.jspwiki.event.WikiSecurityEvent;/** * <p>Represents a long-running wiki session, with an associated user Principal, * user Subject, and authentication status. This class is initialized with * minimal, default-deny values: authentication is set to <code>false</code>, * and the user principal is set to <code>null</code>.</p> * <p>The WikiSession class allows callers to:</p> * <ul> *   <li>Obtain the authentication status of the user via *     {@link #isAnonymous()} and {@link #isAuthenticated()}</li> *   <li>Query the session for Principals representing the *     user's identity via {@link #getLoginPrincipal()}, *     {@link #getUserPrincipal()} and {@link #getPrincipals()}</li> *   <li>Store, retrieve and clear UI messages via *     {@link #addMessage(String)}, {@link #getMessages(String)} *     and {@link #clearMessages(String)}</li> * </ul> * <p>To keep track of the Principals each user posseses, each WikiSession * stores a JAAS Subject. Various login processes add or remove Principals * when users authenticate or log out.</p> * <p>WikiSession implements the {@link com.ecyrd.jspwiki.event.WikiEventListener} * interface and listens for group add/change/delete events fired by * event sources the WikiSession is registered with. Normally, * {@link com.ecyrd.jspwiki.auth.AuthenticationManager} registers each WikiSession * with the {@link com.ecyrd.jspwiki.auth.authorize.GroupManager} * so it can catch group events. Thus, when a user is added to a * {@link com.ecyrd.jspwiki.auth.authorize.Group}, a corresponding * {@link com.ecyrd.jspwiki.auth.GroupPrincipal} is injected into * the Subject's Principal set. Likewise, when the user is removed from * the Group or the Group is deleted, the GroupPrincipal is removed * from the Subject. The effect that this strategy produces is extremely * beneficial: when someone adds a user to a wiki group, that user * <em>immediately</em> gains the privileges associated with that * group; he or she does not need to re-authenticate. * </p> * <p>In addition to methods for examining individual <code>WikiSession</code> * objects, this class also contains a number of static methods for * managing WikiSessions for an entire wiki. These methods allow callers * to find, query and remove WikiSession objects, and * to obtain a list of the current wiki session users.</p> * <p>WikiSession encloses a protected static class, {@link SessionMonitor}, * to keep track of WikiSessions registered with each wiki.</p> * @author Andrew R. Jaquith */public final class WikiSession implements WikiEventListener{    /** An anonymous user's session status. */    public static final String  ANONYMOUS             = "anonymous";    /** An asserted user's session status. */    public static final String  ASSERTED              = "asserted";    /** An authenticated user's session status. */    public static final String  AUTHENTICATED         = "authenticated";    private static final int    ONE                   = 48;    private static final int    NINE                  = 57;    private static final int    DOT                   = 46;    private static final Logger log                   = Logger.getLogger( WikiSession.class );    private static final String ALL                   = "*";    private static ThreadLocal<WikiSession> c_guestSession = new ThreadLocal<WikiSession>();    private final Subject       m_subject             = new Subject();    private final Map<String,Set<String>> m_messages  = new HashMap<String,Set<String>>();    /** The WikiEngine that created this session. */    private WikiEngine          m_engine              = null;    private String              m_status              = ANONYMOUS;    private Principal           m_userPrincipal       = WikiPrincipal.GUEST;    private Principal           m_loginPrincipal      = WikiPrincipal.GUEST;    private Locale              m_cachedLocale        = Locale.getDefault();    /**     * Returns <code>true</code> if one of this WikiSession's user Principals     * can be shown to belong to a particular wiki group. If the user is     * not authenticated, this method will always return <code>false</code>.     * @param group the group to test     * @return the result     */    protected final boolean isInGroup( Group group )    {        for ( Principal principal : getPrincipals() )        {          if ( isAuthenticated() && group.isMember( principal ) )          {              return true;          }        }        return false;    }    /**     * Private constructor to prevent WikiSession from being instantiated     * directly.     */    private WikiSession()    {    }    /**     * Returns <code>true</code> if the user is considered asserted via     * a session cookie; that is, the Subject contains the Principal     * Role.ASSERTED.     * @return Returns <code>true</code> if the user is asserted     */    public final boolean isAsserted()    {        return m_subject.getPrincipals().contains( Role.ASSERTED );    }    /**     * Returns the authentication status of the user's session. The user is     * considered authenticated if the Subject contains the Principal     * Role.AUTHENTICATED. If this method determines that an earlier     * LoginModule did not inject Role.AUTHENTICATED, it will inject one     * if the user is not anonymous <em>and</em> not asserted.     * @return Returns <code>true</code> if the user is authenticated     */    public final boolean isAuthenticated()    {        // If Role.AUTHENTICATED is in principals set, always return true.        if ( m_subject.getPrincipals().contains( Role.AUTHENTICATED ) )        {            return true;        }        // With non-JSPWiki LoginModules, the role may not be there, so        // we need to add it if the user really is authenticated.        if ( !isAnonymous() && !isAsserted() )        {            // Inject AUTHENTICATED role            m_subject.getPrincipals().add( Role.AUTHENTICATED );            return true;        }        return false;    }    /**     * <p>Determines whether the current session is anonymous. This will be     * true if any of these conditions are true:</p>     * <ul>     *   <li>The session's Principal set contains     *       {@link com.ecyrd.jspwiki.auth.authorize.Role#ANONYMOUS}</li>     *   <li>The session's Principal set contains     *       {@link com.ecyrd.jspwiki.auth.WikiPrincipal#GUEST}</li>     *   <li>The Principal returned by {@link #getUserPrincipal()} evaluates     *       to an IP address.</li>     * </ul>     * <p>The criteria above are listed in the order in which they are     * evaluated.</p>     * @return whether the current user's identity is equivalent to an IP     * address     */    public final boolean isAnonymous()    {        Set<Principal> principals = m_subject.getPrincipals();        return principals.contains( Role.ANONYMOUS ) ||                 principals.contains( WikiPrincipal.GUEST ) ||                 isIPV4Address( getUserPrincipal().getName() );    }    /**     * <p> Returns the Principal used to log in to an authenticated session. The     * login principal is determined by examining the Subject's Principal set     * for PrincipalWrappers or WikiPrincipals with type designator     * <code>LOGIN_NAME</code>; the first one found is the login principal.     * If one is not found, this method returns the first principal that isn't     * of type Role or GroupPrincipal. If neither of these conditions hold, this method returns     * {@link com.ecyrd.jspwiki.auth.WikiPrincipal#GUEST}.     * @return the login Principal. If it is a PrincipalWrapper containing an     * externally-provided Principal, the object returned is the Principal, not     * the wrapper around it.     */    public final Principal getLoginPrincipal()    {        return m_loginPrincipal;    }    /**     * <p>Returns the primary user Principal associated with this session. The     * primary user principal is determined as follows:</p> <ol> <li>If the     * Subject's Principal set contains WikiPrincipals, the first WikiPrincipal     * with type designator <code>WIKI_NAME</code> or (alternatively)     * <code>FULL_NAME</code> is the primary Principal.</li>     *   <li>For all other cases, the first Principal in the Subject's principal     *       collection that that isn't of type Role or GroupPrincipal is the primary.</li>     * </ol>     * If no primary user Principal is found, this method returns     * {@link com.ecyrd.jspwiki.auth.WikiPrincipal#GUEST}.     * @return the primary user Principal     */    public final Principal getUserPrincipal()    {        return m_userPrincipal;    }    /**     *  Returns a cached Locale object for this user.  It's better to use     *  WikiContext's corresponding getBundle() method, since that will actually     *  react if the user changes the locale in the middle, but if that's not     *  available (or, for some reason, you need the speed), this method can     *  also be used.  The Locale expires when the WikiSession expires, and     *  currently there is no way to reset the Locale.     *     *  @return A cached Locale object     *  @since 2.5.96     */    public final Locale getLocale()    {        return m_cachedLocale;    }    /**     * Adds a message to the generic list of messages associated with the     * session. These messages retain their order of insertion and remain until     * the {@link #clearMessages()} method is called.     * @param message the message to add; if <code>null</code> it is ignored.     */    public final void addMessage(String message)    {        addMessage( ALL, message );    }    /**     * Adds a message to the specific set of messages associated with the     * session. These messages retain their order of insertion and remain until     * the {@link #clearMessages()} method is called.     * @param topic the topic to associate the message to;     * @param message the message to add     */    public final void addMessage(String topic, String message)    {        if ( topic == null )        {            throw new IllegalArgumentException( "addMessage: topic cannot be null." );        }        if ( message == null )        {            message = "";        }        Set<String> messages = m_messages.get( topic );        if (messages == null )        {            messages = new LinkedHashSet<String>();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -