shiro 整合到springboot 还是比较简单的,只需要新建一个spring-shiro.xml的配置文件:

  1. <span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"
  4. default-lazy-init="true">
  5. <description>Shiro Configuration</description>
  6. <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
  7. <property name="realm" ref="ShiroRealm" />
  8. </bean>
  9. <!-- 項目自定义的Realm -->
  10. <bean id="ShiroRealm" class="org.spring.springboot.interceptor.shiro.ShiroRealm" ></bean>
  11. <!-- Shiro Filter -->
  12. <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
  13. <property name="securityManager" ref="securityManager" />
  14. <property name="loginUrl" value="/login" />
  15. <property name="successUrl" value="/backstage/index" />
  16. <property name="unauthorizedUrl" value="/login" />
  17. <!-- anon:匿名拦截器,即不需要登录即可访问;一般用于静态资源过滤
  18. authc:如果没有登录会跳到相应的登录页面登录
  19. user:用户拦截器,用户已经身份验证/记住我登录的都可 -->
  20. <property name="filterChainDefinitions">
  21. <value>
  22. </span>     /plugins/** = anon
  23. /images/** = anon
  24. /css/**                     = anon
  25. /**                 = authc<span style="font-size:14px;">
  26. </value>
  27. </property>
  28. </bean>
  29. <!-- 缓存管理器 使用Ehcache实现-->
  30. <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
  31. <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>
  32. </bean>
  33. <!-- AOP式方法级权限检查 -->
  34. <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
  35. <property name="proxyTargetClass" value="true" />
  36. </bean>
  37. <!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
  38. <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
  39. </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">
&lt;description&gt;Shiro Configuration&lt;/description&gt;

	 &lt;bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"&gt;
&lt;property name="realm" ref="ShiroRealm" /&gt;
&lt;/bean&gt; &lt;!-- 項目自定义的Realm --&gt;
&lt;bean id="ShiroRealm" class="org.spring.springboot.interceptor.shiro.ShiroRealm" &gt;&lt;/bean&gt; &lt;!-- Shiro Filter --&gt;
&lt;bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"&gt;
&lt;property name="securityManager" ref="securityManager" /&gt;
&lt;property name="loginUrl" value="/login" /&gt;
&lt;property name="successUrl" value="/backstage/index" /&gt;
&lt;property name="unauthorizedUrl" value="/login" /&gt;
&lt;!-- anon:匿名拦截器,即不需要登录即可访问;一般用于静态资源过滤
authc:如果没有登录会跳到相应的登录页面登录
user:用户拦截器,用户已经身份验证/记住我登录的都可 --&gt;
&lt;property name="filterChainDefinitions"&gt;
&lt;value&gt;
</span> /plugins/** = anon
/images/** = anon
/css/** = anon
/** = authc<span style="font-size:14px;">
&lt;/value&gt;
&lt;/property&gt;
&lt;/bean&gt;
&lt;!-- 缓存管理器 使用Ehcache实现--&gt;
&lt;bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"&gt;
&lt;property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/&gt;
&lt;/bean&gt; &lt;!-- AOP式方法级权限检查 --&gt;
&lt;bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"&gt;
&lt;property name="proxyTargetClass" value="true" /&gt;
&lt;/bean&gt; &lt;!-- 保证实现了Shiro内部lifecycle函数的bean执行 --&gt;
&lt;bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" /&gt;

</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拦截设置(转)的更多相关文章

  1. springboot+shiro整合教程

    进阶教程: 1. springboot+shiro+redis(单机redis版)整合教程 2. springboot+shiro+redis(集群redis版)整合教程 3.springboot+s ...

  2. springboot+shiro+redis(单机redis版)整合教程-续(添加动态角色权限控制)

    相关教程: 1. springboot+shiro整合教程 2. springboot+shiro+redis(单机redis版)整合教程 3. springboot+shiro+redis(集群re ...

  3. springboot+shiro+redis(集群redis版)整合教程

    相关教程: 1. springboot+shiro整合教程 2. springboot+shiro+redis(单机redis版)整合教程 3.springboot+shiro+redis(单机red ...

  4. springboot+shiro+redis(单机redis版)整合教程

    相关教程: 1. springboot+shiro整合教程 2. springboot+shiro+redis(集群redis版)整合教程 3.springboot+shiro+redis(单机red ...

  5. springboot+shiro

    作者:纯洁的微笑 出处:http://www.ityouknow.com/ 这篇文章我们来学习如何使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公 ...

  6. Spring Cloud之路:(七)SpringBoot+Shiro实现登录认证和权限管理

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/sage_wang/article/details/79592269一.Shiro介绍1.Shiro是 ...

  7. springboot+shiro+redis项目整合

    介绍: Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码学和会话管理.使用Shiro的易于理解的API,您可以快速.轻松地获得任何应用程序,从最小的移动应用程序到最 ...

  8. SpringBoot+Shiro+mybatis整合实战

    SpringBoot+Shiro+mybatis整合 1. 使用Springboot版本2.0.4 与shiro的版本 引入springboot和shiro依赖 <?xml version=&q ...

  9. Shiro入门这篇就够了【Shiro的基础知识、回顾URL拦截】

    前言 本文主要讲解的知识点有以下: 权限管理的基础知识 模型 粗粒度和细粒度的概念 回顾URL拦截的实现 Shiro的介绍与简单入门 一.Shiro基础知识 在学习Shiro这个框架之前,首先我们要先 ...

随机推荐

  1. 洛谷P3383 【模板】线性筛素数(Miller_Rabin)

    题目描述 如题,给定一个范围N,你需要处理M个某数字是否为质数的询问(每个数字均在范围1-N内) 输入输出格式 输入格式: 第一行包含两个正整数N.M,分别表示查询的范围和查询的个数. 接下来M行每行 ...

  2. vue 中表单 [v-bind:true-value="a" v-bind:false-value="b"] 的用法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. OpenCV —— 图像局部与部分分割(一)

    背景减除 一旦背景模型建立,将背景模型和当前的图像进行比较,然后减去这些已知的背景信息,则剩下的目标物大致就是所求的前景目标了 缺点 —— 该方法基于一个不长成立的假设:所有像素点是独立的 场景建模 ...

  4. PythonOOP面向对象编程1

    什么是对象? 对象是指现实中的物体或实体(拥有一系列变量.函数(方法)的) 什么事面向对象? 把一切看成对象(实例),让对象和对象之间建立关联关系 对象都有什么特征? 属性(名词)实例变量 姓名.年龄 ...

  5. 推断一个java文件和邮箱格式是否合法

    import java.util.Scanner; public class StringTest { public static void main(String[] args) { int bac ...

  6. js23---工厂模式1

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  7. android图片特效处理之光晕效果

    这篇将讲到图片特效处理的图片光晕效果.跟前面一样是对像素点进行处理,本篇实现的思路可参见android图像处理系列之九--图片特效处理之二-模糊效果和android图像处理系列之十三--图片特效处理之 ...

  8. Zabbix监控,Mysql,Nginx,PHP-FTPM

    一 Zabbix监控Mysql 监控Mysql,Zabbix提供了一个监控模板,所有可以直接使用.或者使用Percona提供的监控模板. 1. 使用自带监控模板 1.1.1 编写监控模板 #!/bin ...

  9. Zabbix监控告警

    一 钉钉告警 1.1.1 添加钉钉机器人 发起群聊 创建完群聊选择,机器人管理 选择你要绑定的群聊 复制下面地址留用 1.1.2 编写钉钉告警脚本 安装requests库,HTTP客户端, # yum ...

  10. Appium定义接口测试

    1.Appium如何执行 Appium作为一个服务器,python的脚本代码交给Appium服务器,Appium再去与设备交互. desired_cap = {} desired_cap['platf ...