Shiro總結一架構,動態權限配置,登陸認證明現

首先說明一點,我沒有刪除和shiro無關的不少代碼,主要是但願你們能更清楚的看到完整項目中的配置,其次,下面文中的使用不會採用shiro配置文件的方式,而是採用實際web項目中動態配置權限認證的方式。若是須要了解文件配置的方式能夠參考 https://www.w3cschool.cn/shiro/andc1if0.htmlcss

完整的項目地址 https://gitee.com/jiansin/ssmhtml

簡單介紹

Apache Shiro 是 Java 的一個安全框架。目前,使用 Apache Shiro 的人愈來愈多,由於它至關簡單,對比 Spring Security,可能沒有 Spring Security 作的功能強大,可是在實際工做時可能並不須要那麼複雜的東西,因此使用小而簡單的 Shiro 就足夠了。前端

基本功能

Authentication:身份認證 / 登陸,驗證用戶是否是擁有相應的身份;java

Authorization:受權,即權限驗證,驗證某個已認證的用戶是否擁有某個權限;即判斷用戶是否能作事情,常見的如:驗證某個用戶是否擁有某個角色。或者細粒度的驗證某個用戶對某個資源是否具備某個權限;git

Session Manager:會話管理,即用戶登陸後就是一次會話,在沒有退出以前,它的全部信息都在會話中;會話能夠是普通 JavaSE 環境的,也能夠是如 Web 環境的;web

Session Manager:會話管理,即用戶登陸後就是一次會話,在沒有退出以前,它的全部信息都在會話中;會話能夠是普通 JavaSE 環境的,也能夠是如 Web 環境的;redis

Cryptography:加密,保護數據的安全性,如密碼加密存儲到數據庫,而不是明文存儲;spring

Web Support:Web 支持,能夠很是容易的集成到 Web 環境;數據庫

Caching:緩存,好比用戶登陸後,其用戶信息、擁有的角色 / 權限沒必要每次去查,這樣能夠提升效率;apache

Concurrency:shiro 支持多線程應用的併發驗證,即如在一個線程中開啓另外一個線程,能把權限自動傳播過去;

Testing:提供測試支持;

Run As:容許一個用戶僞裝爲另外一個用戶(若是他們容許)的身份進行訪問;

Remember Me:記住我,這個是很是常見的功能,即一次登陸後,下次再來的話不用登陸了。

Shiro 不會去維護用戶、維護權限;這些須要咱們本身去設計 / 提供;而後經過相應的接口注入給 Shiro 便可。

外部看Shiro

應用代碼直接交互的對象是 Subject,也就是說 Shiro 的對外 API 核心就是 Subject;其每一個 API 的含義:

Subject:主體,表明了當前 「用戶」,這個用戶不必定是一個具體的人,與當前應用交互的任何東西都是 Subject,如網絡爬蟲,機器人等;即一個抽象概念;全部 Subject 都綁定到 SecurityManager,與 Subject 的全部交互都會委託給 SecurityManager;能夠把 Subject 認爲是一個門面;SecurityManager 纔是實際的執行者;

SecurityManager:安全管理器;即全部與安全有關的操做都會與 SecurityManager 交互;且它管理着全部 Subject;能夠看出它是 Shiro 的核心,它負責與後邊介紹的其餘組件進行交互,若是學習過 SpringMVC,你能夠把它當作 DispatcherServlet 前端控制器;

Realm:域,Shiro 從從 Realm 獲取安全數據(如用戶、角色、權限),就是說 SecurityManager 要驗證用戶身份,那麼它須要從 Realm 獲取相應的用戶進行比較以肯定用戶身份是否合法;也須要從 Realm 獲得用戶相應的角色 / 權限進行驗證用戶是否能進行操做;能夠把 Realm 當作 DataSource,即安全數據源。

Shiro架構

Subject:主體,能夠看到主體能夠是任何能夠與應用交互的 「用戶」;

