2019独角兽企业重金招聘Python工程师标准>>>
最近博客很久没搞了,这几天开始搞起来。。。。
这段时间在学习shiro权限框架,我是以张开涛老师的博客为主,孔浩老师的视频为辅来学习的,无奈孔浩老师的视频出的有点让人捉急,后半段要靠自己了,所以把自己的shiro的学习记录和一些坑记录下来~~~
坑1:
自定义的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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://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>
从拦截到登录的一个完整的认证流程:
1.以当前配置为例,spring实例化shiroFilter后,会对/admin/*的所有url进行拦截(在web.xml中配置)
2.下面的配置文件为url配置了需要经过的拦截器链:
<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>
有anon的在经过anon过滤器后会被拦截,有authc的会被要求认证,上面已经配置过拦截器过的url不会被下面的重复定义覆盖。
3. 如果没有经过验证的url,将会跳转到配置的登录界面,进行登录
登录(即认证)的流程,这里我引用开涛老师的博客里的流程,顺便加上自己的注释:
1、首先调用Subject.login(token)进行登录,其会自动委托给Security Manager,调用之前必须通过SecurityUtils. setSecurityManager()设置(通过IOC把Security Manager注入进去);
2、SecurityManager负责真正的身份验证逻辑;它会委托给Authenticator进行身份验证;
3、Authenticator才是真正的身份验证者,Shiro API中核心的身份认证入口点,此处可以自定义插入自己的实现;
”插入自己的实现”的解释:
SecurityManager接受到token(令牌)信息后会委托内置的Authenticator的实例(通常都是ModularRealmAuthenticator类的实例)调用authenticator.authenticate(token).ModularRealmAuthenticator在认证过程中会对设置的一个或多个Realm实例进行适配,它实际上为Shiro提供了一个可拔插的认证机制。
即可以自定义如何对realm进行适配
4、Authenticator可能会委托给相应的AuthenticationStrategy进行多Realm身份验证,默认ModularRealmAuthenticator会调用AuthenticationStrategy进行多Realm身份验证;
5、Authenticator会把相应的token传入Realm,从Realm获取身份验证信息,如果没有返回/抛出异常表示身份验证失败了。此处可以配置多个Realm,将按照相应的顺序及策略进行访问。