集成 Spring

  1. 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>
  1. 在 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>
  2. 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>

集成中的坑

  1. 每次启动项目, ehcache 都去访问官网,检验是否更新,在 <ehcache> 里面添加如下配置即可;

    <ehcache name="ehcache" updateCheck="false">
  2. 启动报错 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的更多相关文章

  1. Spring Boot2 系列教程(三十二)Spring Boot 整合 Shiro

    在 Spring Boot 中做权限管理,一般来说,主流的方案是 Spring Security ,但是,仅仅从技术角度来说,也可以使用 Shiro. 今天松哥就来和大家聊聊 Spring Boot ...

  2. Spring Boot 添加Shiro支持

    前言: Shiro是一个权限.会话管理的开源Java安全框架:Spring Boot集成Shiro后可以方便的使用Session: 工程概述: (工程结构图) 一.建立Spring Boot工程 参照 ...

  3. spring boot整合shiro

    安全框架Shiro和Spring Security比较,本文主要围绕Shiro进行学习 一 Shiro 是一个强大而灵活的开源安全框架,能够清晰的处理认证 授权 管理会话以及,密码加密 01 .认证与 ...

  4. 基于Spring Boot和Shiro的后台管理系统FEBS

    FEBS是一个简单高效的后台权限管理系统.项目基础框架采用全新的Java Web开发框架 —— Spring Boot 2.0.3,消除了繁杂的XML配置,使得二次开发更为简单:数据访问层采用Myba ...

  5. 快速搭建Spring Boot + Apache Shiro 环境

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.Apache Shiro 介绍及概念 概念:Apache Shiro是一个强大且易用的Java安全框 ...

  6. spring boot 和shiro的代码实战demo

    spring boot和shiro的代码实战 首先说明一下,这里不是基础教程,需要有一定的shiro知识,随便百度一下,都能找到很多的博客叫你基础,所以这里我只给出代码. 官方文档:http://sh ...

  7. kubebuilder实战之二:初次体验kubebuilder

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  8. 基于Spring + Spring MVC + Mybatis + shiro 高性能web构建

    一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.AngularJS,做了大量的研究,对前后端交互有了更深层次的认识. 今天抽个时间写这篇文章,我有预感,这将是一篇很详细的文章,详 ...

  9. [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.

    前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...

随机推荐

  1. Laravel中的Storage::disk

    Laravel中的Storage::disk 一.总结 一句话总结: Storage的disk的路径和file的路径都是一回事,都是config/filesystems.php配置文件中disks 比 ...

  2. [转]Myeclipse之web项目的部署(发布)流程

    在myeclipse下新建web工程abc.系统设置默认如下: 项目保存位置:workspace目录\abc Source文件夹:src,保存所有的java类文件(.java文件)和xml文件. We ...

  3. 笔记 - DBSherlock: A Performance Diagnostic Tool for Transactional Databases

    OLTP系统的问题很难排查和定位,这就是为什么要花那么多钱去请DBA 因为TP系统的请求很多都是毫秒级别,而且同时有大量的并发,所以由于资源,或随机的原因导致的问题,很难去定位根因 哪怕数据库系统尤其 ...

  4. GB28181技术基础之2 - H264与PS封包

    二. PS封包 PS 是 GB28181 规定的标准封包格式(也是存储格式),在讲 PS 之前,先介绍几种相关的 数据格式概念: 1)ES 基本流 (Elementary Streams)是直接从编码 ...

  5. 004 springboot文件上传

    关于文件上传,在spring cloud会再经过配置文件的处理,在spring boot则不需要,在这里写一个文件上传的接口. 单文件上传,如果以后写多文件上传再进行补充. 1.文件目录 2.控制器程 ...

  6. 小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_17、SpringBootTest单元测试实战

    笔记 1.@SpringBootTest单元测试实战     简介:讲解SpringBoot的单元测试         1.引入相关依赖              <!--springboot程 ...

  7. mysql的备份与还原,安装(window)

    之前一直使用的navicat的自动的备份功能.但是在使用navicat的还原功能时,贼慢. 今天要做的是window服务器上将mysql单个数据库备份.然后还原到我的mac笔记本上. 本来以为很快的, ...

  8. ubuntu二进制包安装openresty

    date: 2019-08-01 17:59:59 author: headsen chen # 导入我们的 GPG 密钥: wget -qO - https://openresty.org/pack ...

  9. Python之Pandas绘图,设置显示中文问题

    # -*- coding: utf-8 -*- # author:baoshan import pandas as pd import matplotlib.pyplot as plt plt.rcP ...

  10. maven项目新检出后不编译爬坑记 及 mvn clean package报错 WagonTransporterFactory: java.util.NoSuchElementException 异常【我】

    从SVN新检出一个maven项目,配置好后,发现项目无法编译(只有一个test包中的代码显示编译报错,其他所有包中的代码都不编译,也不报错), 先注释掉报错的test包中的所有内容, 用Eclipse ...