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这个框架之前,首先我们要先 ...
随机推荐
- QQ 特效学习 二 侧滑删除
上篇文章: http://www.cnblogs.com/xurui1995/p/5798631.html 今天来写不仅是qq而且在别的软件上也特别流行的侧滑删除 其实套路和前篇的一样,一个自定义Vi ...
- tomcat:web容器
Web容器和应用服务器除了你所说的request response之外还有很多其他重要的功能. 1. 多线程和并发处理,估计你写的socket程序支持不了几个并发访问吧 2. 安全性 3. 生命周期管 ...
- js--基于面向对象的组件开发及案例
组件的开发:多组对象之间想兄弟关系一样,代码复用的形式. 问题:1).参数不写会报错:利用对象复制————配置参数和默认惨啊书的覆盖关系(逻辑或也可以)2).参数特别多时会出现顺序问题:json解决 ...
- userdel---删除用户及相关文件
userdel命令 userdel命令用于删除给定的用户,以及与用户相关的文件.若不加选项,则仅删除用户帐号,而不删除相关文件. 语法 userdel(选项)(参数) 选项 -f:强制删除用户,即 ...
- Python批量重命名指定目录下文件的两种方法
#法一 import os path = "C://Python34//" for file in os.listdir(path): if os.path.isfile(os.p ...
- 35.Node.js GET/POST请求
转自:http://www.runoob.com/nodejs/nodejs-module-system.html 在很多场景中,我们的服务器都需要跟用户的浏览器打交道,如表单提交. 表单提交到服务器 ...
- scrapy爬取知乎某个问题下的所有图片
前言: 1.仅仅是想下载图片,别人上传的图片也是没有版权的,下载来可以自己欣赏做手机背景但不商用 2.由于爬虫周期的问题,这个代码写于2019.02.13 1.关于知乎爬虫 网上能访问到的理论上都能爬 ...
- 推广一下新Blog www.hrwhisper.me
新博客地址:www.hrwhisper.me 欢迎互访加友链~
- leaf cell
leaf cell是否可以理解为设计中与或非门等这些基本的单元?
- [TS] Implement a singly linked list in TypeScript
In a singly linked list each node in the list stores the contents of the node and a reference (or po ...