shiro笔记
***************@Date("2018-5-6")***************
===知識點===
1、spring.xml中的bean id 要與web.xml中定義的過濾器名稱一致
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"/>
和
<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>
PS:默認情況下,Spring會到IOC容器中查找<filter-name>對應的bean,
也可以在web.xml中通過targetBeanName的初始化參數來配置spring.xml中filter bean 的id
2、動態初始化資源和權限時采用第一次匹配優先的方式,所以順序不能隨便擺放
3、未登錄情況下,如果訪問需認證的頁面或需授權的頁面,則會自動跳轉到loginUrl對應的地址;
已登陸情況下,如果訪問需授權的頁面,則會自動跳轉到unauthorizedUrl對應的地址;
4、鹽值加密:即使兩個人的原始密碼一樣,加密后也不一樣;一般是通過用戶名或隨機數生成salt進行加密
5、認證策略(AbstractAuthenticationStrategy類)一共有三種,默認為AtLeastOneSuccessfulStrategy
6、配置realm
<bean id="jdbcRealm" class="com.syjp.shiro.realms.ShiroRealm">
<property name="credentialsMatcher">
<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<property name="hashAlgorithmName" value="MD5"></property><!--加密哈希算法名稱-->
<property name="hashIterations" value="1024"></property><!--加密迭代次數,加密多少次-->
</bean>
</property>
</bean>
<bean id="secondRealm" class="com.syjp.shiro.realms.SecondRealm">
<property name="credentialsMatcher">
<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<property name="hashAlgorithmName" value="Sha1"></property>
<property name="hashIterations" value="1024"></property>
</bean>
</property>
</bean>
底層加密算法:
Object result = new SimpleHash(hashAlgorithmName, credentials, salt,hashIterations);
普通驗證:
AuthenticationInfo info=new SimpleAccount(principals, credentials,realmName);
鹽值加密驗證:
AuthenticationInfo info = new SimpleAccount(principals, credentials, salt,realmName);
計算鹽值方法:
ByteSource salt = ByteSource.Util.bytes(principals);
PS: 形參realmName就是當前realm對應的name,調用父類的getName()方法即可
7、realms可以直接在securityManager中配置,也可以在authenticator中配置
8、配置記住我cookie過期時間
在securityManager中配置以下屬性,單位為秒,默認過期時間為1年,
<property name="rememberMeManager.cookie.maxAge" value="6"></property>
9、如果配置了CacheManager,那么在登陸第一次后,再次登陸shiro就會從還從里面取出用戶信息進行比較,所以不會執行認證方法。
10、登出可以通過自定義的方法,也可以通過shiro的登出過濾器去配置(有兩種辦法)
第一種(推薦):
<bean id="logout" class="org.apache.shiro.web.filter.authc.LogoutFilter">
<property name="redirectUrl" value="/list.jsp" />
</bean>
和在shiroFilter中配置
/shiro/logout = logout (登出的URL地址(隨便寫),登出過濾器的bean id)
參考博客:
http://dwangel.iteye.com/blog/1890524
https://blog.csdn.net/qq_34292044/article/details/79131199
11、動態初始化資源和權限
<!-- 通過實例工廠的方式配置Bean -->
<bean id="filterChainDefinitionMap" factory-bean="filterChainDefinitionMapBuilder" factory-method="buildFilterChainDefinitionMap"></bean>
<bean id="filterChainDefinitionMapBuilder" class="com.syjp.shiro.factory.FilterChainDefinitionMapBuilder"> </bean>
和在shiroFilter中配置
<property name="filterChainDefinitionMap" ref="filterChainDefinitionMap"></property>
和自定義類
public class FilterChainDefinitionMapBuilder {
public LinkedHashMap<String, String> buildFilterChainDefinitionMap() {
// 由于權限有先后順序之分,所以得用LinkedHashMap(先進先出)
LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
map.put("/login.jsp", "anon");
map.put("/login", "anon");
map.put("/index.jsp", "anon");
map.put("/admin.jsp", "roles[admin]");
map.put("/user.jsp", "authc,roles[user]");// authc 必須經過驗證,如果配置了/**則沒有必要寫了
//map.put("/list.jsp", "user");// user 代表可以通過記住我進行訪問
map.put("/list.jsp", "roles[admin]");//供登出成功調用
map.put("/shiro/logoutzzzz", "logout");//鍵值(即登出的對應地址)隨便寫
map.put("/**", "authc");
return map;
}
}
12、通過記住我登陸
在登陸方法中配置,
UsernamePasswordToken token = new UsernamePasswordToken(username,password);
token.setRememberMe(true);
在權限配置中添加
map.put("/list.jsp", "user");// user 代表可以通過記住我進行訪問
13、進行授權操作,需要繼承AuthorizingRealm類,實現其中的授權和認證兩個方法
===問題(已解決)===
1、Shiro的 @RequiresRoles注解不生效
網上搜的時候這倆bean是在一起的,LifecycleBeanPostProcessor 是管理shiro生命周期的,不直接跟權限注解關聯。
所以加上DefaultAdvisorAutoProxyCreator 這個bean就可以了。
/* @Bean
public static LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
return new LifecycleBeanPostProcessor();
}*/
@Bean
public static DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator(){
return new DefaultAdvisorAutoProxyCreator();
}
參考博客:https://blog.csdn.net/qq_35981283/article/details/78631924
===問題(未解決)===
1、所有頁面(login.jsp)中的${request.contextPath}都解析不出來
2、在緩存失效之前進入一個頁面,如果不關閉瀏覽器,那么就可以一直訪問該頁面(即使添加隨機數)
轉載于:https://www.cnblogs.com/syjp/p/11081462.html
總結
- 上一篇: 如何下载js类库
- 下一篇: charles使用说明(基于mac)