• 欢迎访问VPS岛网站,国外VPS,国内VPS,国外服务器,国内服务器,服务器主机,测评及优惠码,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站 QQ群

Apache Shiro学习笔记(七)EnvironmentLoaderListener

Apache技术 luchunli1985 36次浏览 已收录 0个评论

鲁春利的工作笔记,好记性不如烂笔头


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的类图结构

wKioL1enQ3nghQROAAG-HAnWk08766.jpg

EnvironmentLoaderListener

package org.apache.shiro.web.env;

public class EnvironmentLoaderListener extends EnvironmentLoader implements ServletContextListener {
    // Web应用被初始化时默认调用的方法
    public void contextInitialized(ServletContextEvent sce) {
        // 调用父类EnvironmentLoader的initEnvironment方法
        initEnvironment(sce.getServletContext());
    }
    
    // Web应用被销毁时默认调用的方法(reload时是会调用该方法)
    public void contextDestroyed(ServletContextEvent sce) {
        destroyEnvironment(sce.getServletContext());
    }
}

EnvironmentLoader

package org.apache.shiro.web.env;

public class EnvironmentLoader {
    // 在web.xml文件中指定WebEnvironment的实现类
    public static final String ENVIRONMENT_CLASS_PARAM = "shiroEnvironmentClass";
    // 在web.xml文件中指定ini文件路径(不指定则默认加载/WEB-INF/shiro.ini)
    public static final String CONFIG_LOCATIONS_PARAM = "shiroConfigLocations";
    
    // 初始化Environment实例
    public WebEnvironment initEnvironment(ServletContext servletContext) throws IllegalStateException {

        if (servletContext.getAttribute(ENVIRONMENT_ATTRIBUTE_KEY) != null) {
            // 第一次Web应用启动时上下文中已经存在该KEY的属性,说明web.xml文件配置有问题,可能有多个EnvironmentLoader*的定义
            // 异常
        }

        // 记录日志
        servletContext.log("Initializing Shiro environment");
        log.info("Starting Shiro environment initialization.");

        // 记录当前时间
        long startTime = System.currentTimeMillis();

        try {
            // 获取WebEnvironment类的实例
            WebEnvironment environment = createEnvironment(servletContext);
            // 在ServletContext中设置KEY,设置完成后在WebUtils中就可以获取到了
            servletContext.setAttribute(ENVIRONMENT_ATTRIBUTE_KEY, environment);

            // 在日志中输出ServletContext中设置WebEnvironment耗费的时间(毫秒)
            if (log.isInfoEnabled()) {
                long elapsed = System.currentTimeMillis() - startTime;
                log.info("Shiro environment initialized in {} ms.", elapsed);
            }

            return environment;
        } catch (RuntimeException ex) {
            // 异常
        } catch (Error err) {
            // 异常
        }
    }
    
    // 在给定的ServletContext中设置WebEnvironment
    protected WebEnvironment createEnvironment(ServletContext sc) {

        Class<?> clazz = determineWebEnvironmentClass(sc);
        if (!MutableWebEnvironment.class.isAssignableFrom(clazz)) {    // native方法,底层JVM实现
           // 异常
        }

        // 从ServletContext中获取shiroConfigLocations属性的值(classpath:shiro/shiro-form-filter.ini)
        String configLocations = sc.getInitParameter(CONFIG_LOCATIONS_PARAM);
        boolean configSpecified = StringUtils.hasText(configLocations);

        if (configSpecified && !(ResourceConfigurable.class.isAssignableFrom(clazz))) {
            // 异常
        }
        // 获取类的实例(ClassUtils.newInstance(clazz) ==>  return clazz.newInstance();)
        MutableWebEnvironment environment = (MutableWebEnvironment) ClassUtils.newInstance(clazz);
        // 调用父类DefaultWebEnvironment的setServletContext
        environment.setServletContext(sc);

        if (configSpecified && (environment instanceof ResourceConfigurable)) {
            // 调用父类ResourceBasedWebEnvironment的setConfigLocations
            ((ResourceConfigurable) environment).setConfigLocations(configLocations);
        }

        customizeEnvironment(environment);
        // LifecycleUtils.init中会调用environment.init方法(IniWebEnvironment.init())
        LifecycleUtils.init(environment);
        
        // 返回得到的WebEnvironment类的实例(IniWebEnvironment的实例)
        return environment;
    }
    
    // 获取WebEnvironment的Class类对象
    protected Class<?> determineWebEnvironmentClass(ServletContext servletContext) {
        // ENVIRONMENT_CLASS_PARAM = "shiroEnvironmentClass";获取web.xml文件中声明的属性值
        String className = servletContext.getInitParameter(ENVIRONMENT_CLASS_PARAM);
        if (className != null) {
            try {
                // web.xml配置的参数对应className=org.apache.shiro.web.env.IniWebEnvironment
                return ClassUtils.forName(className);    
            } catch (UnknownClassException ex) {
                // 异常
            }
        } else {    // 如果未配置则使用该IniWebEnvironment类
            return IniWebEnvironment.class;
        }
    }
    
    // 空的方法体,可以实现自定义的逻辑
    protected void customizeEnvironment(WebEnvironment environment) {
        // ......
    }
}


VPS岛 的文章和资源来自互联网,仅作为参考资料,如果有侵犯版权的资源请尽快联系站长,我们会在24h内删除有争议的资源。丨 转载请注明Apache Shiro学习笔记(七)EnvironmentLoaderListener
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址