这几天因为项目需要,学习了下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. 【小尝试】Java获取慕课网原有路径课程列表

    作为一个老慕课网(https://www.imooc.com/)粉丝,还记得最开始的慕课网有很多免费的路径课程,练习什么的也特别详细,是入门一门语言的好方法. 现在慕课网发展起来了收费模式,添加了很多 ...

  2. 可以提高php编程效率的20个要点

    整理了可以提高php编程效率的20个要点,发博客记录一下,需要的朋友可以参考.    1.如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍. 2.$row['id' ...

  3. 树莓派如何连接WIFI

    hello,大家好,我是叶秋! 上一期呢,给大家介绍了如何给树莓派安装系统,有细心的朋友就会发现上一期安装系统的文章漏了一点点知识,不知道机智的你是否有发现呢~~(尴尬

  4. C++ & java小结

    JAVA类: 每个类都属于一个包,private成员:只有该类可以访问,子类不能访问 Public:其他类可以访问 Protected: 只有本包内的类可以访问 如果在声明class时不加public ...

  5. socketpair通信

    1.线程间通信(参考安卓源码InputTransport.cpp) #include <pthread.h> #include <sys/types.h> /* See NOT ...

  6. gem install ruby-odbc失败

    解决: brew install unixodbc gem install ruby-odbc -v '0.99998'

  7. C#/STM32 WAV转byte WAV数据格式

    最近在做STM32音乐播放器,选取了最容易做的WAV格式. 为了更方便开发自己做了一个WAV转Byte的小上位机 附软件下载链接 链接:https://pan.baidu.com/s/1Zz7bczZ ...

  8. postgresql 数据库schema 复制

    ------ --- 导出 pg_dump -h *.*.*.* -p 5432 -d you_databasename -n you_schema -f you_sqlfile.sql ---- 替 ...

  9. jquery table 发送两次请求 解惑

    版本1.10 以下链接为一个较低版本解决方案: http://blog.csdn.net/anmo/article/details/17083125 而我的情况有点作, 情况描述: 1,一个页面两个t ...

  10. vue中cssModules理解可以用于加密和避免重复使用

    cssModules可以用于加密和避免重复使用,也就是说可以在当前vue文件中写的样式会生成独一无二的名字,在其他vue文件中是无法调用的, 一.可以直接配cssModules 第一步,配置vue-l ...