shiro 修改验证

来源:互联网 发布:java实施工程师做什么 编辑:程序博客网 时间:2024/06/16 12:11

首先找到认证 ShiroDbRealm

这里是验证验证码、密码等信息

/** * 认证回调函数, 登录时调用 */@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {UsernamePasswordToken token = (UsernamePasswordToken) authcToken;SimpleAuthenticationInfo authenticationInfo = null;User user = getSystemService().getUserByLoginName(token.getUsername());if (user != null) {byte[] salt = Encodes.decodeHex(user.getPassword());return new SimpleAuthenticationInfo(new Principal(user), user.getPassword(), ByteSource.Util.bytes(salt), getName());} else {return null;}}


Class SimpleAuthenticationInfo

api 是这样介绍的,这说本文用的方法:


SimpleAuthenticationInfo(Object principal,Object hashedCredentials,ByteSource credentialsSalt, String realmName)

Constructor that takes in a single 'primary' principal of the account, its corresponding hashed credentials, the salt used to hash the credentials, and the name of the realm to associate with the principals.

意思是:构造函数,在一个“主”的账户,其相应的散列凭证,盐用于哈希凭据和域的名称关联的主体。说白了就是对你的身份认证.合法性认证,那shiro怎么知道我是怎么验证的呢

认证的方法通过initCredentialsMatcher设置;
@PostConstruct      public void initCredentialsMatcher() {  //该句作用是重写shiro的密码验证,让shiro用我自己的验证          setCredentialsMatcher(new CustomCM());        }  




这样我们就可以用自己的方式验证用户的合法性了
那接下来就需要写自己的验证方法

import org.apache.shiro.authc.AuthenticationInfo;import org.apache.shiro.authc.AuthenticationToken;import org.apache.shiro.authc.UsernamePasswordToken;import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;import org.apache.shiro.crypto.hash.Sha384Hash;import com.sicc.oa.common.pass.SHA1;/** * 自定义 密码验证类 * * @author q * */public class CustomCredentialsMatcher extends SimpleCredentialsMatcher {    @Override    public boolean doCredentialsMatch(AuthenticationToken authcToken,            AuthenticationInfo info) {        UsernamePasswordToken token = (UsernamePasswordToken) authcToken;        Object tokenCredentials = encrypt(String.valueOf(token.getPassword()));        Object accountCredentials = getCredentials(info);        return equals(tokenCredentials, accountCredentials);    }    // 将传进来密码加密方法    private String encrypt(String data) {        String sha384Hex = new SHA1().getDigestOfString(String.valueOf(data)                .getBytes());        ;// 这里可以选择自己的密码验证方式 比如 md5或者sha256等        return sha384Hex;    }}


这样就将验证改成自己的方法了。
收工


0 0
原创粉丝点击