成長記錄貼之springboot+shiro(一)
阿新 • • 發佈:2018-11-10
剛開始學習springboot+shiro的安全登入
專案整合參考的http://412887952-qq-com.iteye.com/blog/2299777,是我目前所見寫的最詳細的一位大佬
把各種問題和經驗記錄下來,以備後面學習使用。
這些只是小弟初學shiro的一些粗淺的理解,有錯誤和不完整的地方希望大佬指正
因為專案用到了thymeleaf模板管理前臺頁面,但是在引用layui的時候一直引用不到,最終解決方法如下
在application.properties中增加標紅的配置
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.encoding=utf-8
spring.thymeleaf.content-type=text/html
spring.mvc.static-path-pattern=/static/**
重新梳理一下shiro各種配置
1.登入/註冊時密碼驗證:
1.1 解密演算法配置:在ShiroConfig中配置,這裡的雜湊演算法和雜湊次數要和註冊時對應
@Bean public HashedCredentialsMatcher hashedCredentialsMatcher(){ HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(); hashedCredentialsMatcher.setHashAlgorithmName("md5");//雜湊演算法:這裡使用MD5演算法; hashedCredentialsMatcher.setHashIterations(1024);//雜湊的次數,比如雜湊兩次,相當於 md5(md5("")); return hashedCredentialsMatcher; }
1.2在自己定義的ShiroRealm類,doGetAuthenticationInfo方法返回SimpleAuthenticationInfo物件,
shiro會幫我們用SimpleAuthenticationInfo物件進行登入驗證
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo( userInfo, //使用者 userInfo.getPassword(), //密碼 //鹽值 //如果註冊時加鹽加密了,這裡需要將鹽值傳入 //shiro根據鹽值進行密碼解密,然後與頁面輸入的密碼匹配 //getCredentialsSalt獲取username+salt,我註冊時將username+salt作為真正的鹽值 //如果註冊時只用salt的話,這裡就直接獲取userinfo的salt就行 ByteSource.Util.bytes(userInfo.getCredentialsSalt()), getName() //realm name );
1.3.shiro註冊,這裡的密碼加密配置和1.1中的配置對應即可
@RequestMapping("/register")
public String register(UserInfo user) {
String username=user.getUsername();
String password1=user.getPassword();
//這裡是獲取加密時的鹽值,我這裡將使用者名稱作為鹽值,最好使用隨機數
ByteSource salt = ByteSource.Util.bytes(username);
//使用username+salt作為真正的鹽值,也可以只用salt;1024是加密次數,暫時不知道具體細節,與解密配置對應就行
String password = new SimpleHash("MD5", password1,username+salt,1024).toString();
//這裡只將salt放入資料庫中,解密時獲取username+salt
user.setSalt(salt.toString());
user.setPassword(password);
byte by=1;
user.setState(by);
dao.save(user);
return "login";
}
2.許可權管理
2.1在ShiroConfig中注入許可權管理的Bean
@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){
AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
return authorizationAttributeSourceAdvisor;
}
2.2然後在shiroRealm中獲取使用者許可權交給shiro進行控制
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
UserInfo userInfo = (UserInfo)principals.getPrimaryPrincipal();
for(SysRole role:userInfo.getRoleList()){
authorizationInfo.addRole(role.getRole());
for(SysPermission p:role.getPermissions()){
authorizationInfo.addStringPermission(p.getPermission());
}
}
return authorizationInfo;
}
具體流程大致是,根據使用者與角色對照表獲取角色-->根據角色與資源對照表獲取資源-->交給shiro判斷使用者是否有許可權訪問該資源
表結構如下
使用者表
使用者與角色對照表
角色表
角色與資源對照表
資源表
接上大佬寫的教程,基本就可以正常進行安全登入和許可權的控制了
這些只是小弟初學shiro的一些粗淺的理解,有錯誤和不完整的地方希望大佬指正