shiro 修改验证

首先找到认证 ShiroDbRealm

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

	/**
	 * 认证回调函数, 登录时调用
	 */
	@Override
	protected 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值