【Shiro框架总结】2. Shiro集成SpringMVC

本文详细介绍了如何在SpringMVC项目中整合Shiro框架,包括配置步骤、ShiroFilter的工作原理及缓存设置。通过具体示例,帮助读者理解如何在web.xml中配置ShiroFilter,以及在applicationContext.xml中实现自定义Realm和缓存。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、整合步骤

 

1. 导入Spring的Jar包

2. 编写web.xml

 <!-- 监听器,确保spring的objectFactory提前创建出来,并重新指定配置文件加载的位置 -->
  <listener>
  	<listener-class>
  		org.springframework.web.context.ContextLoaderListener
  	</listener-class>
  </listener>
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>

<!-- springMVC需要配置的核心servlet,重新指定配置文件加载的位置 -->
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>
  		org.springframework.web.servlet.DispatcherServlet
  	</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springmvc.xml</param-value>
  	</init-param>
  	<load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>

3. 编写配置文件

(1)springmvc.xml

    <!-- 开启注解编程 -->
	<context:component-scan base-package="com.action"></context:component-scan>
	<!-- 实现mvc的配置 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	<!-- <mvc:default-servlet-handler/> -->
	

(2)applicationContext.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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
    	http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    	http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
    	http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.1.xsd
    	http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
    	http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
">
	
</beans>

【提示】空文件不需要多余的配置。至此上面的配置都是SpringMVC的配置,若是已经有配好的SpringMVC的环境,上面的步骤可以省略!

4. 导入Shiro的Jar包

5. 在web.xml中配置ShiroFilter

  <!-- 配置ShiroFilter -->
  <filter>
        <filter-name>shiroFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
            <param-name>targetFilterLifecycle</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

【提示】

(1)Shiro 提供了与 Web 集成的支持,其通过一个ShiroFilter 入口来拦截需要安全控制的URL,然后进行相应的控制。

(2)ShiroFilter 类似于如 Strut2/SpringMVC 这种web 框架的前端控制器,是安全控制的入口点,其负责读取配置(如ini 配置文件),然后判断URL 是否需要登录/权限等工作。

 

6. 在applicationContext.xml中配置

(1)缓存

  • 导入jar包

          

  • (2)classpath下的ehcache.xml
<ehcache>

    <diskStore path="java.io.tmpdir/shiro-spring-sample"/>
	
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="false"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"/>

   
    <cache name="shiro-activeSessionCache"
           maxElementsInMemory="10000"
           eternal="true"
           overflowToDisk="true"
           diskPersistent="true"
           diskExpiryThreadIntervalSeconds="600"/>

    <cache name="org.apache.shiro.realm.SimpleAccountRealm.authorization"
           maxElementsInMemory="100"
           eternal="false"
           timeToLiveSeconds="600"
           overflowToDisk="false"/>

</ehcache>

【提示】后期文章会更新springboot整合shiro,缓存用redis。

               https://blog.youkuaiyun.com/u010514380/article/details/82185451

 

(2)自定义realm,实现Realm接口

package com.realms;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.realm.Realm;

public class ShiroRealm implements Realm{

	@Override
	public AuthenticationInfo getAuthenticationInfo(AuthenticationToken arg0) throws AuthenticationException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean supports(AuthenticationToken arg0) {
		// TODO Auto-generated method stub
		return false;
	}

}

 

7. 一切完事之后启动web项目测试

    输入一切请求都会被转发到login.jsp界面!

【提示】这里面是需要配置的 

二、简要总结

1. shirofilter工作原理

2. DelegatingFilterProxy 作用是自动到 Spring 容器查找名字为 shiroFilter(filter-name)的 bean 并把所有 Filter 
的操作委托给它。

这也就是为什么web.xml的shirofilter的fitername要与applicationContext中的bean的id保持一致的原因

4. [urls] 部分的配置,其格式是: “url=拦截器[参数],拦截器[参数]”。如果当前请求的 url 匹配 [urls] 部分的某个 url 模式,将会执行其配置的拦截器

5. URL匹配模式

url 模式使用 Ant 风格模式
• Ant 路径通配符支持 ?、*、**,注意通配符匹配不包括目录分隔符“/”
(1)  ?:匹配一个字符,如 /admin? 将匹配 /admin1,但不匹配 /admin 或 /admin/
(2)   *:匹配零个或多个字符串,如 /admin 将匹配 /admin、/admin123,但不匹配 /admin/1;
(3)   **:匹配路径中的零个或多个路径,如 /admin/** 将匹配 /admin/a 或 /admin/a/b

6. URL匹配顺序

URL 权限采取第一次匹配优先的方式,即从头开始使用第一个匹配的 url 模式对应的拦截器链

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值