发表于: 2020-08-09 22:03:31

1 2107


shiro框架

JSP页面授权

Shiro提供了JSTL标签用于在JSP/GSP页面进行权限控制;首先需要导入标签库:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
标签名称作用
shiro:guest用户没有身份验证时显示相应的信息,即游客访问信息
shiro:user用户已经身份验证、记住我登录后显示相应的信息,未登录用户将会拦截
shiro:authenticated用户已经身份验证通过,即Subject.isAuthenticated() == true;未登录或记住我登录的都会拦截
shiro:notAuthenticated用户已经身份验证通过,但是Subject.isAuthenticated() == false,即可能是通过记住我登录的
shiro:principal显示用户身份信息,默认调用Subject.getPrincipal()获取用户登录信息
shiro:hasRole如:<shiro:hasRole name="admin">,如果当前Subject有admin角色就显示数据,类似于@RequiresRoles()注解;否则就拦截
shiro:hasAnyRole如:<shiro:hasAnyRole name="admin,user">,如果当前Subject有admin或user角色就显示数据,类似于@RequireRoles(Logical=Logical.OR)注解;否则将就拦截
shiro:lackRole如果当前Subject没有角色就显示数据
shiro:hasPermission如:<shiro:hasPermission name="user:create">,如果当前Subject有user:create权限,就显示数据;否则就拦截
shiro:lacksPermission如:<shiro:lacksPermission name="user:create">,如果当前Subject没有user:create权限,就显示数据;否则拦截

Shiro实现会话管理

会话:用户登录后直至注销(Session丢失)前称为一次会话,即用户访问应用时保持的连接关系,可以保证在多次交互中应用能够识别出当前访问的用户是谁,且可在多次交互中保存一些数据。常见的应用实例如:登录时记住我的功能、单点登录的功能...

Shiro提供了会话管理器:sessionManager,管理着所有会话的创建、维护、删除、等工作。在web环境中使用Shiro的会话管理器,我们需要在Spring的配置文件中注入DefaultWebSessionManager:

<bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<!-- 设置全局会话过期时间:默认30分钟 -->
<property name="globalSessionTimeout" value="1800000"/>
<!-- 是否启用sessionIdCookie,默认是启用的 -->
<property name="sessionIdCookieEnabled" value="true"/>
<!-- 会话Cookie -->
<property name="sessionIdCookie" ref="sessionIdCookie"/>
</bean>

       <!-- 会话Cookie模板 -->
<bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
<constructor-arg value="sid"/>
<!-- 如果设置为true,则客户端不会暴露给服务端脚本代码,有助于减少某些类型的跨站脚本攻击 -->
<property name="httpOnly" value="true"/>
<property name="maxAge" value="-1"/><!-- maxAge=-1表示浏览器关闭时失效此Cookie -->
</bean>
       还要将sessionManager注入到SecurityManager中:

<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="userRealm"/>
<!-- 注入sessionManager -->
<property name="sessionManager" ref="sessionManager"/>
</bean>

Shiro缓存实现

Shiro也集成了缓存机制,例如Shiro提供了CachingRealm,提供了一些基础的缓存实现。Shiro默认是禁用缓存的,首先我们要开启Shiro的缓存管理,在XML中进行如下配置:

<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:other/ehcache.xml"/>
</bean>
        <!-- Realm实现 -->
<bean id="userRealm" class="cn.tycoding.realm.UserRealm">
<!-- 使用credentialsMatcher实现密码验证服务 -->
<property name="credentialsMatcher" ref="credentialsMatcher"/>
<!-- 是否启用缓存 -->
<property name="cachingEnabled" value="true"/>
<!-- 是否启用身份验证缓存 -->
<property name="authenticationCachingEnabled" value="true"/>
<!-- 缓存AuthenticationInfo信息的缓存名称 -->
<property name="authenticationCacheName" value="authenticationCache"/>
<!-- 是否启用授权缓存,缓存AuthorizationInfo信息 -->
<property name="authorizationCachingEnabled" value="true"/>
<!-- 缓存AuthorizationInfo信息的缓存名称 -->
<property name="authorizationCacheName" value="authorizationCache"/>
</bean>

resources/other/文件夹下创建配置文件ehcache.xml

        <?xml version="1.0" encoding="UTF-8"?>
<ehcache name="shirocache">
<cache name="shiro-activeSessionCache"
      maxEntriesLocalHeap="2000"
      eternal="false"
      timeToIdleSeconds="3600"
      timeToLiveSeconds="0"
      overflowToDisk="false"
      statistics="true">
</cache>
</ehcache>

明日计划 深度思考


返回列表 返回列表
评论

    分享到