Shiro+ehcache Configuration Cache Authentication Information and Authorization Information

Keywords: Shiro Apache Ehcache xml

<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  
    <property name="realm" ref="userRealm"/>  
    <property name="cacheManager" ref="ehCacheManager"/>  
</bean>

<bean id="ehCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
    <property name="cacheManagerConfigFile" value="classpath:shiro/ehcache.xml"/>
<bean/>


    <bean id="userRealm" class="com.common.shiro.realm.UserRealm">
        <property name="credentialsMatcher" ref="credentialsMatcher"/>
        <property name="cachingEnabled" value="false"/>
        <property name="authenticationCachingEnabled" value="true"/>
        <property name="authenticationCacheName" value="authenticationCache"/>
        <property name="authorizationCachingEnabled" value="true"/>
        <property name="authorizationCacheName" value="authorizationCache"/>
    </bean>

Shiro Cache Authentication Information and Authorization Information

Spr-shiro.xml file configuration

ehcache.xml file configuration

 <cache name="authorizationCache"
           maxEntriesLocalHeap="2000"
           eternal="false"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="0"
           overflowToDisk="false"
           statistics="true">
    </cache>

    <cache name="authenticationCache"
           maxEntriesLocalHeap="2000"
           eternal="false"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="0"
           overflowToDisk="false"
           statistics="true">
    </cache>

If will

        <property name="authorizationCachingEnabled" value="true"/>
        <property name="authorizationCacheName" value="authorizationCache"/>

Comment it out. Each time you visit a place where authentication is required (such as the shiro tag on the page), you load a custom implementation of doGetAuthorizationInfo().

such as

Programming: By writing if/else authorization code block:
Subject subject = SecurityUtils.getSubject();

if(subject.hasRole("admin")) {

/ / have jurisdiction

} else {

/ / no permission

}

Annotation: By placing corresponding annotations on the Java method being executed:

@RequiresRoles("admin")

public void hello() {

/ / have jurisdiction

}

JSP/GSP tags: On JSP/GSP pages, complete with corresponding tags:

<shiro:hasRole name="admin">

<! - Have authority ->

</shiro:hasRole>
<shiro:hasPermission name="Log:viewLog">  </shiro:hasPermission>

If a cache is allocated, the cache is judged in the source code, and if so, the custom doGetAuthorizationInfo() method is not required.

package org.apache.shiro.realm;
public abstract class AuthorizingRealm extends AuthenticatingRealm
        implements Authorizer, Initializable, PermissionResolverAware, RolePermissionResolverAware {...}
 protected AuthorizationInfo getAuthorizationInfo(PrincipalCollection principals) {

        if (principals == null) {
            return null;
        }

        AuthorizationInfo info = null;

        if (log.isTraceEnabled()) {
            log.trace("Retrieving AuthorizationInfo for principals [" + principals + "]");
        }

        Cache<Object, AuthorizationInfo> cache = getAvailableAuthorizationCache();
        if (cache != null) {
            if (log.isTraceEnabled()) {
                log.trace("Attempting to retrieve the AuthorizationInfo from cache.");
            }
            Object key = getAuthorizationCacheKey(principals);
            info = cache.get(key);
            if (log.isTraceEnabled()) {
                if (info == null) {
                    log.trace("No AuthorizationInfo found in cache for principals [" + principals + "]");
                } else {
                    log.trace("AuthorizationInfo found in cache for principals [" + principals + "]");
                }
            }
        }


        if (info == null) {
            // Call template method if the info was not found in a cache
            info = doGetAuthorizationInfo(principals);
            // If the info is not null and the cache has been created, then cache the authorization info.
            if (info != null && cache != null) {
                if (log.isTraceEnabled()) {
                    log.trace("Caching authorization info for principals: [" + principals + "].");
                }
                Object key = getAuthorizationCacheKey(principals);
                cache.put(key, info);
            }
        }

        return info;
    }

 

 

Reference resources:

https://www.cnblogs.com/jpfss/p/8352031.html

Posted by nigeledge on Mon, 30 Sep 2019 07:21:24 -0700