You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shiro.apache.org by lh...@apache.org on 2014/06/01 19:33:34 UTC
svn commit: r1599014 [3/4] - in /shiro/branches/2.0-api-design-changes: ./
cache/src/main/java/org/apache/shiro/cache/
core/src/main/java/org/apache/shiro/account/
core/src/main/java/org/apache/shiro/authc/
core/src/main/java/org/apache/shiro/authc/cre...
Added: shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/mgt/ApplicationSecurityManager.java
URL: http://svn.apache.org/viewvc/shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/mgt/ApplicationSecurityManager.java?rev=1599014&view=auto
==============================================================================
--- shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/mgt/ApplicationSecurityManager.java (added)
+++ shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/mgt/ApplicationSecurityManager.java Sun Jun 1 17:33:31 2014
@@ -0,0 +1,810 @@
+/*
+ * 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 org.apache.shiro.mgt;
+
+import org.apache.shiro.account.Account;
+import org.apache.shiro.authc.AuthenticationException;
+import org.apache.shiro.authc.AuthenticationInfo;
+import org.apache.shiro.authc.AuthenticationToken;
+import org.apache.shiro.authc.Authenticator;
+import org.apache.shiro.authc.DefaultAuthenticator;
+import org.apache.shiro.authc.LogoutAware;
+import org.apache.shiro.authz.AuthorizationException;
+import org.apache.shiro.authz.Authorizer;
+import org.apache.shiro.authz.ModularRealmAuthorizer;
+import org.apache.shiro.authz.Permission;
+import org.apache.shiro.cache.CacheManager;
+import org.apache.shiro.cache.CacheManagerAware;
+import org.apache.shiro.cache.DisabledCacheManager;
+import org.apache.shiro.event.EventBus;
+import org.apache.shiro.event.EventBusAware;
+import org.apache.shiro.event.support.DefaultEventBus;
+import org.apache.shiro.realm.Realm;
+import org.apache.shiro.session.InvalidSessionException;
+import org.apache.shiro.session.Session;
+import org.apache.shiro.session.SessionException;
+import org.apache.shiro.session.mgt.DefaultSessionContext;
+import org.apache.shiro.session.mgt.DefaultSessionKey;
+import org.apache.shiro.session.mgt.SessionContext;
+import org.apache.shiro.session.mgt.SessionKey;
+import org.apache.shiro.session.mgt.SessionManager;
+import org.apache.shiro.subject.PrincipalCollection;
+import org.apache.shiro.subject.Subject;
+import org.apache.shiro.subject.SubjectContext;
+import org.apache.shiro.subject.support.DefaultSubjectContext;
+import org.apache.shiro.util.Assert;
+import org.apache.shiro.util.CollectionUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+
+/** @since 2.0 */
+public class ApplicationSecurityManager implements SecurityManager, EventBusAware, CacheManagerAware {
+
+ private static final Logger log = LoggerFactory.getLogger(ApplicationSecurityManager.class);
+
+ /** The EventBus to use to use to publish and receive events of interest during Shiro's operation. Never null. */
+ private EventBus eventBus;
+
+ /**
+ * The CacheManager to use to perform caching operations to enhance performance.
+ * Cannot be null - to disable caching, a DisabledCacheManager should be configured (the default).
+ */
+ private CacheManager cacheManager;
+
+ private Collection<Realm> realms;
+
+ private Authenticator authenticator;
+ private Authorizer authorizer;
+ private SessionManager sessionManager;
+
+ private RememberMeManager rememberMeManager;
+ private SubjectDAO subjectDAO;
+ private SubjectFactory subjectFactory;
+
+ public ApplicationSecurityManager() {
+ this.eventBus = new DefaultEventBus();
+ setCacheManager(new DisabledCacheManager());
+
+ // ===== Authenticator =====
+ DefaultAuthenticator authenticator = new DefaultAuthenticator();
+ authenticator.setEventBus(this.eventBus);
+ setAuthenticator(authenticator);
+
+ // ===== Authorizer =====
+ setAuthorizer(new ModularRealmAuthorizer());
+ }
+
+ /* ===================================================================== *
+ * Getters and Setters *
+ * ===================================================================== */
+
+ private Set<Object> getDependenciesForInjection(Object ignore) {
+ Set<Object> deps = CollectionUtils.asSet(
+ eventBus, cacheManager, realms, authenticator, authorizer, sessionManager,
+ rememberMeManager, subjectDAO, subjectFactory
+ );
+ if (ignore != null) {
+ deps.remove(ignore);
+ }
+ return deps;
+ }
+
+ public Collection<Realm> getRealms() {
+ return realms;
+ }
+
+ public void setRealms(Collection<Realm> realms) {
+ Assert.notEmpty(realms, "Realms argument cannot be empty.");
+ Collection<Realm> immutableRealmsCollection = Collections.unmodifiableCollection(realms);
+ this.realms = immutableRealmsCollection;
+ applyEventBus(this.realms);
+ applyCacheManager(this.realms);
+ Authenticator authc = this.authenticator;
+ if (authc instanceof DefaultAuthenticator) {
+ ((DefaultAuthenticator) authc).setRealms(immutableRealmsCollection);
+ }
+ Authorizer authz = this.authorizer;
+ if (authz instanceof ModularRealmAuthorizer) {
+ ((ModularRealmAuthorizer) authz).setRealms(immutableRealmsCollection);
+ }
+ }
+
+ public EventBus getEventBus() {
+ return eventBus;
+ }
+
+ public void setEventBus(EventBus eventBus) {
+ Assert.notNull(eventBus, "EventBus argument cannot be null.");
+ this.eventBus = eventBus;
+ applyEventBus(getDependenciesForInjection(this.eventBus));
+ }
+
+ private void applyEventBus(Object target) {
+ if (target instanceof Collection) {
+ Collection c = (Collection) target;
+ for (Object o : c) {
+ applyEventBus(o);
+ }
+ }
+ if (target instanceof EventBusAware) {
+ ((EventBusAware) target).setEventBus(this.eventBus);
+ }
+ }
+
+ public CacheManager getCacheManager() {
+ return cacheManager;
+ }
+
+ public void setCacheManager(CacheManager cacheManager) {
+ Assert.notNull(cacheManager, "CacheManager cannot be null. If you want to disable caching, configure a " +
+ DisabledCacheManager.class.getName() + " instance.");
+ this.cacheManager = cacheManager;
+ applyCacheManager(getDependenciesForInjection(this.cacheManager));
+ }
+
+ private void applyCacheManager(Object target) {
+ if (target instanceof Collection) {
+ Collection c = (Collection) target;
+ for (Object o : c) {
+ applyCacheManager(o);
+ }
+ }
+ if (target instanceof CacheManagerAware) {
+ ((CacheManagerAware) target).setCacheManager(this.cacheManager);
+ }
+ }
+
+ public Authenticator getAuthenticator() {
+ return authenticator;
+ }
+
+ public void setAuthenticator(Authenticator authenticator) {
+ Assert.notNull(authenticator, "Authenticator argument cannot be null.");
+ this.authenticator = authenticator;
+ if (this.authenticator instanceof DefaultAuthenticator) {
+ ((DefaultAuthenticator) this.authenticator).setRealms(this.realms);
+ }
+ applyEventBus(this.authenticator);
+ applyCacheManager(this.authenticator);
+ }
+
+ public Authorizer getAuthorizer() {
+ return authorizer;
+ }
+
+ public void setAuthorizer(Authorizer authorizer) {
+ Assert.notNull(authorizer, "Authorizer argument cannot be null.");
+ this.authorizer = authorizer;
+ applyEventBus(this.authorizer);
+ applyCacheManager(this.authorizer);
+ }
+
+ public SessionManager getSessionManager() {
+ return sessionManager;
+ }
+
+ public void setSessionManager(SessionManager sessionManager) {
+ this.sessionManager = sessionManager;
+ }
+
+ /**
+ * Returns the {@code SubjectFactory} responsible for creating {@link Subject} instances exposed to the
+ * application.
+ *
+ * @return the {@code SubjectFactory} responsible for creating {@link Subject} instances exposed to the
+ * application.
+ */
+ public SubjectFactory getSubjectFactory() {
+ return subjectFactory;
+ }
+
+ /**
+ * Sets the {@code SubjectFactory} responsible for creating {@link Subject} instances exposed to the application.
+ *
+ * @param subjectFactory the {@code SubjectFactory} responsible for creating {@link Subject} instances exposed to
+ * the application.
+ */
+ public void setSubjectFactory(SubjectFactory subjectFactory) {
+ this.subjectFactory = subjectFactory;
+ }
+
+ /**
+ * Returns the {@code SubjectDAO} responsible for persisting Subject state, typically used after login or when an
+ * Subject identity is discovered (eg after RememberMe services). Unless configured otherwise, the default
+ * implementation is a {@link DefaultSubjectDAO}.
+ *
+ * @return the {@code SubjectDAO} responsible for persisting Subject state, typically used after login or when an
+ * Subject identity is discovered (eg after RememberMe services).
+ * @see DefaultSubjectDAO
+ */
+ public SubjectDAO getSubjectDAO() {
+ return subjectDAO;
+ }
+
+ /**
+ * Sets the {@code SubjectDAO} responsible for persisting Subject state, typically used after login or when an
+ * Subject identity is discovered (eg after RememberMe services). Unless configured otherwise, the default
+ * implementation is a {@link DefaultSubjectDAO}.
+ *
+ * @param subjectDAO the {@code SubjectDAO} responsible for persisting Subject state, typically used after login or
+ * when an
+ * Subject identity is discovered (eg after RememberMe services).
+ * @see DefaultSubjectDAO
+ */
+ public void setSubjectDAO(SubjectDAO subjectDAO) {
+ this.subjectDAO = subjectDAO;
+ }
+
+ public RememberMeManager getRememberMeManager() {
+ return rememberMeManager;
+ }
+
+ public void setRememberMeManager(RememberMeManager rememberMeManager) {
+ this.rememberMeManager = rememberMeManager;
+ }
+
+ /* ===================================================================== *
+ * Authenticator Methods *
+ * ===================================================================== */
+
+ public AuthenticationInfo authenticate(AuthenticationToken authenticationToken) throws AuthenticationException {
+ return this.authenticator.authenticate(authenticationToken);
+ }
+
+ public Account authenticateAccount(AuthenticationToken authenticationToken) throws AuthenticationException {
+ return this.authenticator.authenticateAccount(authenticationToken);
+ }
+
+ /* ===================================================================== *
+ * Authorizer Methods *
+ * ===================================================================== */
+
+ public boolean isPermitted(PrincipalCollection principals, String permission) {
+ return this.authorizer.isPermitted(principals, permission);
+ }
+
+ public boolean isPermitted(PrincipalCollection principals, Permission permission) {
+ return this.authorizer.isPermitted(principals, permission);
+ }
+
+ public boolean[] isPermitted(PrincipalCollection principals, String... permissions) {
+ return this.authorizer.isPermitted(principals, permissions);
+ }
+
+ public boolean[] isPermitted(PrincipalCollection principals, List<Permission> permissions) {
+ return this.authorizer.isPermitted(principals, permissions);
+ }
+
+ public boolean isPermittedAll(PrincipalCollection principals, String... permissions) {
+ return this.authorizer.isPermittedAll(principals, permissions);
+ }
+
+ public boolean isPermittedAll(PrincipalCollection principals, Collection<Permission> permissions) {
+ return this.authorizer.isPermittedAll(principals, permissions);
+ }
+
+ public void checkPermission(PrincipalCollection principals, String permission) throws AuthorizationException {
+ this.authorizer.checkPermission(principals, permission);
+ }
+
+ public void checkPermission(PrincipalCollection principals, Permission permission) throws AuthorizationException {
+ this.authorizer.checkPermission(principals, permission);
+ }
+
+ public void checkPermissions(PrincipalCollection principals, String... permissions) throws AuthorizationException {
+ this.authorizer.checkPermissions(principals, permissions);
+ }
+
+ public void checkPermissions(PrincipalCollection principals, Collection<Permission> permissions)
+ throws AuthorizationException {
+ this.authorizer.checkPermissions(principals, permissions);
+ }
+
+ public boolean hasRole(PrincipalCollection principals, String roleIdentifier) {
+ return this.authorizer.hasRole(principals, roleIdentifier);
+ }
+
+ public boolean[] hasRoles(PrincipalCollection principals, List<String> roleIdentifiers) {
+ return this.authorizer.hasRoles(principals, roleIdentifiers);
+ }
+
+ public boolean hasAllRoles(PrincipalCollection principals, Collection<String> roleIdentifiers) {
+ return this.authorizer.hasAllRoles(principals, roleIdentifiers);
+ }
+
+ public void checkRole(PrincipalCollection principals, String roleIdentifier) throws AuthorizationException {
+ this.authorizer.checkRole(principals, roleIdentifier);
+ }
+
+ public void checkRoles(PrincipalCollection principals, Collection<String> roleIdentifiers)
+ throws AuthorizationException {
+ this.authorizer.checkRoles(principals, roleIdentifiers);
+ }
+
+ public void checkRoles(PrincipalCollection principals, String... roleIdentifiers)
+ throws AuthorizationException {
+ this.authorizer.checkRoles(principals, roleIdentifiers);
+ }
+
+ /* ===================================================================== *
+ * SessionManager Methods *
+ * ===================================================================== */
+
+ public Session start(SessionContext context) {
+ return this.sessionManager.start(context);
+ }
+
+ public Session getSession(SessionKey key) throws SessionException {
+ return this.sessionManager.getSession(key);
+ }
+
+ /* ===================================================================== *
+ * SecurityManager Methods *
+ * ===================================================================== */
+
+ protected SubjectContext createSubjectContext() {
+ return new DefaultSubjectContext();
+ }
+
+ /**
+ * Creates a {@code Subject} instance for the user represented by the given method arguments.
+ *
+ * @param token the {@code AuthenticationToken} submitted for the successful authentication.
+ * @param info the {@code AuthenticationInfo} of a newly authenticated user.
+ * @param existing the existing {@code Subject} instance that initiated the authentication attempt
+ * @return the {@code Subject} instance that represents the context and session data for the newly
+ * authenticated subject.
+ */
+ protected Subject createSubject(AuthenticationToken token, AuthenticationInfo info, Subject existing) {
+ SubjectContext context = createSubjectContext();
+ context.setAuthenticated(true);
+ context.setAuthenticationToken(token);
+ context.setAuthenticationInfo(info);
+ if (existing != null) {
+ context.setSubject(existing);
+ }
+ return createSubject(context);
+ }
+
+ /**
+ * Binds a {@code Subject} instance created after authentication to the application for later use.
+ * <p/>
+ * As of Shiro 1.2, this method has been deprecated in favor of {@link #save(org.apache.shiro.subject.Subject)},
+ * which this implementation now calls.
+ *
+ * @param subject the {@code Subject} instance created after authentication to be bound to the application
+ * for later use.
+ * @see #save(org.apache.shiro.subject.Subject)
+ * @deprecated in favor of {@link #save(org.apache.shiro.subject.Subject) save(subject)}.
+ */
+ @Deprecated
+ protected void bind(Subject subject) {
+ save(subject);
+ }
+
+ protected void rememberMeSuccessfulLogin(AuthenticationToken token, AuthenticationInfo info, Subject subject) {
+ RememberMeManager rmm = getRememberMeManager();
+ if (rmm != null) {
+ try {
+ rmm.onSuccessfulLogin(subject, token, info);
+ } catch (Exception e) {
+ if (log.isWarnEnabled()) {
+ String msg = "Delegate RememberMeManager instance of type [" + rmm.getClass().getName() +
+ "] threw an exception during onSuccessfulLogin. RememberMe services will not be " +
+ "performed for account [" + info + "].";
+ log.warn(msg, e);
+ }
+ }
+ } else {
+ if (log.isTraceEnabled()) {
+ log.trace("This " + getClass().getName() + " instance does not have a " +
+ "[" + RememberMeManager.class.getName() + "] instance configured. RememberMe services " +
+ "will not be performed for account [" + info + "].");
+ }
+ }
+ }
+
+ protected void rememberMeFailedLogin(AuthenticationToken token, AuthenticationException ex, Subject subject) {
+ RememberMeManager rmm = getRememberMeManager();
+ if (rmm != null) {
+ try {
+ rmm.onFailedLogin(subject, token, ex);
+ } catch (Exception e) {
+ if (log.isWarnEnabled()) {
+ String msg = "Delegate RememberMeManager instance of type [" + rmm.getClass().getName() +
+ "] threw an exception during onFailedLogin for AuthenticationToken [" +
+ token + "].";
+ log.warn(msg, e);
+ }
+ }
+ }
+ }
+
+ protected void rememberMeLogout(Subject subject) {
+ RememberMeManager rmm = getRememberMeManager();
+ if (rmm != null) {
+ try {
+ rmm.onLogout(subject);
+ } catch (Exception e) {
+ if (log.isWarnEnabled()) {
+ String msg = "Delegate RememberMeManager instance of type [" + rmm.getClass().getName() +
+ "] threw an exception during onLogout for subject with principals [" +
+ (subject != null ? subject.getPrincipals() : null) + "]";
+ log.warn(msg, e);
+ }
+ }
+ }
+ }
+
+ /**
+ * First authenticates the {@code AuthenticationToken} argument, and if successful, constructs a
+ * {@code Subject} instance representing the authenticated account's identity.
+ * <p/>
+ * Once constructed, the {@code Subject} instance is then {@link #bind bound} to the application for
+ * subsequent access before being returned to the caller.
+ *
+ * @param token the authenticationToken to process for the login attempt.
+ * @return a Subject representing the authenticated user.
+ * @throws AuthenticationException if there is a problem authenticating the specified {@code token}.
+ */
+ public Subject login(Subject subject, AuthenticationToken token) throws AuthenticationException {
+ AuthenticationInfo info;
+ try {
+ info = authenticate(token);
+ } catch (AuthenticationException ae) {
+ try {
+ onFailedLogin(token, ae, subject);
+ } catch (Exception e) {
+ if (log.isInfoEnabled()) {
+ log.info("onFailedLogin method threw an " +
+ "exception. Logging and propagating original AuthenticationException.", e);
+ }
+ }
+ throw ae; //propagate
+ }
+
+ Subject loggedIn = createSubject(token, info, subject);
+
+ onSuccessfulLogin(token, info, loggedIn);
+
+ return loggedIn;
+ }
+
+ protected void onSuccessfulLogin(AuthenticationToken token, AuthenticationInfo info, Subject subject) {
+ rememberMeSuccessfulLogin(token, info, subject);
+ }
+
+ protected void onFailedLogin(AuthenticationToken token, AuthenticationException ae, Subject subject) {
+ rememberMeFailedLogin(token, ae, subject);
+ }
+
+ protected void beforeLogout(Subject subject) {
+ rememberMeLogout(subject);
+ }
+
+ protected SubjectContext copy(SubjectContext subjectContext) {
+ return new DefaultSubjectContext(subjectContext);
+ }
+
+ /**
+ * This implementation functions as follows:
+ * <p/>
+ * <ol>
+ * <li>Ensures the {@code SubjectContext} is as populated as it can be, using heuristics to acquire
+ * data that may not have already been available to it (such as a referenced session or remembered
+ * principals).</li>
+ * <li>Calls {@link #doCreateSubject(org.apache.shiro.subject.SubjectContext)} to actually perform the
+ * {@code Subject} instance creation.</li>
+ * <li>calls {@link #save(org.apache.shiro.subject.Subject) save(subject)} to ensure the constructed
+ * {@code Subject}'s state is accessible for future requests/invocations if necessary.</li>
+ * <li>returns the constructed {@code Subject} instance.</li>
+ * </ol>
+ *
+ * @param subjectContext any data needed to direct how the Subject should be constructed.
+ * @return the {@code Subject} instance reflecting the specified contextual data.
+ * @see #ensureSecurityManager(org.apache.shiro.subject.SubjectContext)
+ * @see #resolveSession(org.apache.shiro.subject.SubjectContext)
+ * @see #resolvePrincipals(org.apache.shiro.subject.SubjectContext)
+ * @see #doCreateSubject(org.apache.shiro.subject.SubjectContext)
+ * @see #save(org.apache.shiro.subject.Subject)
+ * @since 1.0
+ */
+ public Subject createSubject(SubjectContext subjectContext) {
+ //create a copy so we don't modify the argument's backing map:
+ SubjectContext context = copy(subjectContext);
+
+ //ensure that the context has a SecurityManager instance, and if not, add one:
+ context = ensureSecurityManager(context);
+
+ //Resolve an associated Session (usually based on a referenced session ID), and place it in the context before
+ //sending to the SubjectFactory. The SubjectFactory should not need to know how to acquire sessions as the
+ //process is often environment specific - better to shield the SF from these details:
+ context = resolveSession(context);
+
+ //Similarly, the SubjectFactory should not require any concept of RememberMe - translate that here first
+ //if possible before handing off to the SubjectFactory:
+ context = resolvePrincipals(context);
+
+ Subject subject = doCreateSubject(context);
+
+ //save this subject for future reference if necessary:
+ //(this is needed here in case rememberMe principals were resolved and they need to be stored in the
+ //session, so we don't constantly rehydrate the rememberMe PrincipalCollection on every operation).
+ //Added in 1.2:
+ save(subject);
+
+ return subject;
+ }
+
+ /**
+ * Actually creates a {@code Subject} instance by delegating to the internal
+ * {@link #getSubjectFactory() subjectFactory}. By the time this method is invoked, all possible
+ * {@code SubjectContext} data (session, principals, et. al.) has been made accessible using all known heuristics
+ * and will be accessible to the {@code subjectFactory} via the {@code subjectContext.resolve*} methods.
+ *
+ * @param context the populated context (data map) to be used by the {@code SubjectFactory} when creating a
+ * {@code Subject} instance.
+ * @return a {@code Subject} instance reflecting the data in the specified {@code SubjectContext} data map.
+ * @see #getSubjectFactory()
+ * @see SubjectFactory#createSubject(org.apache.shiro.subject.SubjectContext)
+ * @since 1.2
+ */
+ protected Subject doCreateSubject(SubjectContext context) {
+ return getSubjectFactory().createSubject(context);
+ }
+
+ /**
+ * Saves the subject's state to a persistent location for future reference if necessary.
+ * <p/>
+ * This implementation merely delegates to the internal {@link #setSubjectDAO(SubjectDAO) subjectDAO} and calls
+ * {@link SubjectDAO#save(org.apache.shiro.subject.Subject) subjectDAO.save(subject)}.
+ *
+ * @param subject the subject for which state will potentially be persisted
+ * @see SubjectDAO#save(org.apache.shiro.subject.Subject)
+ * @since 1.2
+ */
+ protected void save(Subject subject) {
+ this.subjectDAO.save(subject);
+ }
+
+ /**
+ * Removes (or 'unbinds') the Subject's state from the application, typically called during {@link #logout}..
+ * <p/>
+ * This implementation merely delegates to the internal {@link #setSubjectDAO(SubjectDAO) subjectDAO} and calls
+ * {@link SubjectDAO#delete(org.apache.shiro.subject.Subject) delete(subject)}.
+ *
+ * @param subject the subject for which state will be removed
+ * @see SubjectDAO#delete(org.apache.shiro.subject.Subject)
+ * @since 1.2
+ */
+ protected void delete(Subject subject) {
+ this.subjectDAO.delete(subject);
+ }
+
+ /**
+ * Determines if there is a {@code SecurityManager} instance in the context, and if not, adds 'this' to the
+ * context. This ensures the SubjectFactory instance will have access to a SecurityManager during Subject
+ * construction if necessary.
+ *
+ * @param context the subject context data that may contain a SecurityManager instance.
+ * @return The SubjectContext to use to pass to a {@link SubjectFactory} for subject creation.
+ * @since 1.0
+ */
+ @SuppressWarnings({"unchecked"})
+ protected SubjectContext ensureSecurityManager(SubjectContext context) {
+ if (context.resolveSecurityManager() != null) {
+ log.trace("Context already contains a SecurityManager instance. Returning.");
+ return context;
+ }
+ log.trace("No SecurityManager found in context. Adding self reference.");
+ context.setSecurityManager(this);
+ return context;
+ }
+
+ /**
+ * Attempts to resolve any associated session based on the context and returns a
+ * context that represents this resolved {@code Session} to ensure it may be referenced if necessary by the
+ * invoked {@link SubjectFactory} that performs actual {@link Subject} construction.
+ * <p/>
+ * If there is a {@code Session} already in the context because that is what the caller wants to be used for
+ * {@code Subject} construction, or if no session is resolved, this method effectively does nothing
+ * returns the context method argument unaltered.
+ *
+ * @param context the subject context data that may resolve a Session instance.
+ * @return The context to use to pass to a {@link SubjectFactory} for subject creation.
+ * @since 1.0
+ */
+ @SuppressWarnings({"unchecked"})
+ protected SubjectContext resolveSession(SubjectContext context) {
+ if (context.resolveSession() != null) {
+ log.debug("Context already contains a session. Returning.");
+ return context;
+ }
+ try {
+ //Context couldn't resolve it directly, let's see if we can since we have direct access to
+ //the session manager:
+ Session session = resolveContextSession(context);
+ if (session != null) {
+ context.setSession(session);
+ }
+ } catch (InvalidSessionException e) {
+ log.debug("Resolved SubjectContext context session is invalid. Ignoring and creating an anonymous " +
+ "(session-less) Subject instance.", e);
+ }
+ return context;
+ }
+
+ protected Session resolveContextSession(SubjectContext context) throws InvalidSessionException {
+ SessionKey key = getSessionKey(context);
+ if (key != null) {
+ return getSession(key);
+ }
+ return null;
+ }
+
+ protected SessionKey getSessionKey(SubjectContext context) {
+ Serializable sessionId = context.getSessionId();
+ if (sessionId != null) {
+ return new DefaultSessionKey(sessionId);
+ }
+ return null;
+ }
+
+ private static boolean isEmpty(PrincipalCollection pc) {
+ return pc == null || pc.isEmpty();
+ }
+
+ /**
+ * Attempts to resolve an identity (a {@link PrincipalCollection}) for the context using heuristics. This
+ * implementation functions as follows:
+ * <ol>
+ * <li>Check the context to see if it can already {@link SubjectContext#resolvePrincipals resolve an identity}. If
+ * so, this method does nothing and returns the method argument unaltered.</li>
+ * <li>Check for a RememberMe identity by calling {@link #getRememberedIdentity}. If that method returns a
+ * non-null value, place the remembered {@link PrincipalCollection} in the context.</li>
+ * </ol>
+ *
+ * @param context the subject context data that may provide (directly or indirectly through one of its values) a
+ * {@link PrincipalCollection} identity.
+ * @return The Subject context to use to pass to a {@link SubjectFactory} for subject creation.
+ * @since 1.0
+ */
+ @SuppressWarnings({"unchecked"})
+ protected SubjectContext resolvePrincipals(SubjectContext context) {
+
+ PrincipalCollection principals = context.resolvePrincipals();
+
+ if (isEmpty(principals)) {
+ log.trace("No identity (PrincipalCollection) found in the context. Looking for a remembered identity.");
+
+ principals = getRememberedIdentity(context);
+
+ if (!isEmpty(principals)) {
+ log.debug("Found remembered PrincipalCollection. Adding to the context to be used " +
+ "for subject construction by the SubjectFactory.");
+
+ context.setPrincipals(principals);
+
+ // The following call was removed (commented out) in Shiro 1.2 because it uses the session as an
+ // implementation strategy. Session use for Shiro's own needs should be controlled in a single place
+ // to be more manageable for end-users: there are a number of stateless (e.g. REST) applications that
+ // use Shiro that need to ensure that sessions are only used when desirable. If Shiro's internal
+ // implementations used Subject sessions (setting attributes) whenever we wanted, it would be much
+ // harder for end-users to control when/where that occurs.
+ //
+ // Because of this, the SubjectDAO was created as the single point of control, and session state logic
+ // has been moved to the DefaultSubjectDAO implementation.
+
+ // Removed in Shiro 1.2. SHIRO-157 is still satisfied by the new DefaultSubjectDAO implementation
+ // introduced in 1.2
+ // Satisfies SHIRO-157:
+ // bindPrincipalsToSession(principals, context);
+
+ } else {
+ log.trace("No remembered identity found. Returning original context.");
+ }
+ }
+
+ return context;
+ }
+
+ protected SessionContext createSessionContext(SubjectContext subjectContext) {
+ DefaultSessionContext sessionContext = new DefaultSessionContext();
+ if (!CollectionUtils.isEmpty(subjectContext)) {
+ sessionContext.putAll(subjectContext);
+ }
+ Serializable sessionId = subjectContext.getSessionId();
+ if (sessionId != null) {
+ sessionContext.setSessionId(sessionId);
+ }
+ String host = subjectContext.resolveHost();
+ if (host != null) {
+ sessionContext.setHost(host);
+ }
+ return sessionContext;
+ }
+
+ public void logout(Subject subject) {
+
+ if (subject == null) {
+ throw new IllegalArgumentException("Subject method argument cannot be null.");
+ }
+
+ beforeLogout(subject);
+
+ PrincipalCollection principals = subject.getPrincipals();
+ if (principals != null && !principals.isEmpty()) {
+ if (log.isDebugEnabled()) {
+ log.debug("Logging out subject with primary principal {}", principals.getPrimaryPrincipal());
+ }
+ Authenticator authc = getAuthenticator();
+ if (authc instanceof LogoutAware) {
+ ((LogoutAware) authc).onLogout(principals);
+ }
+ }
+
+ try {
+ delete(subject);
+ } catch (Exception e) {
+ if (log.isDebugEnabled()) {
+ String msg = "Unable to cleanly unbind Subject. Ignoring (logging out).";
+ log.debug(msg, e);
+ }
+ } finally {
+ try {
+ stopSession(subject);
+ } catch (Exception e) {
+ if (log.isDebugEnabled()) {
+ String msg = "Unable to cleanly stop Session for Subject [" + subject.getPrincipal() + "] " +
+ "Ignoring (logging out).";
+ log.debug(msg, e);
+ }
+ }
+ }
+ }
+
+ protected void stopSession(Subject subject) {
+ Session s = subject.getSession(false);
+ if (s != null) {
+ s.stop();
+ }
+ }
+
+ protected PrincipalCollection getRememberedIdentity(SubjectContext subjectContext) {
+ RememberMeManager rmm = getRememberMeManager();
+ if (rmm != null) {
+ try {
+ return rmm.getRememberedPrincipals(subjectContext);
+ } catch (Exception e) {
+ if (log.isWarnEnabled()) {
+ String msg = "Delegate RememberMeManager instance of type [" + rmm.getClass().getName() +
+ "] threw an exception during getRememberedPrincipals().";
+ log.warn(msg, e);
+ }
+ }
+ }
+ return null;
+ }
+}
Modified: shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/mgt/AuthenticatingSecurityManager.java
URL: http://svn.apache.org/viewvc/shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/mgt/AuthenticatingSecurityManager.java?rev=1599014&r1=1599013&r2=1599014&view=diff
==============================================================================
--- shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/mgt/AuthenticatingSecurityManager.java (original)
+++ shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/mgt/AuthenticatingSecurityManager.java Sun Jun 1 17:33:31 2014
@@ -18,6 +18,7 @@
*/
package org.apache.shiro.mgt;
+import org.apache.shiro.account.Account;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
@@ -39,7 +40,9 @@ import org.apache.shiro.util.LifecycleUt
* possible, suitable default instances for all dependencies are created upon instantiation.
*
* @since 0.9
+ * @deprecated in 2.0 in favor of the simpler {@link ApplicationSecurityManager} implementation.
*/
+@Deprecated
public abstract class AuthenticatingSecurityManager extends RealmSecurityManager {
/**
@@ -102,10 +105,15 @@ public abstract class AuthenticatingSecu
/**
* Delegates to the wrapped {@link org.apache.shiro.authc.Authenticator Authenticator} for authentication.
*/
+ @Deprecated
public AuthenticationInfo authenticate(AuthenticationToken token) throws AuthenticationException {
return this.authenticator.authenticate(token);
}
+ public Account authenticateAccount(AuthenticationToken authenticationToken) throws AuthenticationException {
+ return this.authenticator.authenticateAccount(authenticationToken);
+ }
+
public void destroy() {
LifecycleUtils.destroy(getAuthenticator());
this.authenticator = null;
Modified: shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/mgt/AuthorizingSecurityManager.java
URL: http://svn.apache.org/viewvc/shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/mgt/AuthorizingSecurityManager.java?rev=1599014&r1=1599013&r2=1599014&view=diff
==============================================================================
--- shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/mgt/AuthorizingSecurityManager.java (original)
+++ shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/mgt/AuthorizingSecurityManager.java Sun Jun 1 17:33:31 2014
@@ -43,7 +43,9 @@ import java.util.List;
* possible, suitable default instances for all dependencies will be created upon instantiation.
*
* @since 0.9
+ * @deprecated in 2.0 in favor of the simpler {@link ApplicationSecurityManager} implementation.
*/
+@Deprecated
public abstract class AuthorizingSecurityManager extends AuthenticatingSecurityManager {
/**
Modified: shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/mgt/CachingSecurityManager.java
URL: http://svn.apache.org/viewvc/shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/mgt/CachingSecurityManager.java?rev=1599014&r1=1599013&r2=1599014&view=diff
==============================================================================
--- shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/mgt/CachingSecurityManager.java (original)
+++ shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/mgt/CachingSecurityManager.java Sun Jun 1 17:33:31 2014
@@ -36,7 +36,9 @@ import org.apache.shiro.util.LifecycleUt
* instance must be explicitly configured if caching across the framework is to be enabled.
*
* @since 0.9
+ * @deprecated in 2.0 in favor of the simpler {@link ApplicationSecurityManager} implementation.
*/
+@Deprecated
public abstract class CachingSecurityManager implements SecurityManager, Destroyable, CacheManagerAware, EventBusAware {
/**
Modified: shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/mgt/RealmSecurityManager.java
URL: http://svn.apache.org/viewvc/shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/mgt/RealmSecurityManager.java?rev=1599014&r1=1599013&r2=1599014&view=diff
==============================================================================
--- shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/mgt/RealmSecurityManager.java (original)
+++ shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/mgt/RealmSecurityManager.java Sun Jun 1 17:33:31 2014
@@ -35,7 +35,9 @@ import java.util.Collection;
* subclasses.
*
* @since 0.9
+ * @deprecated in 2.0 in favor of the simpler {@link ApplicationSecurityManager} implementation.
*/
+@Deprecated
public abstract class RealmSecurityManager extends CachingSecurityManager {
/**
Modified: shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/mgt/SessionsSecurityManager.java
URL: http://svn.apache.org/viewvc/shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/mgt/SessionsSecurityManager.java?rev=1599014&r1=1599013&r2=1599014&view=diff
==============================================================================
--- shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/mgt/SessionsSecurityManager.java (original)
+++ shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/mgt/SessionsSecurityManager.java Sun Jun 1 17:33:31 2014
@@ -45,7 +45,9 @@ import org.apache.shiro.util.LifecycleUt
* possible, suitable default instances for all dependencies will be created upon instantiation.
*
* @since 0.9
+ * @deprecated in 2.0 in favor of the simpler {@link ApplicationSecurityManager} implementation.
*/
+@Deprecated
public abstract class SessionsSecurityManager extends AuthorizingSecurityManager {
/**
Added: shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AbstractCacheHandler.java
URL: http://svn.apache.org/viewvc/shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AbstractCacheHandler.java?rev=1599014&view=auto
==============================================================================
--- shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AbstractCacheHandler.java (added)
+++ shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AbstractCacheHandler.java Sun Jun 1 17:33:31 2014
@@ -0,0 +1,42 @@
+/*
+ * 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 org.apache.shiro.realm;
+
+import org.apache.shiro.cache.CacheManager;
+import org.apache.shiro.cache.DisabledCacheManager;
+
+/**
+ * @since 2.0
+ */
+public abstract class AbstractCacheHandler {
+
+ private CacheManager cacheManager;
+
+ public AbstractCacheHandler() {
+ this.cacheManager = DisabledCacheManager.INSTANCE;
+ }
+
+ public CacheManager getCacheManager() {
+ return cacheManager;
+ }
+
+ public void setCacheManager(CacheManager cacheManager) {
+ this.cacheManager = cacheManager;
+ }
+}
Added: shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountCacheHandler.java
URL: http://svn.apache.org/viewvc/shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountCacheHandler.java?rev=1599014&view=auto
==============================================================================
--- shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountCacheHandler.java (added)
+++ shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountCacheHandler.java Sun Jun 1 17:33:31 2014
@@ -0,0 +1,36 @@
+/*
+ * 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 org.apache.shiro.realm;
+
+import org.apache.shiro.account.Account;
+import org.apache.shiro.account.AccountId;
+import org.apache.shiro.authc.AuthenticationToken;
+
+/**
+ * @since 2.0
+ */
+public interface AccountCacheHandler {
+
+ Account getCachedAccount(AuthenticationToken token);
+
+ void cacheAccount(AuthenticationToken token, Account account);
+
+ void clearCachedAccount(AccountId id);
+
+}
Added: shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountCacheKeyResolver.java
URL: http://svn.apache.org/viewvc/shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountCacheKeyResolver.java?rev=1599014&view=auto
==============================================================================
--- shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountCacheKeyResolver.java (added)
+++ shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountCacheKeyResolver.java Sun Jun 1 17:33:31 2014
@@ -0,0 +1,35 @@
+/*
+ * 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 org.apache.shiro.realm;
+
+import org.apache.shiro.account.Account;
+import org.apache.shiro.account.AccountId;
+import org.apache.shiro.authc.AuthenticationToken;
+
+/**
+ * @since 2.0
+ */
+public interface AccountCacheKeyResolver {
+
+ Object getAccountCacheKey(AuthenticationToken token);
+
+ Object getAccountCacheKey(AuthenticationToken token, Account account);
+
+ Object getAccountCacheKey(AccountId id);
+}
Added: shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountCacheResolver.java
URL: http://svn.apache.org/viewvc/shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountCacheResolver.java?rev=1599014&view=auto
==============================================================================
--- shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountCacheResolver.java (added)
+++ shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountCacheResolver.java Sun Jun 1 17:33:31 2014
@@ -0,0 +1,36 @@
+/*
+ * 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 org.apache.shiro.realm;
+
+import org.apache.shiro.account.Account;
+import org.apache.shiro.account.AccountId;
+import org.apache.shiro.authc.AuthenticationToken;
+import org.apache.shiro.cache.Cache;
+
+/**
+ * @since 2.0
+ */
+public interface AccountCacheResolver {
+
+ Cache<Object,Account> getAccountCache(AuthenticationToken token);
+
+ Cache<Object,Account> getAccountCache(AuthenticationToken token, Account account);
+
+ Cache<Object,Account> getAccountCache(AccountId id);
+}
Added: shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountPermissionResolver.java
URL: http://svn.apache.org/viewvc/shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountPermissionResolver.java?rev=1599014&view=auto
==============================================================================
--- shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountPermissionResolver.java (added)
+++ shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountPermissionResolver.java Sun Jun 1 17:33:31 2014
@@ -0,0 +1,25 @@
+/*
+ * 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 org.apache.shiro.realm;
+
+/**
+ * @since 2.0
+ */
+public interface AccountPermissionResolver {
+}
Added: shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountRolePermissionResolver.java
URL: http://svn.apache.org/viewvc/shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountRolePermissionResolver.java?rev=1599014&view=auto
==============================================================================
--- shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountRolePermissionResolver.java (added)
+++ shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountRolePermissionResolver.java Sun Jun 1 17:33:31 2014
@@ -0,0 +1,40 @@
+/*
+ * 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 org.apache.shiro.realm;
+
+import org.apache.shiro.account.Account;
+import org.apache.shiro.authz.Permission;
+
+import java.util.Collection;
+
+/**
+ * @since 2.0
+ */
+public interface AccountRolePermissionResolver {
+
+ /**
+ * Returns all permissions assigned to the role with the specified {@code roleName}. The role is one of the
+ * account's assigned roles.
+ *
+ * @param account the account assigned the specified role.
+ * @param roleName the name of the role for which permissions will be resolved.
+ * @return all permissions assigned to the role with the specified {@code roleName}.
+ */
+ Collection<Permission> getAccountRolePermissions(Account account, String roleName);
+}
Added: shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountRoleResolver.java
URL: http://svn.apache.org/viewvc/shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountRoleResolver.java?rev=1599014&view=auto
==============================================================================
--- shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountRoleResolver.java (added)
+++ shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountRoleResolver.java Sun Jun 1 17:33:31 2014
@@ -0,0 +1,37 @@
+/*
+ * 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 org.apache.shiro.realm;
+
+import org.apache.shiro.account.Account;
+
+import java.util.Collection;
+
+/**
+ * @since 2.0
+ */
+public interface AccountRoleResolver {
+
+ /**
+ * Returns a collection of the names of all Roles assigned to the specified Account.
+ *
+ * @param account the account for which assigned roles will be found.
+ * @return a collection of the names of all Roles assigned to the specified Account.
+ */
+ Collection<String> getAccountRoleNames(Account account);
+}
Added: shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountStoreRealm.java
URL: http://svn.apache.org/viewvc/shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountStoreRealm.java?rev=1599014&view=auto
==============================================================================
--- shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountStoreRealm.java (added)
+++ shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AccountStoreRealm.java Sun Jun 1 17:33:31 2014
@@ -0,0 +1,156 @@
+/*
+ * 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 org.apache.shiro.realm;
+
+import org.apache.shiro.account.Account;
+import org.apache.shiro.account.AccountStore;
+import org.apache.shiro.authc.AuthenticationException;
+import org.apache.shiro.authc.AuthenticationInfo;
+import org.apache.shiro.authc.AuthenticationToken;
+import org.apache.shiro.authc.IncorrectCredentialsException;
+import org.apache.shiro.authc.UsernamePasswordToken;
+import org.apache.shiro.authc.credential.CredentialsMatcher;
+import org.apache.shiro.authc.credential.PasswordMatcher;
+import org.apache.shiro.util.Assert;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Performs authentication and authorization for accounts in a <em>single</em> account data store. It is expected
+ * that you configure one {@code AccountStoreRealm} for each data store that contains accounts accessible to your
+ * application.
+ *
+ * @since 2.0
+ */
+public class AccountStoreRealm implements Realm {
+
+ private static final Logger log = LoggerFactory.getLogger(AccountStoreRealm.class);
+
+ private String name;
+
+ private AccountStore accountStore;
+
+ //Authentication:
+ private CredentialsMatcher credentialsMatcher;
+ private AccountCacheHandler accountCacheHandler;
+
+ //Authorization:
+ private AuthorizationCacheHandler authorizationCacheHandler;
+ private AccountRoleResolver accountRoleResolver;
+ private AccountRolePermissionResolver accountRolePermissionResolver;
+ private AccountPermissionResolver accountPermissionResolver;
+
+ public AccountStoreRealm() {
+ this.credentialsMatcher = new PasswordMatcher(); //80/20 rule: most Shiro deployments use passwords
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getName() {
+ return this.name;
+ }
+
+ public AccountStore getAccountStore() {
+ return accountStore;
+ }
+
+ public void setAccountStore(AccountStore accountStore) {
+ Assert.notNull(accountStore);
+ this.accountStore = accountStore;
+ }
+
+ public CredentialsMatcher getCredentialsMatcher() {
+ return credentialsMatcher;
+ }
+
+ public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
+ Assert.notNull(credentialsMatcher);
+ this.credentialsMatcher = credentialsMatcher;
+ }
+
+ public AccountCacheHandler getAccountCacheHandler() {
+ return accountCacheHandler;
+ }
+
+ public void setAccountCacheHandler(AccountCacheHandler accountCacheHandler) {
+ Assert.notNull(accountCacheHandler);
+ this.accountCacheHandler = accountCacheHandler;
+ }
+
+ public AuthorizationCacheHandler getAuthorizationCacheHandler() {
+ return authorizationCacheHandler;
+ }
+
+ public void setAuthorizationCacheHandler(AuthorizationCacheHandler authorizationCacheHandler) {
+ Assert.notNull(authorizationCacheHandler);
+ this.authorizationCacheHandler = authorizationCacheHandler;
+ }
+
+ public boolean supports(AuthenticationToken token) {
+ return UsernamePasswordToken.class.isInstance(token);
+ }
+
+ @Deprecated
+ public final AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
+ throw new UnsupportedOperationException("The " + getClass().getName() + " implementation does not support " +
+ "legacy (pre 2.0) authentication behavior. Do not configure this Realm unless you are using " +
+ "a DefaultAuthenticator implementation.");
+ }
+
+ @Deprecated
+ public final AuthenticationInfo authenticate(AuthenticationToken token) throws AuthenticationException {
+ throw new UnsupportedOperationException("The " + getClass().getName() + " implementation does not support " +
+ "legacy (pre 2.0) authentication behavior. Do not configure this Realm unless you are using " +
+ "a DefaultAuthenticator implementation.");
+ }
+
+ public Account authenticateAccount(AuthenticationToken token) throws AuthenticationException {
+ Account account = accountCacheHandler.getCachedAccount(token);
+ if (account == null) {
+ //otherwise not cached, perform the lookup:
+ account = accountStore.getAccount(token);
+ if (token != null && account != null) {
+ log.debug("Acquired Account [{}] from account store", account);
+ accountCacheHandler.cacheAccount(token, account);
+ }
+ } else {
+ log.debug("Using cached account [{}] for credentials matching.", account);
+ }
+
+ if (account == null) {
+ log.debug("No account found for submitted AuthenticationToken [{}]. Returning null.", token);
+ return null;
+ }
+
+ assertCredentialsMatch(token, account);
+
+ return account;
+ }
+
+ protected void assertCredentialsMatch(AuthenticationToken token, Account account) {
+ CredentialsMatcher cm = getCredentialsMatcher();
+ if (!cm.credentialsMatch(token, account)) {
+ //not successful - throw an exception to indicate this:
+ String msg = "Submitted credentials for token [" + token + "] did not match the stored credentials.";
+ throw new IncorrectCredentialsException(msg);
+ }
+ }
+}
Modified: shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AuthenticatingRealm.java
URL: http://svn.apache.org/viewvc/shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AuthenticatingRealm.java?rev=1599014&r1=1599013&r2=1599014&view=diff
==============================================================================
--- shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AuthenticatingRealm.java (original)
+++ shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AuthenticatingRealm.java Sun Jun 1 17:33:31 2014
@@ -18,6 +18,8 @@
*/
package org.apache.shiro.realm;
+import org.apache.shiro.account.Account;
+import org.apache.shiro.account.AccountId;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
@@ -33,9 +35,11 @@ import org.apache.shiro.util.Initializab
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
-
/**
* A top-level abstract implementation of the <tt>Realm</tt> interface that only implements authentication support
* (log-in) operations and leaves authorization (access control) behavior to subclasses.
@@ -109,7 +113,9 @@ import java.util.concurrent.atomic.Atomi
* the subclass implementation.
*
* @since 0.2
+ * @deprecated since 2.0 if favor of the {@link AccountStoreRealm} configured with an {@link org.apache.shiro.account.AccountStore AccountStore}.
*/
+@Deprecated
public abstract class AuthenticatingRealm extends CachingRealm implements Initializable {
//TODO - complete JavaDoc
@@ -562,9 +568,15 @@ public abstract class AuthenticatingReal
* @return the AuthenticationInfo corresponding to the given {@code token}, or {@code null} if no
* AuthenticationInfo could be found.
* @throws AuthenticationException if authentication failed.
+ * @deprecated
*/
+ @Deprecated
public final AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
+ return authenticate(token);
+ }
+ @Deprecated
+ public AuthenticationInfo authenticate(AuthenticationToken token) throws AuthenticationException {
AuthenticationInfo info = getCachedAuthenticationInfo(token);
if (info == null) {
//otherwise not cached, perform the lookup:
@@ -586,6 +598,38 @@ public abstract class AuthenticatingReal
return info;
}
+ public Account authenticateAccount(AuthenticationToken authenticationToken) throws AuthenticationException {
+ final AuthenticationInfo info = authenticate(authenticationToken);
+ final Map<String,Object> attributes = new LinkedHashMap<String,Object>();
+ int i = 0;
+ for(Object principal : info.getPrincipals()) {
+ attributes.put("principal" + i++, principal);
+ }
+
+ return new Account() {
+ public AccountId getId() {
+ return new AccountId() {
+ @Override
+ public String toString() {
+ return info.getPrincipals().getPrimaryPrincipal().toString();
+ }
+ };
+ }
+
+ public Object getCredentials() {
+ return info.getCredentials();
+ }
+
+ public Map<String, Object> getAttributes() {
+ return Collections.unmodifiableMap(attributes);
+ }
+
+ public PrincipalCollection getPrincipals() {
+ return info.getPrincipals();
+ }
+ };
+ }
+
/**
* Asserts that the submitted {@code AuthenticationToken}'s credentials match the stored account
* {@code AuthenticationInfo}'s credentials, and if not, throws an {@link AuthenticationException}.
Added: shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AuthorizationCacheHandler.java
URL: http://svn.apache.org/viewvc/shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AuthorizationCacheHandler.java?rev=1599014&view=auto
==============================================================================
--- shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AuthorizationCacheHandler.java (added)
+++ shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AuthorizationCacheHandler.java Sun Jun 1 17:33:31 2014
@@ -0,0 +1,33 @@
+/*
+ * 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 org.apache.shiro.realm;
+
+import org.apache.shiro.account.AccountId;
+import org.apache.shiro.authz.AuthorizationInfo;
+
+/**
+ * @since 2.0
+ */
+public interface AuthorizationCacheHandler {
+
+ AuthorizationInfo getCachedAuthorizationInfo(AccountId id);
+
+ void cacheAuthorizationInfo(AccountId id, AuthorizationInfo info);
+
+}
Modified: shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AuthorizingRealm.java
URL: http://svn.apache.org/viewvc/shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AuthorizingRealm.java?rev=1599014&r1=1599013&r2=1599014&view=diff
==============================================================================
--- shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AuthorizingRealm.java (original)
+++ shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/AuthorizingRealm.java Sun Jun 1 17:33:31 2014
@@ -19,8 +19,16 @@
package org.apache.shiro.realm;
import org.apache.shiro.authc.credential.CredentialsMatcher;
-import org.apache.shiro.authz.*;
-import org.apache.shiro.authz.permission.*;
+import org.apache.shiro.authz.AuthorizationException;
+import org.apache.shiro.authz.AuthorizationInfo;
+import org.apache.shiro.authz.Authorizer;
+import org.apache.shiro.authz.Permission;
+import org.apache.shiro.authz.UnauthorizedException;
+import org.apache.shiro.authz.permission.PermissionResolver;
+import org.apache.shiro.authz.permission.PermissionResolverAware;
+import org.apache.shiro.authz.permission.RolePermissionResolver;
+import org.apache.shiro.authz.permission.RolePermissionResolverAware;
+import org.apache.shiro.authz.permission.WildcardPermissionResolver;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.subject.PrincipalCollection;
@@ -29,7 +37,14 @@ import org.apache.shiro.util.Initializab
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
@@ -51,7 +66,9 @@ import java.util.concurrent.atomic.Atomi
*
* @see org.apache.shiro.authz.SimpleAuthorizationInfo
* @since 0.2
+ * @deprecated since 2.0 if favor of the {@link AccountStoreRealm} configured with an {@link org.apache.shiro.account.AccountStore AccountStore}.
*/
+@Deprecated
public abstract class AuthorizingRealm extends AuthenticatingRealm
implements Authorizer, Initializable, PermissionResolverAware, RolePermissionResolverAware {
@@ -476,12 +493,12 @@ public abstract class AuthorizingRealm e
return false;
}
- public boolean[] isPermitted(PrincipalCollection subjectIdentifier, String... permissions) {
+ public boolean[] isPermitted(PrincipalCollection principals, String... permissions) {
List<Permission> perms = new ArrayList<Permission>(permissions.length);
for (String permString : permissions) {
perms.add(getPermissionResolver().resolvePermission(permString));
}
- return isPermitted(subjectIdentifier, perms);
+ return isPermitted(principals, perms);
}
public boolean[] isPermitted(PrincipalCollection principals, List<Permission> permissions) {
@@ -504,19 +521,19 @@ public abstract class AuthorizingRealm e
return result;
}
- public boolean isPermittedAll(PrincipalCollection subjectIdentifier, String... permissions) {
+ public boolean isPermittedAll(PrincipalCollection principals, String... permissions) {
if (permissions != null && permissions.length > 0) {
Collection<Permission> perms = new ArrayList<Permission>(permissions.length);
for (String permString : permissions) {
perms.add(getPermissionResolver().resolvePermission(permString));
}
- return isPermittedAll(subjectIdentifier, perms);
+ return isPermittedAll(principals, perms);
}
return false;
}
- public boolean isPermittedAll(PrincipalCollection principal, Collection<Permission> permissions) {
- AuthorizationInfo info = getAuthorizationInfo(principal);
+ public boolean isPermittedAll(PrincipalCollection principals, Collection<Permission> permissions) {
+ AuthorizationInfo info = getAuthorizationInfo(principals);
return info != null && isPermittedAll(permissions, info);
}
@@ -531,13 +548,13 @@ public abstract class AuthorizingRealm e
return true;
}
- public void checkPermission(PrincipalCollection subjectIdentifier, String permission) throws AuthorizationException {
+ public void checkPermission(PrincipalCollection principals, String permission) throws AuthorizationException {
Permission p = getPermissionResolver().resolvePermission(permission);
- checkPermission(subjectIdentifier, p);
+ checkPermission(principals, p);
}
- public void checkPermission(PrincipalCollection principal, Permission permission) throws AuthorizationException {
- AuthorizationInfo info = getAuthorizationInfo(principal);
+ public void checkPermission(PrincipalCollection principals, Permission permission) throws AuthorizationException {
+ AuthorizationInfo info = getAuthorizationInfo(principals);
checkPermission(permission, info);
}
@@ -548,16 +565,16 @@ public abstract class AuthorizingRealm e
}
}
- public void checkPermissions(PrincipalCollection subjectIdentifier, String... permissions) throws AuthorizationException {
+ public void checkPermissions(PrincipalCollection principals, String... permissions) throws AuthorizationException {
if (permissions != null) {
for (String permString : permissions) {
- checkPermission(subjectIdentifier, permString);
+ checkPermission(principals, permString);
}
}
}
- public void checkPermissions(PrincipalCollection principal, Collection<Permission> permissions) throws AuthorizationException {
- AuthorizationInfo info = getAuthorizationInfo(principal);
+ public void checkPermissions(PrincipalCollection principals, Collection<Permission> permissions) throws AuthorizationException {
+ AuthorizationInfo info = getAuthorizationInfo(principals);
checkPermissions(permissions, info);
}
@@ -569,8 +586,8 @@ public abstract class AuthorizingRealm e
}
}
- public boolean hasRole(PrincipalCollection principal, String roleIdentifier) {
- AuthorizationInfo info = getAuthorizationInfo(principal);
+ public boolean hasRole(PrincipalCollection principals, String roleIdentifier) {
+ AuthorizationInfo info = getAuthorizationInfo(principals);
return hasRole(roleIdentifier, info);
}
@@ -578,8 +595,8 @@ public abstract class AuthorizingRealm e
return info != null && info.getRoles() != null && info.getRoles().contains(roleIdentifier);
}
- public boolean[] hasRoles(PrincipalCollection principal, List<String> roleIdentifiers) {
- AuthorizationInfo info = getAuthorizationInfo(principal);
+ public boolean[] hasRoles(PrincipalCollection principals, List<String> roleIdentifiers) {
+ AuthorizationInfo info = getAuthorizationInfo(principals);
boolean[] result = new boolean[roleIdentifiers != null ? roleIdentifiers.size() : 0];
if (info != null) {
result = hasRoles(roleIdentifiers, info);
@@ -602,8 +619,8 @@ public abstract class AuthorizingRealm e
return result;
}
- public boolean hasAllRoles(PrincipalCollection principal, Collection<String> roleIdentifiers) {
- AuthorizationInfo info = getAuthorizationInfo(principal);
+ public boolean hasAllRoles(PrincipalCollection principals, Collection<String> roleIdentifiers) {
+ AuthorizationInfo info = getAuthorizationInfo(principals);
return info != null && hasAllRoles(roleIdentifiers, info);
}
@@ -618,8 +635,8 @@ public abstract class AuthorizingRealm e
return true;
}
- public void checkRole(PrincipalCollection principal, String role) throws AuthorizationException {
- AuthorizationInfo info = getAuthorizationInfo(principal);
+ public void checkRole(PrincipalCollection principals, String role) throws AuthorizationException {
+ AuthorizationInfo info = getAuthorizationInfo(principals);
checkRole(role, info);
}
@@ -630,13 +647,13 @@ public abstract class AuthorizingRealm e
}
}
- public void checkRoles(PrincipalCollection principal, Collection<String> roles) throws AuthorizationException {
- AuthorizationInfo info = getAuthorizationInfo(principal);
+ public void checkRoles(PrincipalCollection principals, Collection<String> roles) throws AuthorizationException {
+ AuthorizationInfo info = getAuthorizationInfo(principals);
checkRoles(roles, info);
}
- public void checkRoles(PrincipalCollection principal, String... roles) throws AuthorizationException {
- checkRoles(principal, Arrays.asList(roles));
+ public void checkRoles(PrincipalCollection principals, String... roles) throws AuthorizationException {
+ checkRoles(principals, Arrays.asList(roles));
}
protected void checkRoles(Collection<String> roles, AuthorizationInfo info) {
Modified: shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/CachingRealm.java
URL: http://svn.apache.org/viewvc/shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/CachingRealm.java?rev=1599014&r1=1599013&r2=1599014&view=diff
==============================================================================
--- shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/CachingRealm.java (original)
+++ shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/CachingRealm.java Sun Jun 1 17:33:31 2014
@@ -44,7 +44,9 @@ import java.util.concurrent.atomic.Atomi
* @see #onLogout(org.apache.shiro.subject.PrincipalCollection)
* @see #getAvailablePrincipal(org.apache.shiro.subject.PrincipalCollection)
* @since 0.9
+ * @deprecated since 2.0 if favor of the {@link AccountStoreRealm} configured with an {@link org.apache.shiro.account.AccountStore AccountStore}.
*/
+@Deprecated
public abstract class CachingRealm implements Realm, Nameable, CacheManagerAware, LogoutAware {
private static final Logger log = LoggerFactory.getLogger(CachingRealm.class);
Added: shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/DefaultAccountCacheHandler.java
URL: http://svn.apache.org/viewvc/shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/DefaultAccountCacheHandler.java?rev=1599014&view=auto
==============================================================================
--- shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/DefaultAccountCacheHandler.java (added)
+++ shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/realm/DefaultAccountCacheHandler.java Sun Jun 1 17:33:31 2014
@@ -0,0 +1,70 @@
+/*
+ * 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 org.apache.shiro.realm;
+
+import org.apache.shiro.account.Account;
+import org.apache.shiro.account.AccountId;
+import org.apache.shiro.authc.AuthenticationToken;
+import org.apache.shiro.cache.Cache;
+
+/**
+ * @since 2.0
+ */
+public class DefaultAccountCacheHandler extends AbstractCacheHandler implements AccountCacheHandler {
+
+ private AccountCacheKeyResolver accountCacheKeyResolver;
+ private AccountCacheResolver accountCacheResolver;
+
+ public DefaultAccountCacheHandler() {
+ }
+
+ public Account getCachedAccount(AuthenticationToken token) {
+ Cache<Object,Account> cache = accountCacheResolver.getAccountCache(token);
+ Object key = accountCacheKeyResolver.getAccountCacheKey(token);
+ return cache.get(key);
+ }
+
+ public void cacheAccount(AuthenticationToken token, Account account) {
+ Cache<Object,Account> cache = accountCacheResolver.getAccountCache(token, account);
+ Object key = accountCacheKeyResolver.getAccountCacheKey(token, account);
+ cache.put(key, account);
+ }
+
+ public void clearCachedAccount(AccountId id) {
+ Cache<Object,Account> cache = accountCacheResolver.getAccountCache(id);
+ Object key = accountCacheKeyResolver.getAccountCacheKey(id);
+ cache.remove(key);
+ }
+
+ public AccountCacheKeyResolver getAccountCacheKeyResolver() {
+ return accountCacheKeyResolver;
+ }
+
+ public void setAccountCacheKeyResolver(AccountCacheKeyResolver accountCacheKeyResolver) {
+ this.accountCacheKeyResolver = accountCacheKeyResolver;
+ }
+
+ public AccountCacheResolver getAccountCacheResolver() {
+ return accountCacheResolver;
+ }
+
+ public void setAccountCacheResolver(AccountCacheResolver accountCacheResolver) {
+ this.accountCacheResolver = accountCacheResolver;
+ }
+}