Springboot + shiro 整合之Url拦截设置(转)
shiro 整合到springboot 还是比较简单的,只需要新建一个spring-shiro.xml的配置文件:
- <span style="font-size:14px;"><?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-4.0.xsd"
- default-lazy-init="true">
- <description>Shiro Configuration</description>
- <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
- <property name="realm" ref="ShiroRealm" />
- </bean>
- <!-- 項目自定义的Realm -->
- <bean id="ShiroRealm" class="org.spring.springboot.interceptor.shiro.ShiroRealm" ></bean>
- <!-- Shiro Filter -->
- <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
- <property name="securityManager" ref="securityManager" />
- <property name="loginUrl" value="/login" />
- <property name="successUrl" value="/backstage/index" />
- <property name="unauthorizedUrl" value="/login" />
- <!-- anon:匿名拦截器,即不需要登录即可访问;一般用于静态资源过滤
- authc:如果没有登录会跳到相应的登录页面登录
- user:用户拦截器,用户已经身份验证/记住我登录的都可 -->
- <property name="filterChainDefinitions">
- <value>
- </span> /plugins/** = anon
- /images/** = anon
- /css/** = anon
- /** = authc<span style="font-size:14px;">
- </value>
- </property>
- </bean>
- <!-- 缓存管理器 使用Ehcache实现-->
- <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
- <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>
- </bean>
- <!-- AOP式方法级权限检查 -->
- <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
- <property name="proxyTargetClass" value="true" />
- </bean>
- <!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
- <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
- </span>
<?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-4.0.xsd"
default-lazy-init="true"><description>Shiro Configuration</description> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="ShiroRealm" />
</bean> <!-- 項目自定义的Realm -->
<bean id="ShiroRealm" class="org.spring.springboot.interceptor.shiro.ShiroRealm" ></bean> <!-- Shiro Filter -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/login" />
<property name="successUrl" value="/backstage/index" />
<property name="unauthorizedUrl" value="/login" />
<!-- anon:匿名拦截器,即不需要登录即可访问;一般用于静态资源过滤
authc:如果没有登录会跳到相应的登录页面登录
user:用户拦截器,用户已经身份验证/记住我登录的都可 -->
<property name="filterChainDefinitions">
<value>
</span> /plugins/** = anon
/images/** = anon
/css/** = anon
/** = authc<span style="font-size:14px;">
</value>
</property>
</bean>
<!-- 缓存管理器 使用Ehcache实现-->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>
</bean> <!-- AOP式方法级权限检查 -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
<property name="proxyTargetClass" value="true" />
</bean> <!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
</beans>
自定义的身份验证就不多说了,主要还是关注本文重点,解释一下filterChainDefinitions:
1、springboot 默认访问路径 resources 下的 static(好像最高级别),它就这么定的爱咋咋地。。,所以静态资源文件(js,css,jpg等)的访问配置要去掉 /static (/static/js/**=anon)
2、/** 必须要放在最下面 ,注意是必须
我个人觉得springboot 的WebMvcConfigurerAdapter bean注解方式不如以前xml方便,只需要导入一下这个xml 文件,并将这个文件放到classpath 下就OK了。
@Configuration //标注此文件为一个配置项,spring boot才会扫描到该配置。
@ImportResource(locations={"classpath:spring-shiro.xml"})
public class BootConfiguration extends WebMvcConfigurerAdapter {
}
上面这个类可以随便放src下的任意目录,springboot 它会找到的。
2018-01-22更新
本次的碰上的问题就是用上面第二条解决的,在配置Shiro的时候,给静态资源加了"/static"没有造成无法访问!但是在引用静态资源的时候,要加上"/static"否则访问不到,比如:
```xml
```
Springboot + shiro 整合之Url拦截设置(转)的更多相关文章
- springboot+shiro整合教程
进阶教程: 1. springboot+shiro+redis(单机redis版)整合教程 2. springboot+shiro+redis(集群redis版)整合教程 3.springboot+s ...
- springboot+shiro+redis(单机redis版)整合教程-续(添加动态角色权限控制)
相关教程: 1. springboot+shiro整合教程 2. springboot+shiro+redis(单机redis版)整合教程 3. springboot+shiro+redis(集群re ...
- springboot+shiro+redis(集群redis版)整合教程
相关教程: 1. springboot+shiro整合教程 2. springboot+shiro+redis(单机redis版)整合教程 3.springboot+shiro+redis(单机red ...
- springboot+shiro+redis(单机redis版)整合教程
相关教程: 1. springboot+shiro整合教程 2. springboot+shiro+redis(集群redis版)整合教程 3.springboot+shiro+redis(单机red ...
- springboot+shiro
作者:纯洁的微笑 出处:http://www.ityouknow.com/ 这篇文章我们来学习如何使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公 ...
- Spring Cloud之路:(七)SpringBoot+Shiro实现登录认证和权限管理
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/sage_wang/article/details/79592269一.Shiro介绍1.Shiro是 ...
- springboot+shiro+redis项目整合
介绍: Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码学和会话管理.使用Shiro的易于理解的API,您可以快速.轻松地获得任何应用程序,从最小的移动应用程序到最 ...
- SpringBoot+Shiro+mybatis整合实战
SpringBoot+Shiro+mybatis整合 1. 使用Springboot版本2.0.4 与shiro的版本 引入springboot和shiro依赖 <?xml version=&q ...
- Shiro入门这篇就够了【Shiro的基础知识、回顾URL拦截】
前言 本文主要讲解的知识点有以下: 权限管理的基础知识 模型 粗粒度和细粒度的概念 回顾URL拦截的实现 Shiro的介绍与简单入门 一.Shiro基础知识 在学习Shiro这个框架之前,首先我们要先 ...
随机推荐
- Yahoo!团队:网站性能优化的35条黄金守则(转)
Excetional Performance 团队总结出了一系列可以提高网站速度的方法.可以分为 7大类 35条.包括内容 .服务器 . CSS . JavaScript .Cookie .图片 .移 ...
- Spring Cloud Netflix Eureka client源码分析
1.client端 EurekaClient提供三个功能: EurekaClient API contracts are:* - provide the ability to get Instance ...
- python2 python3 m2crypto 安装(rsa 私钥文件加密)
转自作者:大道至简_Andy 原文链接:https://www.jianshu.com/p/b308357ef649 第一种方式:使用apt-get(以Python2版本进行测试的) sudo apt ...
- understand软件使用教程(转)
源代码阅读工具(Scientific Toolworks Understand)的特色 1.支持多语言:Ada, C, C++, C#, Java, FORTRAN, Delphi, Jovial, ...
- 阿里云 CentOS7.4 环境安装mysql5.7
1. 删除默认安装的数据库,无所谓的请略过 据说CentOS7.x版本会默认安装mariadb数据库,我有点强迫症,故卸载之: rpm -qa|grep mariadb yum remove mari ...
- Android Support 包里到底有什么
大家假设喜欢我的博客,请关注一下我的微博,请点击这里(http://weibo.com/kifile),谢谢 转载请标明出处(http://blog.csdn.net/kifile),再次感谢 随着 ...
- Android广告轮播图实现
先看效果 第一步,布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmln ...
- MyEclipse 2016 安装/破解
MyEclipse2016 C1 已经出现了!感觉好像不错的样子! 不多说了,开整... 好熟悉的界面,点击Next! 如上图标注1所示,请修改安装目录! 根据自己的喜好可以选择不同的版本,也可以安装 ...
- 为RecyclerView添加item的点击事件
RecyclerView侧重的是布局的灵活性,虽说可以替代ListView但是连基本的点击事件都没有,这篇文章就来详细讲解如何为RecyclerView的item添加点击事件,顺便复习一下观察者模式. ...
- js面向对象3-继承
一.了解继承 首先我们一起了解下js中继承,其实继承就是后辈继承前辈的属性和方法. 二.继承的方法 从父类继承属性和方法 这是对象冒充的方法,模仿java的继承方法.实现的原理是,通过改变父类的执行 ...