簡單地說一下shiro四個(gè)基本核心功能:
Authentication(認(rèn)證)
過程可以分為三步:
1比藻、Collect the user’s identifying information, calledprincipals, and supporting proof of identity, calledcredentials.(收集用戶的標(biāo)識(shí)信息,被稱為主體倘屹,并支持身份證明银亲,稱為憑證。)
2纽匙、Submit the principals and credentials to the system.(向系統(tǒng)提交主體和憑證务蝠。)
3、If the submitted credentials match what the system expects for that user identity (principal), the user is considered authenticated. If they don’t match, the user is not considered authenticated.(如果提交的憑證與系統(tǒng)期望的用戶標(biāo)識(shí)(主體)匹配烛缔,那么用戶就被認(rèn)為是經(jīng)過身份驗(yàn)證的馏段。如果它們不匹配,則用戶不被認(rèn)為是經(jīng)過身份驗(yàn)證的践瓷。)
Subject subject = SecurityUtils.getSubject();
AuthenticationToken authenticationToken = new MyUsernamePasswordToken(userName, password, verifyCode, rememberMe);
public String login() {
try/catch
}
Authentication(認(rèn)證)之后院喜,用戶可以進(jìn)入系統(tǒng),但是当窗,能做哪些操作呢够坐?具體的就需要進(jìn)一步判斷。
Authorization(授權(quán)):
可以分為role(角色)與permission(權(quán)限)驗(yàn)證:
role check:
if ( subject.hasRole(“administrator”) ) {
//show the ‘Create User’ button
} else {
//grey-out the button?
}
permission check:
if ( subject.isPermitted(“user:create”) ) {
//show the ‘Create User’ button
} else {
//grey-out the button?
}
role(角色)驗(yàn)證,在運(yùn)行時(shí)不能添加或者刪除元咙。permission(權(quán)限)驗(yàn)證更加靈活梯影,具有更細(xì)粒度,自己需要什么可以寫什么庶香。
Session Management(會(huì)話管理):
shiro 的session(會(huì)話)是獨(dú)立于容器的甲棍,也就是說,不再需要專門的servlet與EJB容器了赶掖。對于企業(yè)緩存(enterprise caches)感猛、關(guān)系數(shù)據(jù)庫(relational databases)、NoSQL等可以一次性配置會(huì)話集群奢赂,不需要根據(jù)部署應(yīng)用程序的方式重新配置應(yīng)用程序陪白。
Session session = subject.getSession();
Cryptography(加密):
簡化利用JDK的密碼支持使得數(shù)據(jù)混淆加密。使用的是hash(哈希)與ciphers(密碼)膳灶。
之前使用MD5的加密方式如下:
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.digest(bytes);
byte[] hashed = md.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
使用shiro之后:
String hex = new Md5Hash(myFile).toHex();
怎么樣咱士?簡單了太多了吧,一句話就搞定了轧钓。簡單粗暴序厉,代碼也更優(yōu)雅。
使用256-bit AES加密毕箍,shiro下代碼如下:
AesCipherService cipherService = new AesCipherService();
cipherService.setKeySize(256);
推薦微信公眾號(hào)【排骨肉段】弛房,記錄日常的美好。