shiro 集成spring 配置 学习记录(一)

首先当然是项目中需要增加shiro的架包依赖:

 1 <!-- shiro -->
 2             <dependency>
 3                 <groupId>org.apache.shiro</groupId>
 4                 <artifactId>shiro-core</artifactId>
 5                 <version>${shiro.version}</version>
 6                 <exclusions>
 7                     <exclusion>
 8                         <artifactId>slf4j-api</artifactId>
 9                         <groupId>org.slf4j</groupId>
10                     </exclusion>
11                 </exclusions>
12             </dependency>
13             <dependency>
14                 <groupId>org.apache.shiro</groupId>
15                 <artifactId>shiro-spring</artifactId>
16                 <version>${shiro.version}</version>
17             </dependency>
18             <dependency>
19                 <groupId>org.apache.shiro</groupId>
20                 <artifactId>shiro-ehcache</artifactId>
21                 <version>${shiro.version}</version>
22                 <exclusions>
23                     <exclusion>
24                         <artifactId>slf4j-api</artifactId>
25                         <groupId>org.slf4j</groupId>
26                     </exclusion>
27                 </exclusions>
28             </dependency>

1、shiro 总体来说就是过滤器的集合,首先在项目的web.xml里增加shiro的filter配置:如下

 1 <!--Shiro过滤器-->
 2     <filter>
 3         <filter-name>shiroFilter</filter-name>
 4         <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
 5         <init-param>
 6             <param-name>targetFilterLifecycle</param-name>
 7             <param-value>true</param-value>
 8         </init-param>
 9     </filter>