SecurityManager:至關於 SpringMVC 中的 DispatcherServlet 或者 Struts2 中的 FilterDispatcher;是 Shiro 的心臟;全部具體的交互都經過 SecurityManager 進行控制;它管理着全部 Subject、且負責進行認證和受權、及會話、緩存的管理。

Authenticator:認證器,負責主體認證的,這是一個擴展點,若是用戶以爲 Shiro 默認的很差,能夠自定義實現;其須要認證策略(Authentication Strategy),即什麼狀況下算用戶認證經過了;

Authrizer:受權器,或者訪問控制器,用來決定主體是否有權限進行相應的操做;即控制着用戶能訪問應用中的哪些功能;

Realm:能夠有 1 個或多個 Realm,能夠認爲是安全實體數據源,即用於獲取安全實體的;能夠是 JDBC 實現,也能夠是 LDAP 實現,或者內存實現等等;由用戶提供;注意:Shiro 不知道你的用戶 / 權限存儲在哪及以何種格式存儲;因此咱們通常在應用中都須要實現本身的 Realm;

SessionManager:若是寫過 Servlet 就應該知道 Session 的概念,Session 呢須要有人去管理它的生命週期,這個組件就是 SessionManager;而 Shiro 並不只僅能夠用在 Web 環境,也能夠用在如普通的 JavaSE 環境、EJB 等環境;全部呢,Shiro 就抽象了一個本身的 Session 來管理主體與應用之間交互的數據;這樣的話,好比咱們在 Web 環境用,剛開始是一臺 Web 服務器;接着又上了臺 EJB 服務器;這時想把兩臺服務器的會話數據放到一個地方,這個時候就能夠實現本身的分佈式會話(如把數據放到 Memcached 服務器);

SessionDAO:DAO 你們都用過,數據訪問對象,用於會話的 CRUD,好比咱們想把 Session 保存到數據庫,那麼能夠實現本身的 SessionDAO,經過如 JDBC 寫到數據庫;好比想把 Session 放到 Memcached 中,能夠實現本身的 Memcached SessionDAO;另外 SessionDAO 中可使用 Cache 進行緩存,以提升性能;

CacheManager:緩存控制器,來管理如用戶、角色、權限等的緩存的;由於這些數據基本上不多去改變,放到緩存中後能夠提升訪問的性能

Cryptography:密碼模塊,Shiro 提升了一些常見的加密組件用於如密碼加密 / 解密的。

Shiro 身份驗證

在 shiro 中,用戶須要提供 principals (身份)和 credentials(證實)給 shiro,從而應用能驗證用戶身份:

principals:身份,即主體的標識屬性,能夠是任何東西,如用戶名、郵箱等,惟一便可。一個主體能夠有多個 principals,但只有一個 Primary principals,通常是用戶名 / 密碼 / 手機號。

credentials:證實 / 憑證,即只有主體知道的安全值,如密碼 / 數字證書等。

最多見的 principals 和 credentials 組合就是用戶名 / 密碼了。

Spring整合shiro

<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-core</artifactId>
    <version>1.3.2</version>
</dependency>
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring</artifactId>
    <version>1.3.2</version>
</dependency>
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-web</artifactId>
    <version>1.3.2</version>
</dependency>

