Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ public void setCredentialsRetryCacheName(String credentialsRetryCacheName) {
this.credentialsRetryCacheName = credentialsRetryCacheName;
}

public void setCredentialsRetryTimes(Cache<Object, AtomicInteger> credentialsRetryTimes) {
public synchronized void setCredentialsRetryTimes(Cache<Object, AtomicInteger> credentialsRetryTimes) {
this.credentialsRetryTimes = credentialsRetryTimes;
}

private Cache<Object, AtomicInteger> getCredentialsRetryCacheLazy() {
private synchronized Cache<Object, AtomicInteger> getCredentialsRetryCacheLazy() {
if (this.credentialsRetryTimes == null) {
this.credentialsRetryTimes = createCredentialsRetryCache();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo
if (matches) {
cacheManager.getCache(getCredentialsRetryCacheName()).remove(token.getPrincipal());
}
return credentialsMatch(token, info);
return matches;
}

//匹配用户输入的token的凭证(未加密)与系统提供的凭证(已加密)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.pam.AbstractAuthenticationStrategy;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.util.CollectionUtils;
Expand All @@ -34,7 +33,7 @@ public class AtLeastTwoAuthenticatorStrategy extends AbstractAuthenticationStrat
@Override
public AuthenticationInfo beforeAllAttempts(Collection<? extends Realm> realms, AuthenticationToken token) throws AuthenticationException {
//返回一个权限的认证信息
return new SimpleAuthenticationInfo();
return new RealmAuthenticationInfo();
}

@Override
Expand All @@ -60,7 +59,9 @@ public AuthenticationInfo afterAttempt(Realm realm, AuthenticationToken token, A

@Override
public AuthenticationInfo afterAllAttempts(AuthenticationToken token, AuthenticationInfo aggregate) throws AuthenticationException {
if (aggregate == null || CollectionUtils.isEmpty(aggregate.getPrincipals()) || aggregate.getPrincipals().getRealmNames().size() < 2) {
if (aggregate == null || CollectionUtils.isEmpty(aggregate.getPrincipals()) || aggregate.getPrincipals().getRealmNames().size() < 2
|| (aggregate instanceof RealmAuthenticationInfo
&& ((RealmAuthenticationInfo) aggregate).getSuccessfulRealms() < 2)) {
throw new AuthenticationException("Authentication token of type [" + token.getClass() + "] " +
"could not be authenticated by any configured realms. Please ensure that at least two realm can " +
"authenticate these tokens.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,67 +15,27 @@
*/
package org.apache.shiro.biz.authc.pam;

import org.apache.commons.collections.CollectionUtils;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class DefaultModularRealmAuthenticator extends ModularRealmAuthenticator {

private static final Logger logger = LoggerFactory.getLogger(DefaultModularRealmAuthenticator.class);

@Override
protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken)
throws AuthenticationException {

this.assertRealmsConfigured();

Collection<Realm> realms = this.getRealms();

if (CollectionUtils.isEmpty(realms)) {
throw new IllegalStateException("Configuration error: No realms support token type:" + authenticationToken.getClass());
}

if (realms.size() == 1) {
return this.doSingleRealmAuthentication(realms.iterator().next(), authenticationToken);
} else {
//获得匹配的Realm
List<Realm> supportRealms = this.filterSupportRealms(realms, authenticationToken);
if(CollectionUtils.isEmpty(supportRealms)) {
throw new IllegalStateException("Configuration error: No realms support token type:" + authenticationToken.getClass());
}else if(supportRealms.size() == 1) {
//只有一个匹配
return this.doSingleRealmAuthentication(supportRealms.iterator().next(), authenticationToken);
}else {
//具有多个匹配,此时提醒开发者有可能会导致验证时的用户自定义异常丢失
if(logger.isWarnEnabled()) {
logger.warn("token类型为"+authenticationToken.getClass().getName()+"有超多一个对应的Realm处理,有可能会导致认证时用户自定义认证异常丢失,请检查核对配置文件!!!");
}
return this.doMultiRealmAuthentication(realms, authenticationToken);
}
}
}

private List<Realm> filterSupportRealms(Collection<Realm> realms, AuthenticationToken authenticationToken) {
List<Realm> supportRealms = new ArrayList<Realm>(realms);
Iterator<Realm> it = supportRealms.iterator();
while(it.hasNext()) {
Realm r = it.next();
if(!r.supports(authenticationToken)) {
it.remove();
}
}
return supportRealms;
}


@Override
protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken)
throws AuthenticationException {
assertRealmsConfigured();
// Snapshot the configured set, not the supporting subset: strategies apply
// whenever multiple realms are configured, as in ModularRealmAuthenticator.
Collection<Realm> realms = new ArrayList<>(getRealms());
if (realms.size() == 1) {
return doSingleRealmAuthentication(realms.iterator().next(), authenticationToken);
}
return doMultiRealmAuthentication(realms, authenticationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.pam.AbstractAuthenticationStrategy;
import org.apache.shiro.biz.utils.StringUtils2;
import org.apache.shiro.realm.Realm;
Expand All @@ -40,7 +39,7 @@ public class OnlyOneAuthenticatorStrategy extends AbstractAuthenticationStrategy
public AuthenticationInfo beforeAllAttempts(Collection<? extends Realm> realms, AuthenticationToken token)
throws AuthenticationException {
// 返回一个权限的认证信息
return new SimpleAuthenticationInfo();
return new RealmAuthenticationInfo();
}

/** 在每个Realm之前调用 */
Expand All @@ -57,6 +56,12 @@ public AuthenticationInfo beforeAttempt(Realm realm, AuthenticationToken token,
@Override
public AuthenticationInfo afterAttempt(Realm realm, AuthenticationToken token, AuthenticationInfo singleRealmInfo,
AuthenticationInfo aggregateInfo, Throwable t) throws AuthenticationException {
// A non-null realm result must identify an account. Reject malformed
// results explicitly rather than leaking a null pointer from aggregation.
if (singleRealmInfo != null && (singleRealmInfo.getPrincipals() == null
|| singleRealmInfo.getPrincipals().isEmpty())) {
throw new AuthenticationException("Realm returned authentication information without principals.");
}
AuthenticationInfo info;
if (singleRealmInfo == null) {
info = aggregateInfo;
Expand All @@ -65,7 +70,9 @@ public AuthenticationInfo afterAttempt(Realm realm, AuthenticationToken token, A
info = singleRealmInfo;
} else {
info = merge(singleRealmInfo, aggregateInfo);
if (info.getPrincipals().getRealmNames().size() > 1) {
if (info.getPrincipals().getRealmNames().size() > 1
|| (info instanceof RealmAuthenticationInfo
&& ((RealmAuthenticationInfo) info).getSuccessfulRealms() > 1)) {
LOG.debug("RealmNames:" + StringUtils2.join(info.getPrincipals().getRealmNames(), ","));
throw new AuthenticationException("Authentication token of type [" + token.getClass() + "] "
+ "could not be authenticated by any configured realms. Please ensure that only one realm can "
Expand All @@ -80,6 +87,14 @@ public AuthenticationInfo afterAttempt(Realm realm, AuthenticationToken token, A
@Override
public AuthenticationInfo afterAllAttempts(AuthenticationToken token, AuthenticationInfo aggregate)
throws AuthenticationException {
return aggregate;
}
if (aggregate == null || aggregate.getPrincipals() == null
|| aggregate.getPrincipals().isEmpty()
|| aggregate.getPrincipals().getRealmNames().size() != 1
|| (aggregate instanceof RealmAuthenticationInfo
&& ((RealmAuthenticationInfo) aggregate).getSuccessfulRealms() != 1)) {
throw new AuthenticationException("Exactly one realm must authenticate the token.");
}
return aggregate;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2018 (https://github.com/loong10k).
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.apache.shiro.biz.authc.pam;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.SaltedAuthenticationInfo;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.subject.SimplePrincipalCollection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;

/** An invocation-local aggregate that never mutates a realm's cached principals. */
final class RealmAuthenticationInfo extends SimpleAuthenticationInfo {
private static final long serialVersionUID = 1L;
private int successfulRealms;

int getSuccessfulRealms() {
return successfulRealms;
}

@Override
public void merge(AuthenticationInfo info) {
if (info == null || info.getPrincipals() == null || info.getPrincipals().isEmpty()) {
return;
}
SimpleAuthenticationInfo snapshot = new SimpleAuthenticationInfo();
snapshot.setPrincipals(new SimplePrincipalCollection(info.getPrincipals()));
Object credentials = info.getCredentials();
if (credentials instanceof Set<?>) {
credentials = new LinkedHashSet<>((Set<?>) credentials);
} else if (credentials instanceof Collection<?>) {
credentials = new ArrayList<>((Collection<?>) credentials);
}
snapshot.setCredentials(credentials);
if (info instanceof SaltedAuthenticationInfo) {
snapshot.setCredentialsSalt(((SaltedAuthenticationInfo) info).getCredentialsSalt());
}
super.merge(snapshot);
successfulRealms++;
}
}
Loading
Loading