SpringBoot Shiro配置自定義密碼加密器代碼實(shí)例
shiro主要有三大功能模塊:
1. Subject:主體,一般指用戶。
2. SecurityManager:安全管理器,管理所有Subject,可以配合內(nèi)部安全組件。(類似于SpringMVC中的DispatcherServlet)
3. Realms:用于進(jìn)行權(quán)限信息的驗(yàn)證,一般需要自己實(shí)現(xiàn)。
細(xì)分功能
1. Authentication:身份認(rèn)證/登錄(賬號(hào)密碼驗(yàn)證)。 2. Authorization:授權(quán),即角色或者權(quán)限驗(yàn)證。 3. Session Manager:會(huì)話管理,用戶登錄后的session相關(guān)管理。 4. Cryptography:加密,密碼加密等。 5. Web Support:Web支持,集成Web環(huán)境。 6. Caching:緩存,用戶信息、角色、權(quán)限等緩存到如redis等緩存中。 7. Concurrency:多線程并發(fā)驗(yàn)證,在一個(gè)線程中開啟另一個(gè)線程,可以把權(quán)限自動(dòng)傳播過去。 8. Testing:測(cè)試支持; 9. Run As:允許一個(gè)用戶假裝為另一個(gè)用戶(如果他們?cè)试S)的身份進(jìn)行訪問。 10. Remember Me:記住我,登錄后,下次再來的話不用登錄了。自定義認(rèn)證加密方式
/** * 自定義認(rèn)證加密方式 */public static class CustomCredentialsMatcher extends SimpleCredentialsMatcher { @Override public boolean doCredentialsMatch(AuthenticationToken authcToken, AuthenticationInfo info) { UsernamePasswordToken token = (UsernamePasswordToken) authcToken; //加密類型,密碼,鹽值,迭代次數(shù) Object tokenCredentials = new SimpleHash('md5', password, username, 2).toHex(); //數(shù)據(jù)庫(kù)存儲(chǔ)密碼 Object accountCredentials = getCredentials(info); //將密碼加密與系統(tǒng)加密后的密碼校驗(yàn),內(nèi)容一致就返回true,不一致就返回false return equals(tokenCredentials, accountCredentials); }}
第一種方式:配置Bean
/** * Shiro自帶密碼管理器 * * @return HashedCredentialsMatcher */@Beanpublic HashedCredentialsMatcher hashedCredentialsMatcher() { //Shiro自帶加密 HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher(); //散列算法使用md5 credentialsMatcher.setHashAlgorithmName('md5'); //散列次數(shù),2表示md5加密兩次 credentialsMatcher.setHashIterations(2); credentialsMatcher.setStoredCredentialsHexEncoded(true); return credentialsMatcher;}/** * 將自己的身份驗(yàn)證器驗(yàn)證方式加入容器 * * @return CustomRealm */@Beanpublic CustomRealm shiroRealm() { CustomRealm shiroRealm = new CustomRealm(); //shiroRealm.setCacheManager(cacheManager()); //加入密碼管理 //shiroRealm.setCredentialsMatcher(hashedCredentialsMatcher());//Shiro自帶密碼管理器 shiroRealm.setCredentialsMatcher(new CustomCredentialsMatcher());//自定義密碼管理器 return shiroRealm;}
第二種方式:實(shí)現(xiàn)AuthorizingRealm類setCredentialsMatcher方法
public class CustomRealm extends AuthorizingRealm { ... ... ... /** * 設(shè)置自定義認(rèn)證加密方式 * * @param credentialsMatcher 默認(rèn)加密方式 */ @Override public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) { //自定義認(rèn)證加密方式 CustomCredentialsMatcher customCredentialsMatcher = new CustomCredentialsMatcher(); // 設(shè)置自定義認(rèn)證加密方式 super.setCredentialsMatcher(customCredentialsMatcher); }}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. IntelliJ IDEA安裝插件的方法步驟2. Java如何基于反射機(jī)制獲取不同的類3. php模擬實(shí)現(xiàn)斗地主發(fā)牌4. asp判斷某個(gè)文件是否存在的函數(shù)5. 在IDEA中實(shí)現(xiàn)同時(shí)運(yùn)行2個(gè)相同的java程序6. 理解PHP5中static和const關(guān)鍵字7. Android table布局開發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器8. .NET Core Web APi類庫(kù)內(nèi)嵌運(yùn)行的方法9. .Net Core使用Coravel實(shí)現(xiàn)任務(wù)調(diào)度的完整步驟10. Vuex localStorage的具體使用

網(wǎng)公網(wǎng)安備