码迷,mamicode.com
首页 > Web开发 > 详细

Apache Shiro学习笔记(七)Shiro Listener介绍

时间:2016-08-03 15:31:32      阅读:323      评论:0      收藏:0      [点我收藏+]

标签: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介绍

标签:apache shiro listener

原文地址:http://luchunli.blog.51cto.com/2368057/1833919

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!