Apache Shiro学习----配置(与SpringMVC集成)

1.web.xml文件中配置

<!--将shiro的配置文件交给Spring监听器初始化--> 
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring.xml,classpath:spring-shiro-web.xml</param-value>
    </context-param>
 <!-- shiro配置 开始 --><!-- shiro filter的名字是shiroFilter,那么在shiro的配置文件中要有一个名字为shiroFilter的bean-->  
    <filter>
        <filter-name>shiroFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <async-supported>true</async-supported>
        <init-param>
            <param-name>targetFilterLifecycle</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- shiro配置 结束 -->

由于项目通过Spring管理,因此所有的配置原则上都是交给Spring。DelegatingFilterProxy的功能是通知Spring将所有的Filter交给ShiroFilter管理。

2.在classpath路径下配置spring-shiro-web.xml文件

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <!-- 缓存管理器 使用Ehcache实现 -->
    <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
        <property name="cacheManagerConfigFile" value="classpath:ehcache.xml" />
    </bean>

    <!-- 凭证匹配器 -->
    <bean id="credentialsMatcher" class="utils.RetryLimitHashedCredentialsMatcher">
        <constructor-arg ref="cacheManager" />
        <property name="hashAlgorithmName" value="md5" />
        <property name="hashIterations" value="2" />
        <property name="storedCredentialsHexEncoded" value="true" />
    </bean>

    <!-- Realm实现 -->
    <bean id="userRealm" class="utils.UserRealm">
        <property name="credentialsMatcher" ref="credentialsMatcher" />
    </bean>

    <!-- 安全管理器 -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="userRealm" />
    </bean>

    <!-- Shiro的Web过滤器 -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager" />
        <property name="loginUrl" value="/" />
        <property name="unauthorizedUrl" value="/" />
        <property name="filterChainDefinitions">
            <value>
                /authc/admin = roles[admin]
                /authc/** = authc
                /** = anon
            </value>
        </property>
    </bean>

    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
</beans>

需要注意filterChainDefinitions过滤器中对于路径的配置是有顺序的,当找到匹配的条目之后容器不会再继续寻找。因此带有通配符的路径要放在后面。

3.Shiro内置过滤器说明   (Shiro Filter的执行顺序:自上而下,从左到右” 即可。

通常可分为三组:

1)身份验证相关的   -------authc,anon,authcBasic,logout,user

注意user和authc不同:当应用开启了rememberMe时,用户下次访问时可以是一个user,但绝不会是authc,因为authc是需要重新认证的

2)授权相关的   ---- perms,port,rest,roles,ssl

3)其他  ---noSessionCreation

时间: 2024-08-24 08:45:06

Apache Shiro学习----配置(与SpringMVC集成)的相关文章

Apache Shiro学习笔记(六)FilterChain

鲁春利的工作笔记,好记性不如烂笔头 Apache Shiro学习笔记(七)IniWebEnvironment

Apache Shiro学习笔记(九)Spring集成

鲁春利的工作笔记,好记性不如烂笔头 Integrating Apache Shiro into Spring-based Applications Shiro 的组件都是JavaBean/POJO 式的组件,所以非常容易使用Spring进行组件管理,可以非常方便的从ini配置迁移到Spring进行管理,且支持JavaSE应用及Web 应用的集成. Web Applications 1.web.xml <!-- The filter-name matches name of a 'shiroFil

Apache Shiro学习笔记(五)Web集成使用JdbcRealm

鲁春利的工作笔记,好记性不如烂笔头 http://shiro.apache.org/web-features.html 前面的示例都是把用户名或密码以及权限信息放在ini文件中,但实际的Web项目开发过程中,实际上一般是user<--->role.role<-->permission进行关联关系的配置,每次登录时加载其拥有的权限或者是每次访问时再判断其权限. jdbc-shiro.ini [main] #默认是/login.jsp authc.loginUrl=/login rol

Apache Shiro学习笔记(五)Web集成扩展

鲁春利的工作笔记,好记性不如烂笔头 http://shiro.apache.org/web-features.html 基于Basic的拦截器身份验证 shiro-authc-basic.ini # 基于Basic的拦截器身份验证 [main] # 默认是/login.jsp authc.loginUrl=/login authcBasic.applicationName=请登录 [users] # 用户名=密码,角色 lucl=123456,admin wang=123456 [roles]

Apache Shiro 学习记录5

本来这篇文章是想写从Factory加载ini配置到生成securityManager的过程的....但是貌似涉及的东西有点多...我学的又比较慢...很多类都来不及研究,我又怕等我后面的研究了前面的都忘记了...所以就先与大家分享一下我对Shiro里Ini这个类的研究. 简介 我感觉其实利用ini来配置shiro在实际应用中应该不是很多,我想大部分java应用还是web类型的,数据也应该储存在数据库里..不会简简单单的存在.ini这样的配置文件中的...但是对于学习Shiro来说,我觉得研究一下

Apache Shiro学习笔记

鲁春利的工作笔记,好记性不如烂笔头 官网地址:http://shiro.apache.org/ 主要功能包括: Authentication:身份认证/登录,验证用户是不是拥有相应的身份:Authorization:授权,即权限验证,验证某个已认证的用户是否拥有某个权限:即判断用户是否能做事情:常见的如:验证某个用户是否拥有某个角色. Session Manager:会话管理,即用户登录后就是一次会话,在没有退出之前,它的所有信息都在会话中:会话可以是普通JavaSE环境的,也可以是如Web环境

Apache Shiro SessionManager配置详解.

SessionManager是在应用程序中为所有Subject提供Session的管理,包括创建,删除,失效及验证等.同其的核心组件一样,SessionManager 也是一个由SecurityManager 维护的顶级组件. 在Shiro中默认提供了一个SessionManager的实现DefaultSessionManager.DefaultSessionManager 提供一个应用程序所需的所有企业级会话管理.可以在任何应用程序中使用. 如果想自定义一个SessionManager,可在S

Shiro学习(22)集成验证码

在做用户登录功能时,很多时候都需要验证码支持,验证码的目的是为了防止机器人模拟真实用户登录而恶意访问,如暴力破解用户密码/恶意评论等.目前也有一些验证码比较简单,通过一些OCR工具就可以解析出来:另外还有一些验证码比较复杂(一般通过如扭曲.加线条/噪点等干扰)防止OCR工具识别:但是在中国就是人多,机器干不了的可以交给人来完成,所以在中国就有很多打码平台,人工识别验证码:因此即使比较复杂的如填字.算数等类型的验证码还是能识别的.所以验证码也不是绝对可靠的,目前比较可靠还是手机验证码,但是对于用户

Apache Shiro学习---简介

Apache Shiro是Java的一个安全框架.Shiro可以非常容易的开发出足够好的应用,其不仅可以用在JavaSE环境,也可以用在JavaEE环境. 其基本功能点如下图所示: Authentication:身份认证/登录,验证用户是不是拥有相应的身份: Authorization:授权,即权限验证,验证某个已认证的用户是否拥有某个权限:即判断用户是否能做事情,常见的如:验证某个用户是否拥有某个角色.或者细粒度的验证某个用户对某个资源是否具有某个权限: Session Manager:会话管