Java tutorial
/** * Copyright (c) 2005-2012 https://github.com/zhangkaitao * * Licensed under the Apache License, Version 2.0 (the "License"); */ package cn.guoyukun.spring.shiro.realm; import java.io.Serializable; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationInfo; import org.apache.shiro.authc.AuthenticationToken; import org.apache.shiro.authc.IncorrectCredentialsException; import org.apache.shiro.authc.LockedAccountException; import org.apache.shiro.authc.SimpleAuthenticationInfo; import org.apache.shiro.authc.UnknownAccountException; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.authz.SimpleAuthorizationInfo; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import cn.guoyukun.spring.shiro.spi.SystemAccount; public abstract class AbstractUserPasswordRealm extends AuthorizingRealm { // private static final Logger LOG = LoggerFactory.getLogger(AbstractUserPasswordRealm.class); /** * ?????? */ @Override public boolean supports(AuthenticationToken token) { return token instanceof UsernamePasswordToken; } /** * ???? */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { AuthorizationInfo info = new SimpleAuthorizationInfo(); return info; } /** * ?? */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { UsernamePasswordToken upt = (UsernamePasswordToken) token; // String identify = upt.getUsername().trim(); try { // ? SystemAccount account = getAccountByLoginIdentify(identify); // ? if (account == null) { throw new UnknownAccountException("[" + identify + "]??"); } if (account.isLocked()) { throw new LockedAccountException("[" + identify + "]????"); } // LOG.debug("[{}]???:[{}]",identify,account.getCredentials()); SimpleAuthenticationInfo sai = new SimpleAuthenticationInfo(account.getIdentify(), account.getCredentials(), this.getName()); if (!getCredentialsMatcher().doCredentialsMatch(token, sai)) { throw new IncorrectCredentialsException("?"); } LOG.debug("[{}]?", identify); return sai; } catch (AuthenticationException ae) { throw ae; } catch (Exception e) { throw new AuthenticationException("?[" + identify + "]?", e); } } /** * ?? * @param identify * @return ???null * @throws Exception */ protected abstract SystemAccount getAccountByLoginIdentify(Serializable identify) throws Exception; }