shiro是apache开源的安全框架,可方便用来处理用户信息认证、用户受权控制、信息加密等功能。web
<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> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
工做原理:web.xml中配置DelegatingFilterProxy的Servlet对象,其代理的目标Serlvet实际上是org.apache.shiro.spring.web.ShiroFilterFactoryBean,为了创建两个DelegatingFilterProxy和ShiroFilterFactoryBean关系,主要是经过Filter名称进行绑定的,当客户端发送请求时,DelegatingFilterProxy进行拦截,而后根据获取Filter名称,最后调用IOC容器的getBean的方法获取和Filter名称匹配的Bean,因ShiroFilterFactoryBean是FactoryBean因实际上处理客户端请求的是shiro在IOC容器Filter的实现类spring
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <!--Ehcache缓存配置文件为空则使用默认配置--> <property name="cacheManagerConfigFile" value="classpath:ehcache-shiro.xml"/> </bean>
<bean id="shiroSessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager"> <property name="globalSessionTimeout" value="600000"/> <!-- session 有效时间为半小时 (毫秒单位)--> <property name="sessionListeners"> <list> <bean class="com.zhiwei.shiro.listener.sessionLisenter"/> </list> </property> </bean>
<bean id="myRealm" class="com.zhiwei.shiro.realm.MyRealm" init-method="setCredentialMatcher"/>
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="cacheManager" ref="cacheManager"/> <property name="sessionManager" ref="shiroSessionManager"/> <property name="realm" ref="myRealm"/> <!-- AuthenticationListener在认证器里面维护 --> <property name="authenticator.authenticationListeners"> <set> <bean class="com.zhiwei.shiro.listener.DefineAuthenticationListener"/> </set> </property> </bean>
注意过滤器的工做顺序:shiro采起第1次匹配优先,第一次匹配后后面的过滤器链不会匹配,顺序不当可能出现"302 not found"错误apache
经常使用过滤器通俗解释:(过滤路径支持ant风格)缓存
千万注意:名称必须与web.xml配置的Filter名称一致,不然IOC找不到对应的Bean出现异常安全
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <property name="loginUrl" value="/toLoginPage"/> <property name="successUrl" value="/toSuccessfulPage"/> <property name="unauthorizedUrl" value="/toUnauthorizedPage"/> <property name="filterChainDefinitions"> <value> /shiroHandler/shiro-logout = logout /shiroHandler/shiro-login = anon /toUserPage = authc,roles[user] /toAdminPage = authc,roles[admin] /** = authc </value> </property> </bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
补充: 配置文件AuthenticationListener/SessionListener配置session
AuthenticationListener:认证监听器:会负责监听整个shiro认证过程的状况,对于一些项目想统计用户登录的请求,可使用该接口,该接口在AuthenticatingSecurityManager的Authenticator中的authenticationListeners维护,DefaultWebSecurityManager因继承了AuthenticatingSecurityManager:使用级联属性配置authenticator.authenticationListeners便可:mvc
AuthenticationListener接口:app
public interface AuthenticationListener { void onSuccess(AuthenticationToken token, AuthenticationInfo info); void onLogout(PrincipalCollection principals); }
spring配置AuthenticationListener:authenticator.authenticationListeners框架
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="authenticator.authenticationListeners"> <set> <bean class="com.zhiwei.shiro.listener.DefineAuthenticationListener"/> </set> </property> </bean>
SessionListener:会话监听器:顾名思义就是监听整个会话的操做过程在SessionManager中的sessionListeners维护:加密
SessionListener 接口:
public interface SessionListener { void onStart(Session session); void onStop(Session session); void onExpiration(Session session); }
spring配置SessionListener :sessionListeners
<bean id="shiroSessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager"> <property name="sessionListeners"> <list> <bean class="com.zhiwei.shiro.listener.sessionLisenter"/> </list> </property> </bean>