本文是针对web应用
web.xml:

<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter> <filter-mapping>
<!-- Make sure any request you want accessible to Shiro is filtered. /* catches all -->
<!-- requests. Usually this filter mapping is defined first (before all others) to -->
<!-- ensure that Shiro works in subsequent filters in the filter chain: -->
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>

参数TargetFilterLifecycle:缺省值为false,即生命周期由Spring app context管理。设置为true时由servlet container管理。

配置applicationContext.xml:

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/> <!-- 配置要跳转的URL -->
<property name="loginUrl" value="/login.jsp"/>
<property name="successUrl" value="/main.jsp"/>
<property name="unauthorizedUrl" value="/err404.jsp"/> <!-- 配置过滤策略 切记这是FIRST MATCH WINS -->
<property name="filterChainDefinitions">
<value>
/download/** = user
/images/** = anon
/admin/** = authc, roles[admin]
/docs/** = authc, perms[document:read]
/** = authc
/logout.html = logout
</value>
</property>
</bean> <bean id="myRealm" class="king.common.security.MyRealm"></bean> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="myRealm" />
</bean> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

另外,DefaultSecurityManager继承RealmSecurityManager。因此,当需要多个realm时可以使用"realms"property。
ShiroFilterFactoryBean提供了Filters属性,关于Filters:
This property is optional: this {@code FactoryBean} implementation will discover all beans in the web application context that implement the {@link Filter} interface and automatically add them to this filter map under their bean name.

如果需要的话可以配置一下,如:

<property name="filters">
<util:map>
<entry key="myAlias1" value-ref="myFilter1"/>
</util:map>
</property>

filterChainDefinitions这一property的set方法是这样定义的:

public void setFilterChainDefinitions(String definitions) {
Ini ini = new Ini();
ini.load(definitions);
//did they explicitly state a 'urls' section? Not necessary, but just in case:
Ini.Section section = ini.getSection(IniFilterChainResolverFactory.URLS);
if (CollectionUtils.isEmpty(section)) {
//no urls section. Since this _is_ a urls chain definition property, just assume the
//default section contains only the definitions:
section = ini.getSection(Ini.DEFAULT_SECTION_NAME);
}
setFilterChainDefinitionMap(section);
}

于是我们便可以使用filterChainDefinitionMap这一property。我们可以写一个继承FactoryBean<Section>的类动态构成一个filterChainDefinitionMap。(Ps:Section是实现Map<String,String>的Ini的静态内部类。)

另外,如果希望使用注解:

<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>

<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>

Shiro - 与Spring集成的更多相关文章

  1. shiro与spring集成

    简介 Apache Shiro 是 Java 的一个安全(权限)框架.主要提供了认证.授权.加密和会话管理等功能. Authentication:身份认证/登录,验证用户是不是拥有相应的身份:Auth ...

  2. [原]CAS和Shiro在spring中集成

    shiro是权限管理框架,现在已经会利用它如何控制权限.为了能够为多个系统提供统一认证入口,又研究了单点登录框架cas.因为二者都会涉及到对session的管理,所以需要进行集成. Shiro在1.2 ...

  3. Shiro之身份认证、与spring集成(入门级)

    目录 1. Shro的概念 2. Shiro的简单身份认证实现 3. Shiro与spring对身份认证的实现 前言: Shiro 可以非常容易的开发出足够好的应用,其不仅可以用在 JavaSE 环境 ...

  4. Spring集成shiro做登陆认证

    一.背景 其实很早的时候,就在项目中有使用到shiro做登陆认证,直到今天才又想起来这茬,自己抽空搭了一个spring+springmvc+mybatis和shiro进行集成的种子项目,当然里面还有很 ...

  5. spring集成shiro报错解决(no bean named 'shiroFilter' is defined)

    引言: 本人在使用spring集成shiro是总是报“no bean named 'shiroFilter' is defined”,网上的所有方式挨个试了一遍,又检查了一遍, 还是没有解决,最后,抱 ...

  6. shiro实战系列(十五)之Spring集成Shiro

    Shiro 的 JavaBean 兼容性使得它非常适合通过 Spring XML 或其他基于 Spring 的配置机制.Shiro 应用程序需要一个具 有单例 SecurityManager 实例的应 ...

  7. Java 权限框架 Shiro 实战二:与spring集成、filter机制

    转自:https://www.cnblogs.com/digdeep/archive/2015/07/04/4620471.html Shiro和Spring的集成,涉及到很多相关的配置,涉及到shi ...

  8. 2017.2.13 开涛shiro教程-第十二章-与Spring集成(二)shiro权限注解

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第十二章-与Spring集成(二)shiro权限注解 shiro注 ...

  9. 2017.2.13 开涛shiro教程-第十二章-与Spring集成(一)配置文件详解

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第十二章-与Spring集成(一)配置文件详解 1.pom.xml ...

随机推荐

  1. django中models联合唯一unique_together

    例: 文章点赞 class ArticleUpDown(models.Model): """ 点赞表 """ nid = models.Au ...

  2. 洛谷P4931 情侣?给我烧了!(加强版)(组合数学)

    题面 传送门 题解 首先我们算出刚好有\(k\)对情侣的方案数 从\(n\)对情侣中选出\(k\)对,方案数为\({n\choose k}\) 从\(n\)排座位中选出\(k\)排,方案数为\({n\ ...

  3. linux性能评估与分析工具---CPU篇

    一. uptime root@calm:~# uptime :: up days, :, user, load average: 0.04, 0.09, 0.04 这里主要关注load average ...

  4. Postman使用手册1——导入导出和发送请求查看响应

    导读: 现在的web和移动开发,常常会调用服务器提供restful接口进行数据请求,为了调试,一般会先用工具进行测试,通过测试后才开始在开发中使用.这里介绍一下如何在chrome浏览器利用postma ...

  5. react的一些思考

    在做好第一个需求之后,我接到了一个react写的产品,这让我异常的兴奋,终于能写react了 开始做的时候整体框架已经搭建好了,这让我有点小失落,我还以为我要开始搭框架了呢,没事,搭的也挺好的. 有了 ...

  6. macdown快速上手

    1.断句 在结尾处输入两个空格并使用回车. 2.标题分级 使用#来进行分级,#越多级数越低 3.链接 可以使用<>里面直接加上地址 或者使用[}里面加上链接名字然后后面接上()里面就是地址 ...

  7. 1. java 的访问修饰符

    一.什么情况下使用修饰符 属性通常使用private封装起来 方法一般使用public用于被调用 会被子类继承的方法,通常使用protected private protected package p ...

  8. image_size must contain 3 elements[4]

    今天在制作自己训练集合并且训练的时候,碰见了如下的错: image_size must contain 3 elements[4] 这是因为训练的数据集中不是所有的图片位深都是三通道的. 写一个脚本查 ...

  9. selenium(python)登录时账号密码错误提示语

    selenium(python)登录时账号密码错误提示语的获取 可以用text

  10. Linux and Shell 实用命令

    -name '*.jar' -printf '%p:' ### 查看CPU使用率 mpstat -P ALL