spring-shiro.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="redisSessionDAO" class="com.plantform.shiro.commons.RedisSessionDao"/>
    <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
        <!-- 設置session過時時間爲1小時(單位:毫秒),默認爲30分鐘 -->
        <property name="globalSessionTimeout" value="3600000"/>
        <property name="sessionValidationSchedulerEnabled" value="true"/>
        <property name="sessionDAO" ref="redisSessionDAO"/>
    </bean>


    <bean id="cacheManager" class="com.plantform.shiro.commons.RedisCacheManager">
        <property name="redisTemplate" ref="redisTemplate"/>
    </bean>
    <!-- Shiro默認會使用Servlet容器的Session,可經過sessionMode屬性來指定使用Shiro原生Session -->
    <!-- 即<property name="sessionMode" value="native"/>,詳細說明見官方文檔 -->
    <!-- 這裏主要是設置自定義的單Realm應用,如有多個Realm,可以使用'realms'屬性代替 -->
    <!-- securityManager安全管理器 -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realms">
            <list>
                <ref bean="shiroRealm"/>
            </list>
        </property>
        <!-- 注入緩存管理器 -->
        <property name="cacheManager" ref="cacheManager"/>
        <!-- 注入session管理器 -->
        <property name="sessionManager" ref="sessionManager"/>
        <!-- 記住我 -->

    </bean>

    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"/>
        <!-- 要求登陸時的連接(可根據項目的URL進行替換),非必須的屬性,默認會自動尋找Web工程根目錄下的"/login.html"頁面 -->
        <property name="loginUrl" value="/index.jsp"/>
        <!-- 用戶訪問未對其受權的資源時,所顯示的鏈接 -->
        <property name="unauthorizedUrl" value="/"/>
        <property name="filters">
            <map>
                <entry key="authc" value-ref="authenticationFilter"/>
            </map>
        </property>

        <!-- Shiro鏈接約束配置,即過濾鏈的定義 -->
        <!-- 此處可配合個人這篇文章來理解各個過濾連的做用http://blog.csdn.net/jadyer/article/details/12172839 -->
        <!-- 下面value值的第一個'/'表明的路徑是相對於HttpServletRequest.getContextPath()的值來的 -->
        <!-- anon:它對應的過濾器裏面是空的,什麼都沒作,這裏.do和.jsp後面的*表示參數,比方說login.jsp?main這種 -->
        <!-- authc:該過濾器下的頁面必須驗證後才能訪問,它是Shiro內置的一個攔截器org.apache.shiro.web.filter.authc.FormAuthenticationFilter -->
        <property name="filterChainDefinitions">
            <value>
                /login.jsp=anon
                /system/captcha=anon
                /static/**=anon
                /system/logout = anon
                /system/login=anon
                /oauth/**=anon
                /error/**=anon
                /v2/**/=anon
                /webjars/**=anon
                /swagger-resources/**=anon
                /swagger-ui.html/**=anon
                /system/welcome=authc
                /**=authc
            </value>
        </property>
    </bean>

    <bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
        <property name="hashAlgorithmName" value="md5"/>
        <property name="hashIterations" value="2"/>
    </bean>
    <bean id="shiroRealm" class="com.plantform.shiro.commons.ShiroRealm">
        <property name="credentialsMatcher" ref="credentialsMatcher"/>
    </bean>

    <bean id="authenticationFilter" class="com.plantform.shiro.commons.ShiroAuthenticationFilter"/>

    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

    <!-- AOP式方法級權限檢查  -->
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
          depends-on="lifecycleBeanPostProcessor">
        <property name="proxyTargetClass" value="true"/>
    </bean>
    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
        <property name="securityManager" ref="securityManager"/>
    </bean>

</beans>

在spring配置文件中引入shiro資源文件,項目的其餘配置我依然不會刪除,一樣是但願你們能瞭解一下spring的整合方式,擴展你們的思路。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 加載property文件配置 -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:*.properties</value>
            </list>
        </property>
    </bean>
    <!-- 組件掃描 -->
    <context:component-scan base-package="com.plantform.**"/>
    <!-- 加載mybatis文件配置 -->
    <import resource="classpath*:spring-mybatis.xml"/>
    <!--redis-->
    <import resource="classpath*:spring-redis.xml"/>
    <!--shiro-->
    <import resource="classpath*:spring-shiro.xml"/>
    <bean id="customJobFactory" class="com.plantform.quartz.utils.CustomJobFactory"/>
    <bean id="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <!--Scheduler推遲10秒啓動-->
        <property name="startupDelay" value="10"/>
        <property name="autoStartup" value="true"/>
        <property name="jobFactory" ref="customJobFactory"/>
        <property name="applicationContextSchedulerContextKey" value="applicationContextKey"/>
        <property name="configLocation" value="classpath:quartz.properties"/>
    </bean>
</beans>

登陸

@ApiOperation(value = "登陸", httpMethod = "POST", produces = "application/json", response = Result.class)
@ResponseBody
@RequestMapping(value = "login", method = RequestMethod.POST)
public Result login(@RequestParam String loginName,
                    @RequestParam String password,
                    @RequestParam int platform,
                    HttpServletRequest request) throws Exception {
    //根據用戶名查詢是否存在用戶
    SysUser user = sysUserService.selectByLoginName(loginName);
    if (user == null) {
        //告訴前端目前系統中不存在該用戶,直接返回,再也不進行後面的shiro登陸。
        return Result.instance(ResponseCode.unknown_account.getCode(), ResponseCode.unknown_account.getMsg());
    }

    Subject subject = SecurityUtils.getSubject();
	//這個地方可能會拋出異常,系統已經進行全局處理了
    subject.login(new UsernamePasswordToken(loginName, password));
    LoginInfo loginInfo = sysUserService.login(user, subject.getSession().getId(), platform);
    subject.getSession().setAttribute("loginInfo", loginInfo);
    log.debug("登陸成功");
    return Result.success(loginInfo);
}

完整的項目代碼地址 https://gitee.com/jiansin/ssm.git

身份認證流程

首先調用 Subject.login(token) 進行登陸,其會自動委託給 Security Manager,SecurityManager 負責真正的身份驗證邏輯;它會委託給 Authenticator 進行身份驗證;

Authenticator 纔是真正的身份驗證者,Shiro API 中核心的身份認證入口點,此處能夠自定義插入本身的實現;

Authenticator 可能會委託給相應的 AuthenticationStrategy 進行多 Realm 身份驗證,默認 ModularRealmAuthenticator 會調用 AuthenticationStrategy 進行多 Realm 身份驗證;

Authenticator 會把相應的 token 傳入 Realm,從 Realm 獲取身份驗證信息,若是沒有返回 / 拋出異常表示身份驗證失敗了。此處能夠配置多個 Realm,將按照相應的順序及策略進行訪問。

Realm

Shiro 從從 Realm 獲取安全數據(如用戶、角色、權限),就是說SecurityManager 要驗證用戶身份,那麼它須要從 Realm 獲取相應的用戶進行比較以肯定用戶身份是否合法;也須要從 Realm 獲得用戶相應的角色 / 權限進行驗證用戶是否能進行操做;能夠把 Realm 當作 DataSource,即安全數據源。

package com.plantform.shiro.commons;

import com.plantform.common.SystemConstant;
import com.plantform.shiro.bean.*;
import com.plantform.shiro.mapper.*;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author: lee
 * @Date : 2019/03/18
 * @Description 自定義realm實現
 */
