shiro免密登录

1.新建类继承HashedCredentialsMatcher

2.重写doCredentialsMatch,具体实现如下:

  

package com.xiujing.sso.server.credentials;


import com.xiujing.sso.server.realm.ShiroDbRealm;
import com.xiujing.sso.server.token.CustomToken;
import com.xiujing.sso.server.type.LoginType;
import net.sf.ehcache.CacheManager;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;

import java.util.concurrent.atomic.AtomicInteger;

public class CustomCredentialsMatch extends HashedCredentialsMatcher {


    @Override
    public boolean doCredentialsMatch(AuthenticationToken authcToken, AuthenticationInfo info) {
        CustomToken tk = (CustomToken) authcToken;
        if(tk.getType().equals(LoginType.NOPASSWD)){
            return true;
        }
        boolean matches = super.doCredentialsMatch(authcToken, info);
        return matches;
    }
}

  3.在自定义reaml doGetAuthenticationInfo方法调用super.setCredentialsMatcher,具体实现:

CustomCredentialsMatch hashedCredentialsMatcher = new CustomCredentialsMatch();
        // 采用MD5方式加密
        hashedCredentialsMatcher.setHashAlgorithmName("MD5");
        // 设置加密次数
        hashedCredentialsMatcher.setHashIterations(1);
        setCredentialsMatcher(hashedCredentialsMatcher);
        log.warn("密码方式:{}",getCredentialsMatcher().getClass());

  

 

posted @ 2021-01-28 16:40  x0f  阅读(620)  评论(0)    收藏  举报