package com.dcit.authority.shiro.realm;

import java.util.List;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.realm.Realm;

import static java.util.stream.Collectors.toList;

/**
* <p>Title: CustomizedModularRealmAuthenticator</p>
* <p>Description: 自定义Authenticator
* 当只有一个Realm时，就使用这个Realm，当配置了多个Realm时，会使用所有配置的Realm。
* </p>
* <p>Company: SJZ DC-IT co. LTD</p> 
* @author ZZZ
* @date 2018年9月6日 下午5:24:44
*/
public class CustomizedModularRealmAuthenticator extends ModularRealmAuthenticator {

	@Override
	protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken)
			throws AuthenticationException {
		
		//判断getRealms()是否返回为空
		assertRealmsConfigured();
		
		List<Realm> realms = this.getRealms().stream()
                .filter(realm -> {
                    return realm.supports(authenticationToken);
                }).collect(toList());
		
		//判断是单Realm还是多Realm
        return realms.size() == 1 ? this.doSingleRealmAuthentication(realms.iterator().next(), authenticationToken) : this.doMultiRealmAuthentication(realms, authenticationToken);

	}
	
	

}
