前端HTML中:
導入標籤庫
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
使用方式:
<shiro:hasPermission name="setTrade:apr">
<button type="button" class="btn blue btn-circular"
style="width: 100px;" onclick="failReq()">失敗請求
</button>
</shiro:hasPermission>
權限標籤:
guest標籤
<shiro:guest>
</shiro:guest>
用戶沒有身份驗證時顯示相應信息,即遊客訪問信息。
user標籤
<shiro:user>
</shiro:user>
用戶已經身份驗證/記住我登錄後顯示相應的信息。
authenticated標籤
<shiro:authenticated>
</shiro:authenticated>
用戶已經身份驗證通過,即Subject.login登錄成功,不是記住我登錄的。
notAuthenticated標籤
<shiro:notAuthenticated>
</shiro:notAuthenticated>
用戶已經身份驗證通過,即沒有調用Subject.login進行登錄,包括記住我自動登錄的也屬於未進行身份驗證。
principal標籤
<shiro: principal/>
<shiro:principal property="username"/>
相當於((User)Subject.getPrincipals()).getUsername()。
lacksPermission標籤
<shiro:lacksPermission name="org:create">
</shiro:lacksPermission>
如果當前Subject沒有權限將顯示body體內容。
hasRole標籤
<shiro:hasRole name="admin">
</shiro:hasRole>
如果當前Subject有角色將顯示body體內容。
hasAnyRoles標籤
<shiro:hasAnyRoles name="admin,user">
</shiro:hasAnyRoles>
如果當前Subject有任意一個角色(或的關係)將顯示body體內容。
lacksRole標籤
<shiro:lacksRole name="abc">
</shiro:lacksRole>
如果當前Subject沒有角色將顯示body體內容。
hasPermission標籤
<shiro:hasPermission name="user:create">
</shiro:hasPermission>
如果當前Subject有權限將顯示body體內容
後臺
配置過濾器 applicationContext-shiro.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<description>apache shiro配置</description>
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="/page/login"/>
<property name="successUrl" value="/index"/>
<property name="unauthorizedUrl" value="/page/404"/>
<property name="filterChainDefinitions">
<value>
<!-- 靜態資源允許訪問 -->
/app/** = anon
/assets/** = anon
<!-- 登錄頁允許訪問 -->
/user/login = anon
/trans/** = anon
/cardSign/** = anon
/sms/** = anon
/** = authc
</value>
</property>
</bean>
<!-- 緩存管理器 使用Ehcache實現 -->
<bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:ehcache-shiro.xml"/>
</bean>
<!-- 會話DAO -->
<bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.MemorySessionDAO"/>
<!-- 會話管理器 -->
<bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<property name="sessionDAO" ref="sessionDAO"/>
</bean>
<!-- 安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realms">
<list>
<ref bean="xxxxx"/>
</list>
</property>
<!-- cacheManager,集合spring緩存工廠 -->
<!-- <property name="cacheManager" ref="shiroEhcacheManager" /> -->
<!-- <property name="sessionManager" ref="sessionManager" /> -->
</bean>
<!-- Shiro生命週期處理器 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
</beans>
具體校驗方法:
@Component(value = "securitySomething")
public class SecurityRealm extends AuthorizingRealm {
/**
* 權限檢查
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//TODO
//條件查詢權限數據庫,角色
return
}
/**
* 登錄驗證
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//TODO
//數據庫進行驗證
return
}
}
在方法上
@RequiresPermissions("base:sale:detail")
學習是最公平的事!
-----有教無類