/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.portal.security.access.control;
import com.liferay.portal.kernel.security.access.control.AccessControlUtil;
import com.liferay.portal.kernel.security.access.control.AccessControlled;
import com.liferay.portal.kernel.security.access.control.BaseAccessControlPolicy;
import com.liferay.portal.kernel.security.auth.AccessControlContext;
import com.liferay.portal.kernel.util.MapUtil;
import com.liferay.portal.kernel.util.SetUtil;
import com.liferay.portal.kernel.util.StringUtil;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
/**
* @author Tomas Polesovsky
* @author Igor Spasic
* @author Michael C. Han
* @author Raymond Augé
*/
public class AllowedHostsAccessControlPolicy extends BaseAccessControlPolicy {
@Override
public void onServiceRemoteAccess(
Method method, Object[] arguments,
AccessControlled accessControlled)
throws SecurityException {
if (!accessControlled.hostAllowedValidationEnabled()) {
return;
}
AccessControlContext accessControlContext =
AccessControlUtil.getAccessControlContext();
if (accessControlContext == null) {
return;
}
Map<String, Object> settings = accessControlContext.getSettings();
int serviceDepth = (Integer)settings.get(
AccessControlContext.Settings.SERVICE_DEPTH.toString());
if (serviceDepth > 1) {
return;
}
HttpServletRequest request = accessControlContext.getRequest();
String hostsAllowedString = MapUtil.getString(
accessControlContext.getSettings(), "hosts.allowed");
String[] hostsAllowed = StringUtil.split(hostsAllowedString);
Set<String> hostsAllowedSet = SetUtil.fromArray(hostsAllowed);
if (!AccessControlUtil.isAccessAllowed(request, hostsAllowedSet)) {
throw new SecurityException(
"Access denied for " + request.getRemoteAddr());
}
}
}