这几天因为项目需要,学习了下shiro,由此留下一些记录,也希望对初学shiro的朋友有帮助。

springboot 是这两年新兴起来的一个项目,它的出现是为了减少springmvc开发过程中需要引入各种的jar包,各种xml配置文件,它充分利用了JavaConfig的配置模式以及“约定优于配置”的理念,帮开发者配置大部分需要的东西,在github上的springboot项目里面,提供了很多列子,

而apache shiro 是一个轻量级的身份验证与授权框架,与spring security 相比较,简单易用,灵活性高,springboot本身是提供了对security的支持,毕竟是自家的东西。springboot暂时没有集成shiro,这得自己配。

网上找了一些资料,配置shiro的,有很多需要在web.xml、application.xml里面各种配置,然而springboot 并没有这些,而且springboot提倡无xml,使用javaconfig的配置方式,对这个不是很熟悉,但有人使用javaconfig的方式配置了shiro,参见这位csdn里面一位同学的博客spring boot 集成shiro的配置,下载了demo,然后模仿着成功配置了下。但习惯了xml的配置方式,感觉javaconfig的方式并不是很直观,于是自己又把它换成了xml的方式。以下是主要的配置过程

首先是spring-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" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- =========================================================
Shiro Components
========================================================= --> <!-- 缓存管理器 使用Ehcache实现 -->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:app/config/ehcache-shiro.xml" />
</bean> <!-- 虚需要自己写的realm实现类 充当shiro和应用的安全数据的桥梁 -->
<bean id="MonitorRealm" class="com.test.MonitorRealm"></bean> <!-- 安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realms">
<list>
<ref bean="MonitorRealm" />
</list>
</property>
<property name="cacheManager" ref="cacheManager" />
</bean> <!-- Shiro生命周期处理器 -->
<!-- 官方对其的解释是 http://shiro.apache.org/static/1.2.1/apidocs/org/apache/shiro/spring/LifecycleBeanPostProcessor.html
This post processor makes it easier to configure Shiro beans in Spring,
since the user never has to worry about whether or not
if they have to specify init-method and destroy-method bean attributes.
大意是使shiro bena 注入更加方便 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" /> <!-- Shiro的Web过滤器 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/user-web/login" />
<property name="unauthorizedUrl" value="/unauthorized " />
<property name="filters">
<util:map>
<entry key="authc">
<!-- 身份验证拦截器,默认为FormAuthenticationFilter,但 PassThruAuthenticationFilter功能相对强大,详情见
https://shiro.apache.org/static/1.2.1/apidocs/org/apache/shiro/web/filter/authc/PassThruAuthenticationFilter.html-->
<bean class="org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter" />
</entry>
</util:map>
</property> <!-- shiro的强大的拦截器链,可以匹配全部的url,并根据配置进行拦截-->
<property name="filterChainDefinitions">
<value>
# 无需认证便可以访问的的文件放在前面
/js/* = anon
/css/* = anon
/img/* = anon
/images/* = anon /user-web/login = anon
/logout = logout /user-web/* = authc
/backend-web/* = authc
</value>
</property>
</bean> <!-- 开启Shiro的注解(如@RequiresRoles,@RequiresPermissions),需借助SpringAOP扫描使用Shiro注解的类,并在必要时进行安全逻辑验证 -->
<!-- 这里要配置以下两个bean,在这之前要配置好lifecycleBeanPostProcessor--> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
<!-- 加上下面这一句是为了解决If the controller requires proxying (e.g. due to @Transactional), please use class-based proxying 的报错-->
<!-- 参考http://www.cnblogs.com/digdeep/p/4624998.html 会发现上面错误是 Spring AOP 不同配置方式产生的冲突问题 -->
<property name="proxyTargetClass" value="true"/>
</bean>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager" />
</bean> <!-- 异常拦截 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="org.apache.shiro.authz.UnauthorizedException">
/unauthorized <!-- 未授权处理页 -->
</prop>
<prop key="org.apache.shiro.authz.UnauthenticatedException">
/user-web/login <!-- 身份没有验证 -->
</prop>
</props>
</property>
</bean>
</beans>

接着是Ehcache.xml文件。

ehcache是一个纯Java的进程内缓存框架,相关介绍可以看这里

<ehcache updateCheck="false" name="shiroCache">
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
/>
</ehcache>

springboot加载xml配置文件

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@SpringBootApplication
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(new String[] {
"classpath*:app/config/spring-*.xml",
"classpath*:app/config/spring-session-redis.xml",
"classpath*:/user/captcha.xml"
//....
}, args);
}
}

这样。spingboot以xml形式配置shiro就完成了,后面在controller的方法上面使用注解的的方式,就可以进行权限控制。

这里没有提供MonitorRealm类,里面要实现doGetAuthorizationInfo(授权)和doGetAuthenticationInfo(认证)两个方法,还有就是loginController里面要做一些改动,有需要的朋友可以参考这篇SpringMVC整合Shiro博文。

springboot 整合apache shiro的更多相关文章

  1. SpringBoot整合Apache Shiro权限验证框架

    比较常见的权限框架有两种,一种是Spring Security,另一种是Apache Shiro,两种框架各有优劣,个人感觉Shiro更容易使用,更加灵活,也更符合RABC规则,而且是java官方更推 ...

  2. SpringBoot整合Apache Shiro

    Subject 用户主体 (把操作交给SecurityManager)SecurityManager 安全管理器 (关联Realm)Realm   Shiro连接数据的桥梁 引入maven依赖 < ...

  3. springboot整合apache ftpserver详细教程(看这一篇就够了)

    原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/12192425.html,否则将追究法律责任!!! 一.Apache ftpserver相关 ...

  4. SpringBoot集成Apache Shiro

    笔者因为项目转型的原因,对Apache Shiro安全框架做了一点研究工作,故想写点东西以便将来查阅.之所以选择Shiro也是看了很多人的推荐,号称功能丰富强大,而且易于使用.实践下来的确如大多数人所 ...

  5. SpringMVC整合Apache Shiro

    关于什么是Shiro,可以查看这篇文章http://www.cnblogs.com/Laymen/articles/6117751.html 一.添加maven依赖 <dependency> ...

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

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

  7. SpringBoot整合Apache-CXF实践

    一.Apache CXF是什么? Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS . ...

  8. springboot整合shiro应用

    1.Shiro是Apache下的一个开源项目,我们称之为Apache Shiro.它是一个很易用与Java项目的的安全框架,提供了认证.授权.加密.会话管理,与spring Security 一样都是 ...

  9. 补习系列(6)- springboot 整合 shiro 一指禅

    目标 了解ApacheShiro是什么,能做什么: 通过QuickStart 代码领会 Shiro的关键概念: 能基于SpringBoot 整合Shiro 实现URL安全访问: 掌握基于注解的方法,以 ...

随机推荐

  1. CentOS7 yum命令

    1.yum 清理缓存 [hado@localhost /]# yum clean all [hado@localhost /]# rm -rf /var/cache/yum/*

  2. MySQL语句整理(二)

    数据库操作前的准备 -- 创建数据库 -- create database python_test_1 charset=utf8; -- 使用数据库 -- use python_test_1; -- ...

  3. js新开窗口避免浏览器拦截解决方案

    Ajax回调函数中,打开新窗口解决方案 var w = window.open(); $.post("GetHomToTestPaper", {homeworkName:homew ...

  4. thinkphp中的大字母的意思

    ThinkPHP 单字母函数 A() 内部实例化控制器 D() 实例化自定义模型类 M() 实例化一个基础模型类 R() 调用某个控制器的操作方法 L() 启用多语言的情况下,设置和获取当前的语言定义 ...

  5. tp js结合时间戳

    $(document).ready(function(){ $.extend({ show:function(){ } }); setInterval("show()",1000) ...

  6. python逻辑判断 () not and or

    python逻辑判断 () not and or 优先级关系:()>not>and>or 运算符示意 not –表示取反运算. and –表示取与运算. or –表示取或运算. or ...

  7. Python 入门(一)

    IDE 个人推荐  Pycharm : 比较好用,虽然没有中文,但是练练英语也不错,毕竟大同小异 基础语法 行与缩进 python最具特色的就是使用缩进来表示代码块,不需要使用大括号 {} . 缩进的 ...

  8. LeetCode初级算法的Python实现--数组

    LeetCode初级算法的Python实现--数组 # -*- coding: utf-8 -*- """ @Created on 2018/6/3 17:06 @aut ...

  9. 由OpenResty粘合的企业Web架构

    前言:    2012年2月章亦春(agentzh)在Tech-Club的一次线下聚会上以<由Lua 粘合的Nginx生态环境>为主题做了演讲,分析了企业Web架构的趋势,即一个看起来完整 ...

  10. ORB-SLAM (四)Initializer单目初始化

    一. 通过对极约束并行计算F和H矩阵初始化 VO初始化目的是为了获得准确的帧间相对位姿,并通过三角化恢复出初始地图点.初始化方法要求适用于不同的场景(特别是平面场景),并且不要进行人为的干涉,例如选取 ...