springboot 整合apache shiro
这几天因为项目需要,学习了下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的更多相关文章
- SpringBoot整合Apache Shiro权限验证框架
比较常见的权限框架有两种,一种是Spring Security,另一种是Apache Shiro,两种框架各有优劣,个人感觉Shiro更容易使用,更加灵活,也更符合RABC规则,而且是java官方更推 ...
- SpringBoot整合Apache Shiro
Subject 用户主体 (把操作交给SecurityManager)SecurityManager 安全管理器 (关联Realm)Realm Shiro连接数据的桥梁 引入maven依赖 < ...
- springboot整合apache ftpserver详细教程(看这一篇就够了)
原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/12192425.html,否则将追究法律责任!!! 一.Apache ftpserver相关 ...
- SpringBoot集成Apache Shiro
笔者因为项目转型的原因,对Apache Shiro安全框架做了一点研究工作,故想写点东西以便将来查阅.之所以选择Shiro也是看了很多人的推荐,号称功能丰富强大,而且易于使用.实践下来的确如大多数人所 ...
- SpringMVC整合Apache Shiro
关于什么是Shiro,可以查看这篇文章http://www.cnblogs.com/Laymen/articles/6117751.html 一.添加maven依赖 <dependency> ...
- 快速搭建Spring Boot + Apache Shiro 环境
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一.Apache Shiro 介绍及概念 概念:Apache Shiro是一个强大且易用的Java安全框 ...
- SpringBoot整合Apache-CXF实践
一.Apache CXF是什么? Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS . ...
- springboot整合shiro应用
1.Shiro是Apache下的一个开源项目,我们称之为Apache Shiro.它是一个很易用与Java项目的的安全框架,提供了认证.授权.加密.会话管理,与spring Security 一样都是 ...
- 补习系列(6)- springboot 整合 shiro 一指禅
目标 了解ApacheShiro是什么,能做什么: 通过QuickStart 代码领会 Shiro的关键概念: 能基于SpringBoot 整合Shiro 实现URL安全访问: 掌握基于注解的方法,以 ...
随机推荐
- html+css3 实现各种loading效果
效果见下图 代码: 建议直接去本人github上浏览代码 https://github.com/wuliqiangqiang/loading <!DOCTYPE html> <htm ...
- 【mongodb分片中mogos启动的报错】
- ASP.NET Core学习网站推荐
跟大家推荐一个不错的学习.NET Core 的网站,这个网站的视频是付费的,但是录视频的都是.NET Core的大佬们,个人觉得很不错推荐出来 video.jessetalk.cn
- JS 时间格式化,模拟PHP date,时间格式化封装函数
Date.prototype.Format = function (fmt) { var o = { "Y": this.getFullYear(), "m": ...
- excel 开头 结尾,中间 类似 SQL like ab% ,%ab ,%ab%
excel 开头 结尾,中间 类似 SQL like 'ab%' ,'%ab' ,'%ab%' 在R2 单元格 查找 ab开头,ab结尾 =Search("ab",R ...
- flask的查询,一对多,多对多
模型的关联: 一对多 class Role(db.Model): us = db.relationship('User',backref='role',lazy='dynamic') class Us ...
- Python学习知识库
2017年10月16日 1. too broad exception clause 捕获的异常过于宽泛了,没有针对性,应该指定精确的异常类型场景: def check_data_type(column ...
- c语言中 *p++ 和 (*p)++ 有什么区别?以及C语言运算符的优先级。整理。
*p++是指下一个地址. (*p)++是指将*p所指的数据的值加一. C编译器认为*和++是同优先级操作符,且都是从右至左结合的,所以*p++中的++只作用在p上,和*(p++)意思一样:在(*p)+ ...
- MongoDB入门---数据库&&&集合的基本操作
MongoDB作为一种nosql的数据库,它自己本身的增伤改查还有数据库集合的创建和展示与一般的数据库较之是有一部分差别的.我们今天就来看一下MongoDB的一些基本操作. 首先呢,就是先来数据 ...
- windows 设置tomcat为自动启动服务
1.下载免安装tomcat包,解压 2.配置环境变量: 点击新建,创建一个 变量名为:CATALINA_HOME 变量值为:tomcat解压文件的位置, 例如 F:\apache-tomcat ...