最近博客好久没搞了,这几天开始搞起来。。。。web
这段时间在学习shiro权限框架,我是以张开涛老师的博客为主,孔浩老师的视频为辅来学习的,无奈孔浩老师的视频出的有点让人捉急,后半段要靠本身了,因此把本身的shiro的学习记录和一些坑记录下来~~~spring
坑1:apache
自定义的permissionResovler的配置应该是配置在realm里面的(这里的permissionResovler应该不是全局的)安全
完整的认证配置以下:框架
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="urlPermissionResolver" class="com.EP.permission.UrlPermissionResovler"/> <!-- 凭证匹配器 --> <bean id="hashMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher"> <property name="hashAlgorithmName" value="md5"/> </bean> <bean id="adminRealm" class="com.EP.realm.AdminRealm"> <property name="credentialsMatcher" ref="hashMatcher"/> <property name="PermissionResolver" ref="urlPermissionResolver"/> </bean> <!-- 安全管理器 --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="adminRealm"/> </bean> <!-- <!–自定义审核权限的filter –> <bean id="resourceCheckFilter" class="com.EP.shiroFilter.ResourceCheckFilter"> <property name="errorUrl" value="/common.jsp"/> </bean>--> <!-- Shiro的Web过滤器 --> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <property name="loginUrl" value="/admin/toLoginPage"/> <!--<property name="successUrl" value="/home.jsp"/>--> <property name="unauthorizedUrl" value="/common.jsp"/> <!--<property name="filters"> <map> <entry key="resourceCheckFilter" value-ref="resourceCheckFilter" /> </map> </property>--> <property name="filterChainDefinitions"> <value> /admin/static/** = anon /admin/lib/** = anon /admin/temp/** = anon /admin/toLoginPage = anon /admin/login = anon <!--/admin/logout = logout--> <!--/bgimg/** = resourceCheckFilter--> <!--/message/** = resourceCheckFilter--> /admin/** = authc </value> </property> </bean> <!--<!–开启shiro注解–> <bean id="serviceAdvisorAutoProxyCreator" 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生命周期处理器–> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>--> </beans>
从拦截到登陆的一个完整的认证流程:jsp
1.以当前配置为例,spring实例化shiroFilter后,会对/admin/*的全部url进行拦截(在web.xml中配置)学习
2.下面的配置文件为url配置了须要通过的拦截器链:url
<value>
/admin/static/** = anon
/admin/lib/** = anon
/admin/temp/** = anonspa
/admin/toLoginPage = anon
/admin/login = anoncode
<!--/admin/logout = logout-->
<!--/bgimg/** = resourceCheckFilter-->
<!--/message/** = resourceCheckFilter-->
/admin/** = authc
</value>
有anon的在通过anon过滤器后会被拦截,有authc的会被要求认证,上面已经配置过拦截器过的url不会被下面的重复定义覆盖。
3. 若是没有通过验证的url,将会跳转到配置的登陆界面,进行登陆
登陆(即认证)的流程,这里我引用开涛老师的博客里的流程,顺便加上本身的注释:
一、首先调用Subject.login(token)进行登陆,其会自动委托给Security Manager,调用以前必须经过SecurityUtils. setSecurityManager()设置(经过IOC把Security Manager注入进去);
二、SecurityManager负责真正的身份验证逻辑;它会委托给Authenticator进行身份验证;
三、Authenticator才是真正的身份验证者,Shiro API中核心的身份认证入口点,此处能够自定义插入本身的实现;
”插入本身的实现”的解释:
SecurityManager接受到token(令牌)信息后会委托内置的Authenticator的实例(一般都是ModularRealmAuthenticator类的实例)调用authenticator.authenticate(token).ModularRealmAuthenticator在认证过程当中会对设置的一个或多个Realm实例进行适配,它实际上为Shiro提供了一个可拔插的认证机制。
便可以自定义如何对realm进行适配
四、Authenticator可能会委托给相应的AuthenticationStrategy进行多Realm身份验证,默认ModularRealmAuthenticator会调用AuthenticationStrategy进行多Realm身份验证;
五、Authenticator会把相应的token传入Realm,从Realm获取身份验证信息,若是没有返回/抛出异常表示身份验证失败了。此处能够配置多个Realm,将按照相应的顺序及策略进行访问。