public class ShiroRealm extends AuthorizingRealm {
    private static Logger log = LoggerFactory.getLogger(ShiroRealm.class);
 
    /**
     * 登陸驗證
     *
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        log.debug("登陸驗證");
   String username = (String)token.getPrincipal();  //獲得用戶名
        String password = new String((char[])token.getCredentials()); //獲得密碼
        if(!"admin".equals(username)) {
            throw new UnknownAccountException(); //若是用戶名錯誤
        }
        if(!"123456".equals(password)) {
            throw new IncorrectCredentialsException(); //若是密碼錯誤
        }
        //若是身份認證驗證成功,返回一個AuthenticationInfo實現;
        return new SimpleAuthenticationInfo(username, password, getName());
}

Shiro 受權方式

編程式:經過寫 if/else 受權代碼塊完成:

Subject subject = SecurityUtils.getSubject();
if(subject.hasRole(「admin」)) {
    //有權限
} else {
    //無權限
}

註解式:經過在執行的 Java 方法上放置相應的註解完成,實際應用開發中基本採用註解 方式:

@RequiresRoles("admin")
public void hello() {
    //有權限
}

沒有權限將拋出相應的異常;

JSP/GSP 標籤:在 JSP/GSP 頁面經過相應的標籤完成:

<shiro:hasRole name="admin">
<!— 有權限 —>
</shiro:hasRole>

Shiro動態受權

package com.plantform.shiro.commons;

import com.plantform.common.SystemConstant;
import com.plantform.shiro.bean.*;
import com.plantform.shiro.mapper.*;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author: lee
 * @Description 自定義realm實現
 */
public class ShiroRealm extends AuthorizingRealm {
    private static Logger log = LoggerFactory.getLogger(ShiroRealm.class);
    @Autowired
    private SysUserPermissionMapper sysUserPermissionMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysPermissionMapper sysPermissionMapper;
    @Autowired
    private SysUserRoleOrganizationMapper sysUserRoleOrganizationMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRoleOrganizationMapper sysRoleOrganizationMapper;
    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    /**
     * 鑑權信息
     *
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        log.debug("開始查詢受權信息");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        String loginStr = (String) principalCollection.getPrimaryPrincipal();
        SysUser user = sysUserMapper.selectUserByLoginName(loginStr);
        List<SysUserPermission> userPermissions = sysUserPermissionMapper.selectByUserId(user.getId());
        Set<String> permissions = new HashSet<>();
        Set<String> roles = new HashSet<>();
        for (SysUserPermission userPermission : userPermissions) {
            SysPermission sysPermission = sysPermissionMapper.selectById(userPermission.getSysPermissionId());
            permissions.add(sysPermission.getCode());
        }
        List<SysUserRoleOrganization> userRoleOrganizations = sysUserRoleOrganizationMapper.selectByUserId(user.getId());
        for (SysUserRoleOrganization sysUserRoleOrganization : userRoleOrganizations) {
            SysRoleOrganization sysRoleOrganization = sysRoleOrganizationMapper.selectById(sysUserRoleOrganization.getSysRoleOrganizationId());
            SysRole sysRole = sysRoleMapper.selectById(sysRoleOrganization.getSysRoleId());
            roles.add(sysRole.getName());
            List<SysRolePermission> sysRolePermissions = sysRolePermissionMapper.selectByRoleId(sysRole.getId());
            for (SysRolePermission sysRolePermission : sysRolePermissions) {
                SysPermission sysPermission = sysPermissionMapper.selectById(sysRolePermission.getSysPermissionId());
                permissions.add(sysPermission.getCode());
            }
        }
        info.addRoles(roles);
        info.addStringPermissions(permissions);
        log.debug("角色信息: \n {}", roles.toString());
        log.debug("權限信息: \n{}", permissions.toString());
        return info;
    }

    /**
     * 登陸驗證
     *
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        log.debug("登陸驗證");
        String loginName = (String) authenticationToken.getPrincipal();
        SysUser sysUser = sysUserMapper.selectUserByLoginName(loginName);
        AuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(loginName, sysUser.getPassword(), ByteSource.Util.bytes(sysUser.getPasswordSalt()), getName());
        return authenticationInfo;
    }

    @Override
    protected void doClearCache(PrincipalCollection principals) {
        redisTemplate.delete(SystemConstant.shiro_cache_prefix + principals.getPrimaryPrincipal().toString());
    }

    @Override
    protected void clearCachedAuthorizationInfo(PrincipalCollection principals) {
        log.debug("clearCachedAuthorizationInfo");
    }

}

配置shiro受權也能夠採用以下這種方式

https://www.w3cschool.cn/shiro/skex1if6.html

web應用集成shiro

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1" metadata-complete="true">

    <display-name>ssm</display-name>
    <description>springmvc-mybatis腳手架</description>

    <!-- 在Spring框架中是如何解決從頁面傳來的字符串的編碼問題的呢?
    下面咱們來看看Spring框架給咱們提供過濾器CharacterEncodingFilter
     這個過濾器就是針對於每次瀏覽器請求進行過濾的,而後再其之上添加了父類沒有的功能即處理字符編碼。
      其中encoding用來設置編碼格式,forceEncoding用來設置是否理會 request.getCharacterEncoding()方法,設置爲true則強制覆蓋以前的編碼格式。-->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
     <!-- 添加druid監控-->
    <servlet>
        <servlet-name>DruidStatView</servlet-name>
        <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>DruidStatView</servlet-name>
        <url-pattern>/druid/*</url-pattern>
    </servlet-mapping>
    <!-- 添加Web應用等監控-->
    <filter>
        <filter-name>DruidWebStatFilter</filter-name>
        <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
        <init-param>
            <param-name>exclusions</param-name>
            <param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value>
        </init-param>
        <init-param>
            <param-name>profileEnable</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>principalCookieName</param-name>
            <param-value>USER_COOKIE</param-value>
        </init-param>
        <init-param>
            <param-name>principalSessionName</param-name>
            <param-value>USER_SESSION</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>DruidWebStatFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 項目中使用Spring 時,applicationContext.xml配置文件中並無BeanFactory,要想在業務層中的class 文件中直接引用Spring容器管理的bean可經過如下方式-->
    <!--一、在web.xml配置監聽器ContextLoaderListener-->
    <!--ContextLoaderListener的做用就是啓動Web容器時,自動裝配ApplicationContext的配置信息。由於它實現了ServletContextListener這個接口,在web.xml配置這個監聽器,啓動容器時,就會默認執行它實現的方法。
    在ContextLoaderListener中關聯了ContextLoader這個類,因此整個加載配置過程由ContextLoader來完成。
    它的API說明
    第一段說明ContextLoader能夠由 ContextLoaderListener和ContextLoaderServlet生成。
    若是查看ContextLoaderServlet的API,能夠看到它也關聯了ContextLoader這個類並且它實現了HttpServlet    這個接口
    第二段,ContextLoader建立的是 XmlWebApplicationContext這樣一個類,它實現的接口是WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext->
    BeanFactory這樣一來spring中的全部bean都由這個類來建立
     IUploaddatafileManager uploadmanager = (IUploaddatafileManager)
     ContextLoaderListener.getCurrentWebApplicationContext().getBean("uploadManager");-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--二、部署applicationContext的xml文件-->
    <!--若是在web.xml中不寫任何參數配置信息,默認的路徑是"/WEB-INF/applicationContext.xml,
    在WEB-INF目錄下建立的xml文件的名稱必須是applicationContext.xml。
    若是是要自定義文件名能夠在web.xml里加入contextConfigLocation這個context參數:
    在<param-value> </param-value>裏指定相應的xml文件名,若是有多個xml文件,能夠寫在一塊兒並以「,」號分隔。
    也能夠這樣applicationContext-*.xml採用通配符,好比這那個目錄下有applicationContext-ibatis-base.xml,
    applicationContext-action.xml,applicationContext-ibatis-dao.xml等文件,都會一同被載入。
    在ContextLoaderListener中關聯了ContextLoader這個類,因此整個加載配置過程由ContextLoader來完成。-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!--若是你的DispatcherServlet攔截"/",爲了實現REST風格,攔截了全部的請求,那麼同時對*.js,*.jpg等靜態文件的訪問也就被攔截了。-->
    <!--方案一:激活Tomcat的defaultServlet來處理靜態文件-->
    <!--要寫在DispatcherServlet的前面, 讓 defaultServlet先攔截請求,這樣請求就不會進入Spring了,我想性能是最好的吧。-->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.css</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.swf</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.gif</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.jpg</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.png</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.js</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.xml</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.json</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.map</url-pattern>
    </servlet-mapping>
    <!--使用Spring MVC,配置DispatcherServlet是第一步。DispatcherServlet是一個Servlet,,因此能夠配置多個DispatcherServlet-->
    <!--DispatcherServlet是前置控制器,配置在web.xml文件中的。攔截匹配的請求,Servlet攔截匹配規則要自已定義,把攔截下來的請求,依據某某規則分發到目標Controller(咱們寫的Action)來處理。-->
    <servlet>
        <servlet-name>spring-mvc</servlet-name>
        <!--在DispatcherServlet的初始化過程當中,框架會在web應用的 WEB-INF文件夾下尋找名爲[servlet-name]-servlet.xml 的配置文件,生成文件中定義的bean。-->
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--指明瞭配置文件的文件名,不使用默認配置文件名,而使用dispatcher-servlet.xml配置文件。-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <!--其中<param-value>**.xml</param-value> 這裏可使用多種寫法-->
            <!--一、不寫,使用默認值:/WEB-INF/<servlet-name>-servlet.xml-->
            <!--二、<param-value>/WEB-INF/classes/dispatcher-servlet.xml</param-value>-->
            <!--三、<param-value>classpath*:dispatcher-servlet.xml</param-value>-->
            <!--四、多個值用逗號分隔-->
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <!--是啓動順序,讓這個Servlet隨Servletp容器一塊兒啓動。-->
    </servlet>
    <servlet-mapping>
        <!--這個Servlet的名字是dispatcher,能夠有多個DispatcherServlet,是經過名字來區分的。每個DispatcherServlet有本身的WebApplicationContext上下文對象。同時保存的ServletContext中和Request對象中.-->
        <!--ApplicationContext是Spring的核心,Context咱們一般解釋爲上下文環境,我想用「容器」來表述它更容易理解一些,ApplicationContext則是「應用的容器」了:P,Spring把Bean放在這個容器中,在須要的時候,用getBean方法取出-->
        <servlet-name>spring-mvc</servlet-name>
        <!--Servlet攔截匹配規則能夠自已定義,當映射爲@RequestMapping("/user/add")時,爲例,攔截哪一種URL合適?-->
        <!--一、攔截*.do、*.htm, 例如:/user/add.do,這是最傳統的方式,最簡單也最實用。不會致使靜態文件(jpg,js,css)被攔截。-->
        <!--二、攔截/,例如:/user/add,能夠實現如今很流行的REST風格。不少互聯網類型的應用很喜歡這種風格的URL。弊端:會致使靜態文件(jpg,js,css)被攔截後不能正常顯示。 -->
        <!--<url-pattern>*.do</url-pattern>-->
        <url-pattern>/</url-pattern>
        <!--會攔截URL中帶「/」的請求。-->
    </servlet-mapping>

    <!-- shiro的filter -->
    <!-- shiro過慮器,DelegatingFilterProxy經過代理模式將spring容器中的bean和filter關聯起來 -->
    <filter>
        <filter-name>shiroFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <!-- 設置true由servlet容器控制filter的生命週期 -->
        <init-param>
            <param-name>targetFilterLifecycle</param-name>
            <param-value>true</param-value>
        </init-param>
        <!-- 設置spring容器filter的bean id,若是不設置則找與filter-name一致的bean-->
        <init-param>
            <param-name>targetBeanName</param-name>
            <param-value>shiroFilter</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <error-page>
        <!--當系統出現404錯誤,跳轉到頁面nopage.html-->
        <error-code>404</error-code>
        <location>/nopage.html</location>
    </error-page>
    <error-page>
        <!--當系統出現java.lang.NullPointerException,跳轉到頁面error.html-->
        <exception-type>java.lang.NullPointerException</exception-type>
        <location>/error.html</location>
    </error-page>
    <session-config>
        <!--會話超時配置,單位分鐘-->
        <session-timeout>360</session-timeout>
    </session-config>
</web-app>
相關文章
相關標籤/搜索