10     <filter-mapping>
11         <filter-name>shiroFilter</filter-name>
12         <url-pattern>/*</url-pattern>
13         <dispatcher>REQUEST</dispatcher>
14         <dispatcher>FORWARD</dispatcher>
15     </filter-mapping>

2、在项目的resource目录下 新增shiro的配置xml文件如:applicationContext-shiro.xml:

1)、增加 securityManager  的初始化;

2)、增加  shiroFilter  的配置, 注意 此配置文件中的bean  过滤器的 id  必须是 web.xml中的 filter-name值

3)、增加自定义的realm实现

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 5
 6     <description>Shiro安全配置</description>
 7
 8     <!--此Bean 只是增加登录时的验证码处理,不是shiro必须的配置-->
 9     <bean class="com.itzixi.web.utils.ItzixiCaptcha">
10         <property name="cacheManager" ref="shiroEhcacheManager"/>
11         <!-- 复用半小时缓存 -->
12         <property name="cacheName" value="itzixiCaptcha"/>
13     </bean>
14
15     <!-- 安全管理器 -->
16     <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
17         <property name="realm" ref="shiroDbRealm"></property>
18     </bean>
19
20     <!-- 用户授权信息Cache, 采用EhCache -->
21     <bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
22         <property name="cacheManagerConfigFile" value="classpath:shiro/ehcache-shiro.xml"/>
23     </bean>
24
25     <!-- 项目自定义的Realm -->
26     <bean id="shiroDbRealm" class="com.itzixi.web.shiro.ShiroDBRealm">
27     </bean>
28
29     <!-- Shiro Filter -->
30     <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
31         <!-- 安全管理器 -->
32         <property name="securityManager" ref="securityManager"/>
33         <!-- 默认的登陆访问url -->
34         <property name="loginUrl" value="/login.action"/>
35         <!-- 登陆成功后跳转的url -->
36         <property name="successUrl" value="/index.action"/>
37         <!-- 登录成功以后,没有权限访问的页面,会跳转到该url -->
38         <property name="unauthorizedUrl" value="/unauth.action"/>
39
40         <property name="filterChainDefinitions">
41             <value>
42                 <!--
43                     anon  不需要认证
44                     authc 需要认证
45                     user  验证通过或RememberMe登录的都可以
46                 -->
47                 /** = authc
48             </value>
49         </property>
50     </bean>
51 </beans>

经过如上的配置 基本完成了shiro初始化集成。

原文地址:https://www.cnblogs.com/yinfengjiujian/p/9073596.html

时间: 2024-08-29 12:27:53

shiro 集成spring 配置 学习记录(一)的相关文章

Spring Boot学习记录(二)--thymeleaf模板

Spring Boot学习记录(二)–thymeleaf模板 标签(空格分隔): spring-boot 自从来公司后都没用过jsp当界面渲染了,因为前后端分离不是很好,反而模板引擎用的比较多,thymeleaf最大的优势后缀为html,就是只需要浏览器就可以展现页面了,还有就是thymeleaf可以很好的和spring集成.下面开始学习. 1.引入依赖 maven中直接引入 <dependency> <groupId>org.springframework.boot</gr

Spring Boot学习记录(一)--环境搭建

Spring Boot学习记录(一)–环境搭建 标签(空格分隔): spring-boot 最近趁着下班闲时间学习spring-boot,记录下学习历程,最后打算实战一个API管理平台,下面开始环境配置. 1.工程结构 使用maven建立一个普通结构,因为spring-boot内嵌tomcat,所以打包只需要打包成jar就可以直接运行,所以并不像以前那样建立WEB程序了,目录如下,类可以先建立好放在那: 2.引入maven依赖 根据官方教程提示,直接引入parent就可以使用spring-boo

Spring Boot学习记录(三)--整合Mybatis

Spring Boot学习记录(三)–整合Mybatis 标签(空格分隔): spring-boot 控制器,视图解析器前面两篇都已弄好,这一篇学习持久层框架整合. 1.数据源配置 数据源使用druid,maven引入相关依赖,包括spring-jdbc依赖,mysql依赖 1.转换问题 配置的过程要学会为什么这样配置,而不是只学会了配置.这里我们可以和以前的配置方式对比: 以前版本 <!--配置数据库连接池Druid--> <bean id="dataSource"

shiro整合spring配置

shiro应用到项目中,一般都是通过spring来管理.下面就如何把shiro整理到spring中进行了讲解,及给出了配置的步骤: 一.pom.xml文件配置 本例子主要是介绍maven管理的web项目进行配置介绍,因此,首先需建立好一个maven管理的web项目(可参考本博客创建maven管理的web项目). pom.xml文件配置,主要是添加相关依赖的jar支持.因为整合到spring中需添加spring支持.具体配置参考配置代码: 1 <project xmlns="http://m

Spring配置错误记录

更多Spring问题由于发生时未记录而遗忘了~~~~~~~ 现在动动手 解决方案由于不是源头分析因而仅供参考!!! 严重: Exception sending context destroyed event to listener instance of class org.springframework.web.context.ContextLoaderListener java.lang.IllegalStateException: BeanFactory not initialized o

Shiro 集成Spring 使用 redis时 使用redisTemplate替代jedisPool(五)

1.添加依赖架包: 1 <dependency> 2 <groupId>org.springframework.data</groupId> 3 <artifactId>spring-data-redis</artifactId> 4 <version>${spring-data-redis.version}</version> 5 </dependency> 6 <!-- 用于jedis-spring-

Spring Boot学习记录

Spring Boot——开发新一代Spring Java应用 深入学习微框架:Spring Boot SpringBoot入门系列:第一篇 Hello World Spring Boot 入门

关于Spring定时器学习记录

Spring定时器: 1.时间表达式: 需要配置的有: 秒 0-59 , - * /   | 分 0-59 , - * /   | 小时 0-23 , - * /   | 日期 1-31 , - * ? / L W C   | 月份 1-12 或者 JAN-DEC , - * /   | 星期 1-7 或者 SUN-SAT , - * ? / L C #   | 年(可选) 留空, 1970-2099 , - * / 表达式示例: "0 0 12 * * ?" 每天中午12点触发 &q

spring boot 学习记录-(二)第一个程序

一.新建maven工程 二.项目构建 1.添加依赖 2.编写启动类 App.java 1 package org.yanjiemao; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 import org.springframework.context.annotation.Compon