(二)spring初次遇见shiro
集成 Spring
- pom.xml 添加shiro相关的依赖
我使用的版本是
${version.shiro}
—> 1.3.2
<!-- shiro配置 -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>${version.shiro}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>${version.shiro}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-aspectj</artifactId>
<version>${version.shiro}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>${version.shiro}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>${version.shiro}</version>
</dependency>
在 web.xml 中作如下配置:
<!-- 配置 shiro 的 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>
<!--
可以配置 targetBeanName 属性,指定 applicationContext-shiro 中的 ShiroFilterFactoryBean id值
-->
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在
spring
配置文件中添加如下配置一般都是新建一个配置文件,比如博主这里是新建一个
applicationContext-shiro.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- Sample RDBMS data source that would exist in any application - not Shiro related. -->
<!--<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->
<!--<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>-->
<!--<property name="url" value="jdbc:hsqldb:mem:shiro-spring"/>-->
<!--<property name="username" value="sa"/>-->
<!--</bean>--> <!-- =========================================================
配置 securityManager ,配置三个属性
========================================================= -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="cacheManager" ref="cacheManager"/>
<!-- Single realm app. If you have multiple realms, use the 'realms' property instead. -->
<!--<property name="sessionMode" value="native"/>-->
<property name="realm" ref="jdbcRealm"/>
</bean> <!--
解决 Resolved SubjectContext context session is invalid.
Ignoring and creating an anonymous (session-less) Subject instance. 问题出现的原因:好像是cookie名字冲突了
-->
<bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<property name="sessionIdCookieEnabled" value="true"/>
<property name="sessionIdCookie" ref="sessionIdCookie"/>
</bean>
<bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
<constructor-arg name="name" value="jeesite.session.id"/>
</bean> <!-- =========================================================
配置 cacaheManager ,内部可以配置自己想用的缓存框架,这里配置成 hibernate 的 ehcache
========================================================= -->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>
</bean> <!-- =========================================================
配置 Realm
这里配置自己实现的 Realm
========================================================= -->
<bean id="jdbcRealm" class="cn.hyc.shiro.realm.ShiroRealm"/> <!-- =========================================================
管理 springIOC 容器中的 shiro bean 生命周期方法
========================================================= -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> <!-- =========================================================
启用 IOC 容器中 shiro 的注解,但是必须在配置了 lifecycleBeanPostProcessor 以后,该项配置才会生效
========================================================= -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor"/>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean> <!-- =========================================================
配置 shiroFilter,
细节:
1、这里的 id 的值,必须和在 web.xml 中配置的 DelegatingFilterProxy 的过滤器的名字一样
2、如果 web.xml DelegatingFilterProxy 指定了targetBeanName属性,则跟属性值一样
========================================================= -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<!--下面三个属性依次为,登陆页面(入口),登陆成功页面、没有权限页面-->
<property name="loginUrl" value="/login.jsp"/>
<property name="successUrl" value="/index.jsp"/>
<property name="unauthorizedUrl" value="/unauthorized.jsp"/> <!--
配置哪些页面需要受保护,以及访问这些页面需要的权限
1、anon 可以匿名访问,即不用登陆也能访问
2、authc 需要认证(登陆)才能访问,如果没有登陆访问这些页面,shiro 会自动的重定向到入口文件;
3、logout 退出
其中URL,页面需要写出后缀名,访问控制器不需要写出具体的后缀名
-->
<property name="filterChainDefinitions">
<value>
/login.jsp = anon
/index.jsp = authc
/ = authc
/** = authc
/logout = logout
</value>
</property>
</bean> </beans>
集成中的坑
每次启动项目,
ehcache
都去访问官网,检验是否更新,在<ehcache>
里面添加如下配置即可;<ehcache name="ehcache" updateCheck="false">
启动报错
org.apache.shiro.session.UnknownSessionException: There is no session with id
这是cookie 名字冲突导致的;做如下更改:
<!-- =========================================================
配置 securityManager ,配置三个属性
========================================================= -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="cacheManager" ref="cacheManager"/>
<!-- 一定一定要注释掉这个属性,否则添加下面的 cookie 名字配置,也无效 -->
<!--<property name="sessionMode" value="native"/>-->
<property name="realm" ref="jdbcRealm"/>
</bean>
自定义
cookie
的名字:<!--
解决 Resolved SubjectContext context session is invalid.
Ignoring and creating an anonymous (session-less) Subject instance. 问题出现的原因:好像是cookie名字冲突了
-->
<bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<property name="sessionIdCookieEnabled" value="true"/>
<property name="sessionIdCookie" ref="sessionIdCookie"/>
</bean>
<bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
<constructor-arg name="name" value="jeesite.session.id"/>
</bean>
shiroFilter 的工作原理
shiroFilter
是一个拦截器,浏览器的任何访问都会被拦截到;
其中 loginUrl
是入口,在配置文件中进行配置;
权限配置细节
在上面配置 shiroFilter
的时候,使用了如下配置:
注意写出后缀名:
<property name="filterChainDefinitions">
<value>
/logout.action = logout
/index.jsp = anon
/shiro/isAllowLogin.action = anon
/ = authc
/** = authc
</value>
</property>
其具体规则如下:
(二)spring初次遇见shiro的更多相关文章
- Spring Boot2 系列教程(三十二)Spring Boot 整合 Shiro
在 Spring Boot 中做权限管理,一般来说,主流的方案是 Spring Security ,但是,仅仅从技术角度来说,也可以使用 Shiro. 今天松哥就来和大家聊聊 Spring Boot ...
- Spring Boot 添加Shiro支持
前言: Shiro是一个权限.会话管理的开源Java安全框架:Spring Boot集成Shiro后可以方便的使用Session: 工程概述: (工程结构图) 一.建立Spring Boot工程 参照 ...
- spring boot整合shiro
安全框架Shiro和Spring Security比较,本文主要围绕Shiro进行学习 一 Shiro 是一个强大而灵活的开源安全框架,能够清晰的处理认证 授权 管理会话以及,密码加密 01 .认证与 ...
- 基于Spring Boot和Shiro的后台管理系统FEBS
FEBS是一个简单高效的后台权限管理系统.项目基础框架采用全新的Java Web开发框架 —— Spring Boot 2.0.3,消除了繁杂的XML配置,使得二次开发更为简单:数据访问层采用Myba ...
- 快速搭建Spring Boot + Apache Shiro 环境
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一.Apache Shiro 介绍及概念 概念:Apache Shiro是一个强大且易用的Java安全框 ...
- spring boot 和shiro的代码实战demo
spring boot和shiro的代码实战 首先说明一下,这里不是基础教程,需要有一定的shiro知识,随便百度一下,都能找到很多的博客叫你基础,所以这里我只给出代码. 官方文档:http://sh ...
- kubebuilder实战之二:初次体验kubebuilder
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- 基于Spring + Spring MVC + Mybatis + shiro 高性能web构建
一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.AngularJS,做了大量的研究,对前后端交互有了更深层次的认识. 今天抽个时间写这篇文章,我有预感,这将是一篇很详细的文章,详 ...
- [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.
前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...
随机推荐
- 重写mybatis的字符串类型处理器
1.简介 无论是 MyBatis 在预处理语句(PreparedStatement)中设置一个参数时,还是从结果集中取出一个值时, 都会用类型处理器将获取的值以合适的方式转换成 Java 类型. St ...
- 查看CPU占用工具
recording_5492_1.jfr jcmd 5942 JFR.start delay=1s duration=200s name=serverRecording filename=./reco ...
- Vue学习手记08-vue-cli的启动过程
分两种情况---无路由和有路由 无路由 看到启动页面 在文件main.js( vue项目的入口文件)中 这里可以看到,生成了一个全局的vue实例,绑定在了#app上面,也就是在文件index.html ...
- make 实例 二 V56
######################################################################### # # Makefile used for buil ...
- 静态导入(static import)
1.传统静态方法的调用方式 定义一个Common类,里面有静态方法和静态常量 package com.example.common; public class Common { public stat ...
- Leetcode: Campus Bikes II
On a campus represented as a 2D grid, there are N workers and M bikes, with N <= M. Each worker a ...
- Flutter 中SimpleDialog简单弹窗使用
import 'package:flutter/material.dart'; import 'dart:async'; enum Option { A, B, C } class SimpleDia ...
- Angular中使用ECharts图表
1.安装: npm install echarts --save 2.在 TypeScript 文件中导入echarts import * as echarts from 'echarts'; 3.根 ...
- openresty开发系列15--lua基础语法4表table和运算符
openresty开发系列15--lua基础语法4表table和运算符 lua中的表table 一)table (表)Table 类型实现了一种抽象的"关联数组".即可用作数组,也 ...
- 测试一下windowsLiveWriter
一个是看看这个东西能不能发布出博客,还有一个就是准备开始写博客了,所以随便写个作为开始吧,我不想多说什么目标啊,什么的,所以就这一句简单的一句话就够了.