Apache Shiro學習筆記(七)Shiro Listener介紹

來源:互聯網
上載者:User

標籤:apache shiro listener

魯春利的工作筆記,好記性不如爛筆頭


web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns="http://java.sun.com/xml/ns/javaee"     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"     id="WebApp_ID" version="3.0">  <display-name>Invicme</display-name>  <listener>    <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>  </listener>    <context-param>    <param-name>shiroEnvironmentClass</param-name>    <param-value>org.apache.shiro.web.env.IniWebEnvironment</param-value>  </context-param>  <context-param>    <param-name>shiroConfigLocations</param-name>    <param-value>classpath:shiro/shiro-form-filter.ini</param-value>  </context-param>    <filter>    <filter-name>ShiroFilter</filter-name>    <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>  </filter>    <filter-mapping>    <filter-name>ShiroFilter</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping>  <session-config>    <session-timeout>30</session-timeout>  </session-config>    <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>

web.xml的配置中<context-param>的作用
1. 啟動一個WEB項目的時候,容器(如:Tomcat)會去讀它的設定檔web.xml
    讀兩個節點: <listener></listener> 和 <context-param></context-param>
2. 緊接著,容器建立一個ServletContext(上下文),這個WEB項目所有部分都將共用這個上下文
3. 容器將<context-param></context-param>轉化為索引值對,並交給ServletContext
4. 容器建立<listener></listener>中的類執行個體,即建立監聽
5. 在監聽中會有contextInitialized(ServletContextEvent args)初始化方法
    在這個方法中獲得ServletContext = ServletContextEvent.getServletContext();
    context-param的值 = ServletContext.getInitParameter("context-param的鍵");
6. 得到這個context-param的值之後,可以做一些其他動作了


ShiroFilter

package org.apache.shiro.web.servlet;import org.apache.shiro.web.env.WebEnvironment;import org.apache.shiro.web.filter.mgt.FilterChainResolver;import org.apache.shiro.web.util.WebUtils;/** * @see org.apache.shiro.web.env.EnvironmentLoader EnvironmentLoader * @see org.apache.shiro.web.env.EnvironmentLoaderListener EnvironmentLoaderListener * @see <a href="http://shiro.apache.org/web.html">Apache Shiro Web Documentation</a> * @since 1.2 */public class ShiroFilter extends AbstractShiroFilter {    /**     * @see org.apache.shiro.web.env.EnvironmentLoaderListener     * @since 1.2     */    @Override    public void init() throws Exception {        //         WebEnvironment env = WebUtils.getRequiredWebEnvironment(getServletContext());        setSecurityManager(env.getWebSecurityManager());        FilterChainResolver resolver = env.getFilterChainResolver();        if (resolver != null) {            setFilterChainResolver(resolver);        }    }}


協助工具輔助類WebUtils

package org.apache.shiro.web.util;public class WebUtils {        public static WebEnvironment getRequiredWebEnvironment(ServletContext sc)            throws IllegalStateException {        WebEnvironment we = getWebEnvironment(sc);        if (we == null) {            throw new IllegalStateException("No WebEnvironment found: no EnvironmentLoaderListener registered?");        }        return we;    }        public static WebEnvironment getWebEnvironment(ServletContext sc) {        return getWebEnvironment(sc, EnvironmentLoader.ENVIRONMENT_ATTRIBUTE_KEY);    }        public static WebEnvironment getWebEnvironment(ServletContext sc, String attrName) {        if (sc == null) {            // 異常        }        Object attr = sc.getAttribute(attrName);        if (attr == null) {    // 返回null(ServletContext中無該屬性)            return null;        }        if (attr instanceof RuntimeException) {            // 異常        }        if (attr instanceof Error) {            // 異常        }        if (attr instanceof Exception) {            // 異常        }        if (!(attr instanceof WebEnvironment)) {            // 異常        }        // 擷取到實際的值        return (WebEnvironment) attr;    }}


ServletContext中的EnvironmentLoader.ENVIRONMENT_ATTRIBUTE_KEY屬性是在什麼時候設定的呢?


WebEnvironment的類圖結構




本文出自 “悶葫蘆的世界” 部落格,請務必保留此出處http://luchunli.blog.51cto.com/2368057/1833919

Apache Shiro學習筆記(七)Shiro Listener介紹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.