這是我的shiro.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<bean id="jdbcRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm">
<property name="permissionsLookupEnabled" value="true"></property>
<property name="dataSource" ref="dataSource"/>
<property name="authenticationQuery"
value="SELECT password FROM USER_INFO WHERE username = ?"></property>
<property name="userRolesQuery"
value="select r.roleurl from role_user_info ru left join role_info r on ru.role_id = r.id ,user_info u where u.id =ru.user_id and u.username=?"></property>
<property name="permissionsQuery"
value="select t.right_url from role_rights_info lt left join rights_info t on lt.right_id = t.id ,role_info r where r.id =lt.role_id and r.roleurl=?"></property>
</bean>
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- 基於ehCache來緩存用戶認證信息和授權信息的實現 -->
<property name="cacheManager" ref="cacheManager"/>
<!-- sessionMode參數設置爲native時,那麼shrio就將用戶的基本認證信息保存到缺省名稱爲shiro-activeSessionCache 的Cache中 -->
<property name="sessionMode" value="native" />
<!--設置自定義realm -->
<property name="realms" >
<list>
<ref bean="jdbcRealm"></ref>
</list>
</property>
</bean>
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManager" ref="ehCacheManager"/>
<property name="cacheManagerConfigFile" value="classpath:shiro_ehcache.xml"/>
</bean>
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/>
<!--
Shiro主過濾器本身功能十分強大,其強大之處就在於它支持任何基於URL路徑表達式的、自定義的過濾器的執行
Web應用中,Shiro可控制的Web請求必須經過Shiro主過濾器的攔截,Shiro對基於Spring的Web應用提供了完美的支持
-->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- Shiro的核心安全接口,這個屬性是必須的 -->
<property name="securityManager" ref="securityManager"></property>
<!-- 要求登錄時的鏈接(登錄頁面地址),非必須的屬性,默認會自動尋找Web工程根目錄下的"/login.jsp"頁面 -->
<property name="loginUrl" value="/admin/login/login.html"></property>
<!-- 登錄成功後要跳轉的連接(本例中此屬性用不到,因爲登錄成功後的處理邏輯在LoginController裏硬編碼) -->
<property name="successUrl" value="/" ></property>
<!-- 用戶訪問未對其授權的資源時,所顯示的連接 -->
<property name="unauthorizedUrl" value="/admin/norights.html"></property>
<property name="filters">
<map>
<entry key="commonauth" value-ref="commonauth"/>
</map>
</property>
<property name="filterChainDefinitions">
<value>
/admin/norights.html=anon
/admin/login/**=anon
/admin/login/loginout.html=anon
/**=authc,commonauth
</value>
</property>
</bean>
<bean id="commonauth" class="common.shiro.CommonAuthFilter" />
</beans>
其中<bean id="commonauth" class="common.shiro.CommonAuthFilter" />
爲自定義的過濾器
以下是CommonAuthFilter的源碼:
package common.shiro;
import java.io.IOException;
import java.util.Set;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.CollectionUtils;
import org.apache.shiro.web.filter.authz.AuthorizationFilter;
import org.apache.shiro.web.util.WebUtils;
/**
* Created by lt on 2016/8/3.
* 權限過慮
*/
public class CommonAuthFilter extends AuthorizationFilter {
public CommonAuthFilter(){
}
protected boolean isAccessAllowed(ServletRequest servletRequest, ServletResponse servletResponse, Object o) throws Exception {
String requestURI = WebUtils.getPathWithinApplication(WebUtils.toHttp(servletRequest));
Subject subject = this.getSubject(servletRequest, servletResponse);
//超級管理員無阻
if(subject.hasRole("adminrole")) return true;
//通過subject判斷用戶有沒有些url權限
return subject.isPermitted(requestURI);
}
}
其中String requestURI = WebUtils.getPathWithinApplication(WebUtils.toHttp(servletRequest));
爲取到當前訪問的URL地址,在我的權限資源表中,我存的是每一個controller配製的@RequestMapping(value=”/main.html”) ,如果用戶能訪問此權限,自動通過。
當然一些別的權限也可以是虛擬的URL或者NAME,看自己怎麼定義。