关于什么是Shiro,可以查看这篇文章http://www.cnblogs.com/Laymen/articles/6117751.html
一、添加maven依赖
<dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.2.3</version> </dependency>
如果嫌麻烦可以直接添加shiro-all的依赖
二、web.xml配置Shiro的过滤器
要让shiro拦截web的所有请求那么需要我们在web.xml中配置Shrio和web项目整合提供的filter,配置如下:
<!-- 配置Shiro过滤器,先让Shiro过滤系统接收到的请求 --> <!-- 这里filter-name必须对应applicationContext.xml中定义的<bean >--> <!-- 使用[/*]匹配所有请求,保证所有的可控请求都经过Shiro的过滤 --> <!-- 通常会将此filter-mapping放置到最前面(即其他filter-mapping前面),以保证它是过滤器链中第一个起作用的 --> <filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <init-param> <!-- 该值缺省为false,表示生命周期由SpringApplicationContext管理,设置为true则表示由ServletContainer管理 --> <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>
三、Application-shiro.xml配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <util:properties id="securityCode" location="classpath:config/security-management.properties"/> <bean id="jdbcRealm" class="com.layman.study.core.shiro.realm.LaymanJdbcRealm"> <property name="permissionsLookupEnabled" value="true"/> <property name="name" value="jdbcRealm"/> <property name="credentialsMatcher"> <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher"> <property name="storedCredentialsHexEncoded" value="true"/> <property name="hashAlgorithmName" value="MD5"/> </bean> </property> <property name="authorizationCachingEnabled" value="true"/> <property name="authorizationCacheName" value="shiro_authorization_cache"/> </bean> <bean id="customAuthorizationFilter" class="com.layman.study.core.shiro.filter.CustomAuthorizationFilter"> <property name="ignoreList"> <list> <value>/</value> <value>/login</value> <value>/logout</value> <value>/index</value> <value>/user/register</value> </list> </property> </bean> <!-- Shiro默认会使用Servlet容器的Session,可通过sessionMode属性来指定使用Shiro原生Session --> <!-- 即<property name="sessionMode" value="native"/>,详细说明见官方文档 --> <!-- 这里主要是设置自定义的单Realm应用,若有多个Realm,可使用'realms'属性代替 --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="jdbcRealm"/> <property name="cacheManager"> <bean class="com.layman.study.core.shiro.cache.CustomCacheManager"/> </property> <property name="sessionManager"> <bean class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager"> <property name="deleteInvalidSessions" value="true"/> <property name="sessionDAO"> <bean class="com.layman.study.core.shiro.session.CustomSessionDao"/> </property> </bean> </property> </bean> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <property name="loginUrl" value="/login"/> <property name="successUrl" value="/index"/> <property name="unauthorizedUrl" value="/static/page/404.html"/> <property name="filters"> <util:map> <entry key="customAuthorizationFilter" value-ref="customAuthorizationFilter"/> </util:map> </property> <property name="filterChainDefinitionMap"> <map> <entry key="/index" value="authc"/> <entry key="/**" value="customAuthorizationFilter"/> </map> </property> </bean> <!-- 保证实现了Shiro内部lifecycle函数的bean执行 --> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> </beans>
bean的id为shiroFilter的就是web.xml中代理filter需要的spring bean,这个bean中可以看到:
1.loginUrl:指定了当拦截到请求时如果没有登录那么就会跳转到这个属性指定的地址让用户进行登录操作。
2.successUrl:指定了用户登录成功后跳转的地址
3.unauthorizedUrl:用户的请求被判断为没有权限时会跳转到这个属性指定的页面
4.filters:指定过滤器链,可以是默认提供的过滤器也可以指定自定义的过滤器,在这里我指定的是自定义的过滤器com.layman.study.core.shiro.filter.CustomAuthorizationFilter
5.filterChainDefinitionMap:指定过滤器拦截的urlpatten,<entry key="/index" value="authc"/>这句就是使用了Shiro默认给我们提供的一个过滤器org.apache.shiro.web.filter.authc.FormAuthenticationFilter(用户访问/index这个路劲时是需要登录的)<entry key="/**" value="customAuthorizationFilter"/>定义了我们自定义过滤器拦截所有的请求。
Shrio为我们提供的默认过滤器:
/**
* Shiro-1.2.2内置的FilterChain
* @see =========================================================================================================
* @see 1)Shiro验证URL时,URL匹配成功便不再继续匹配查找(所以要注意配置文件中的URL顺序,尤其在使用通配符时)
* @see 故filterChainDefinitions的配置顺序为自上而下,以最上面的为准
* @see 2)当运行一个Web应用程序时,Shiro将会创建一些有用的默认Filter实例,并自动地在[main]项中将它们置为可用
* @see 自动地可用的默认的Filter实例是被DefaultFilter枚举类定义的,枚举的名称字段就是可供配置的名称
* @see anon---------------org.apache.shiro.web.filter.authc.AnonymousFilter
* @see authc--------------org.apache.shiro.web.filter.authc.FormAuthenticationFilter
* @see authcBasic---------org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter
* @see logout-------------org.apache.shiro.web.filter.authc.LogoutFilter
* @see noSessionCreation--org.apache.shiro.web.filter.session.NoSessionCreationFilter
* @see perms--------------org.apache.shiro.web.filter.authz.PermissionAuthorizationFilter
* @see port---------------org.apache.shiro.web.filter.authz.PortFilter
* @see rest---------------org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter
* @see roles--------------org.apache.shiro.web.filter.authz.RolesAuthorizationFilter
* @see ssl----------------org.apache.shiro.web.filter.authz.SslFilter
*@see user---------------org.apache.shiro.web.filter.authz.UserFilter
* @see =========================================================================================================
* @see 3)通常可将这些过滤器分为两组
* @see anon,authc,authcBasic,user是第一组认证过滤器
* @see perms,port,rest,roles,ssl是第二组授权过滤器
* @see 注意user和authc不同:当应用开启了rememberMe时,用户下次访问时可以是一个user,但绝不会是authc,因为authc是需要重新认证的
* @see user表示用户不一定已通过认证,只要曾被Shiro记住过登录状态的用户就可以正常发起请求,比如rememberMe
* @see 说白了,以前的一个用户登录时开启了rememberMe,然后他关闭浏览器,下次再访问时他就是一个user,而不会authc
* @see ==========================================================================================================
* @see 4)举几个例子
* @see /admin=authc,roles[admin] 表示用户必需已通过认证,并拥有admin角色才可以正常发起'/admin'请求
* @see /edit=authc,perms[admin:edit] 表示用户必需已通过认证,并拥有admin:edit权限才可以正常发起'/edit'请求
* @see /home=user 表示用户不一定需要已经通过认证,只需要曾经被Shiro记住过登录状态就可以正常发起'/home'请求
* @see ==========================================================================================================
* @see 5)各默认过滤器常用如下(注意URL Pattern里用到的是两颗星,这样才能实现任意层次的全匹配)
* @see /admins/**=anon 无参,表示可匿名使用,可以理解为匿名用户或游客
* @see /admins/user/**=authc 无参,表示需认证才能使用
* @see /admins/user/**=authcBasic 无参,表示httpBasic认证
* @see /admins/user/**=user 无参,表示必须存在用户,当登入操作时不做检查
* @see /admins/user/**=ssl 无参,表示安全的URL请求,协议为https
* @see /admins/user/**=perms[user:add:*]
* @see 参数可写多个,多参时必须加上引号,且参数之间用逗号分割,如/admins/user/**=perms["user:add:*,user:modify:*"]
* @see 当有多个参数时必须每个参数都通过才算通过,相当于isPermitedAll()方法
* @see /admins/user/**=port[8081]
* @see 当请求的URL端口不是8081时,跳转到schemal://serverName:8081?queryString
* @see 其中schmal是协议http或https等,serverName是你访问的Host,8081是Port端口,queryString是你访问的URL里的?后面的参数
* @see /admins/user/**=rest[user]
* @see 根据请求的方法,相当于/admins/user/**=perms[user:method],其中method为post,get,delete等
* @see /admins/user/**=roles[admin]
* @see 参数可写多个,多个时必须加上引号,且参数之间用逗号分割,如/admins/user/**=roles["admin,guest"]
* @see 当有多个参数时必须每个参数都通过才算通过,相当于hasAllRoles()方法