簡單記錄一下shiro的驗證流程
阿新 • • 發佈:2019-02-06
關於shiro我就不說了,公司的用到shiro這兩天看了下,又重新認知了一遍使用者角色許可權之間的關係,在看shiro和web整合的時候,關於驗證流程這一塊記錄一下。
這裡直接寫驗證流程了:關於配置就不說了,這是與資料庫互動的shiro要依賴service層,嗯 先看是怎麼驗證的
瞭解shiro之後呢,我們都是自己去實現一個realm重寫認證和許可權函式(這裡只說登入)。程式碼如下:
public class ShiroDbRealm extends AuthorizingRealm {
protected AccountService accountService;
/**
* 認證回撥函式,登入時呼叫.
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
User user = accountService.findUserByLoginName(token.getUsername());
if (user != null ) {
byte[] salt = Encodes.decodeHex(user.getSalt());
return new SimpleAuthenticationInfo(new ShiroUser(user.getId(), user.getLoginName(), user.getName()),
user.getPassword(), ByteSource.Util.bytes(salt), getName());
} else {
return null;
}
}
/**
* 授權查詢回撥函式, 進行鑑權但快取中無使用者的授權資訊時呼叫.
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal();
User user = accountService.findUserByLoginName(shiroUser.loginName);
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
info.addRoles(user.getRoleList());
return info;
}
/**
* 設定Password校驗的Hash演算法與迭代次數.
*/
@PostConstruct
public void initCredentialsMatcher() {
HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(AccountService.HASH_ALGORITHM);
matcher.setHashIterations(AccountService.HASH_INTERATIONS);
setCredentialsMatcher(matcher);//這裡把匹配器傳過去
}
public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}
/**
* 自定義Authentication物件,使得Subject除了攜帶使用者的登入名外還可以攜帶更多資訊.
*/
public static class ShiroUser implements Serializable {
private static final long serialVersionUID = -1373760761780840081L;
public Long id;
public String loginName;
public String name;
public ShiroUser(Long id, String loginName, String name) {
this.id = id;
this.loginName = loginName;
this.name = name;
}
public String getName() {
return name;
}
/**
* 本函式輸出將作為預設的<shiro:principal/>輸出.
*/
@Override
public String toString() {
return loginName;
}
/**
* 過載hashCode,只計算loginName;
*/
@Override
public int hashCode() {
return Objects.hashCode(loginName);
}
/**
* 過載equals,只計算loginName;
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
ShiroUser other = (ShiroUser) obj;
if (loginName == null) {
if (other.loginName != null) {
return false;
}
} else if (!loginName.equals(other.loginName)) {
return false;
}
return true;
}
}
}
但看這裡可能會很迷茫,密碼驗證呢?
setCredentialsMatcher(matcher);//這裡把匹配器傳過去
在這裡看到傳了一個matcher於是我們繼續開啟這個方法的原始碼如下:
public abstract class AuthenticatingRealm extends CachingRealm implements Initializable {
................
public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
this.credentialsMatcher = credentialsMatcher;
}
...........
protected void assertCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) throws AuthenticationException {
CredentialsMatcher cm = getCredentialsMatcher();
if (cm != null) {
if (!cm.doCredentialsMatch(token, info)) {
//not successful - throw an exception to indicate this:
String msg = "Submitted credentials for token [" + token + "] did not match the expected credentials.";
throw new IncorrectCredentialsException(msg);
}
} else {
throw new AuthenticationException("A CredentialsMatcher must be configured in order to verify " +
"credentials during authentication. If you do not wish for credentials to be examined, you " +
"can configure an " + AllowAllCredentialsMatcher.class.getName() + " instance.");
}
是這個抽象類AuthenticatingRealm的方法,再往下看getCredentialsMatcher();就看到了Info和token那麼就是資料的比較了
cm.doCredentialsMatch(token, info)
再往下看無非就是一些解密比較返回了
內部流程:
1.AuthenticatingRealm繼承CachingRealm執行getAuthenticationInfo
2.呼叫SimpleCredentialsMatcher的doCredentialsMatch()