Jetty9 - Code cleanups: using generics, diamond operator, removed warnings, etc.
This commit is contained in:
parent
c84b496330
commit
360bdfa051
|
@ -84,37 +84,35 @@ public class PathMap<O> extends HashMap<String,O>
|
|||
boolean _nodefault=false;
|
||||
|
||||
/* --------------------------------------------------------------- */
|
||||
/** Construct empty PathMap.
|
||||
*/
|
||||
public PathMap()
|
||||
{
|
||||
super(11);
|
||||
_entrySet=entrySet();
|
||||
this(11);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------- */
|
||||
/** Construct empty PathMap.
|
||||
*/
|
||||
public PathMap(boolean nodefault)
|
||||
public PathMap(boolean noDefault)
|
||||
{
|
||||
super(11);
|
||||
_entrySet=entrySet();
|
||||
_nodefault=nodefault;
|
||||
this(11, noDefault);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------- */
|
||||
/** Construct empty PathMap.
|
||||
*/
|
||||
public PathMap(int capacity)
|
||||
{
|
||||
super (capacity);
|
||||
this(capacity, false);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------- */
|
||||
private PathMap(int capacity, boolean noDefault)
|
||||
{
|
||||
super(capacity);
|
||||
_nodefault=noDefault;
|
||||
_entrySet=entrySet();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------- */
|
||||
/** Construct from dictionary PathMap.
|
||||
*/
|
||||
public PathMap(Map m)
|
||||
public PathMap(Map<String, ? extends O> m)
|
||||
{
|
||||
putAll(m);
|
||||
_entrySet=entrySet();
|
||||
|
@ -129,16 +127,15 @@ public class PathMap<O> extends HashMap<String,O>
|
|||
@Override
|
||||
public O put(String pathSpec, O object)
|
||||
{
|
||||
String str = pathSpec.toString();
|
||||
if ("".equals(str.trim()))
|
||||
if ("".equals(pathSpec.trim()))
|
||||
{
|
||||
MappedEntry entry = new MappedEntry("",object);
|
||||
MappedEntry<O> entry = new MappedEntry<>("",object);
|
||||
entry.setMapped("");
|
||||
_exactMap.put("", entry);
|
||||
return super.put("", object);
|
||||
}
|
||||
|
||||
StringTokenizer tok = new StringTokenizer(str,__pathSpecSeparators);
|
||||
StringTokenizer tok = new StringTokenizer(pathSpec,__pathSpecSeparators);
|
||||
O old =null;
|
||||
|
||||
while (tok.hasMoreTokens())
|
||||
|
@ -151,7 +148,7 @@ public class PathMap<O> extends HashMap<String,O>
|
|||
old = super.put(spec,object);
|
||||
|
||||
// Make entry that was just created.
|
||||
MappedEntry entry = new MappedEntry(spec,object);
|
||||
MappedEntry<O> entry = new MappedEntry<>(spec,object);
|
||||
|
||||
if (entry.getKey().equals(spec))
|
||||
{
|
||||
|
@ -194,9 +191,9 @@ public class PathMap<O> extends HashMap<String,O>
|
|||
* @param path the path.
|
||||
* @return Best matched object or null.
|
||||
*/
|
||||
public Object match(String path)
|
||||
public O match(String path)
|
||||
{
|
||||
Map.Entry entry = getMatch(path);
|
||||
MappedEntry<O> entry = getMatch(path);
|
||||
if (entry!=null)
|
||||
return entry.getValue();
|
||||
return null;
|
||||
|
@ -210,13 +207,13 @@ public class PathMap<O> extends HashMap<String,O>
|
|||
*/
|
||||
public MappedEntry<O> getMatch(String path)
|
||||
{
|
||||
MappedEntry<O> entry=null;
|
||||
|
||||
if (path==null)
|
||||
return null;
|
||||
|
||||
int l=path.length();
|
||||
|
||||
MappedEntry<O> entry=null;
|
||||
|
||||
//special case
|
||||
if (l == 1 && path.charAt(0)=='/')
|
||||
{
|
||||
|
|
|
@ -15,7 +15,6 @@ package org.eclipse.jetty.security;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
@ -37,16 +36,16 @@ import org.eclipse.jetty.util.security.Constraint;
|
|||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Handler to enforce SecurityConstraints. This implementation is servlet spec
|
||||
* 2.4 compliant and precomputes the constraint combinations for runtime
|
||||
* 2.4 compliant and pre-computes the constraint combinations for runtime
|
||||
* efficiency.
|
||||
*
|
||||
*/
|
||||
public class ConstraintSecurityHandler extends SecurityHandler implements ConstraintAware
|
||||
{
|
||||
private static final String ALL_METHODS = "*";
|
||||
private final List<ConstraintMapping> _constraintMappings= new CopyOnWriteArrayList<ConstraintMapping>();
|
||||
private final Set<String> _roles = new CopyOnWriteArraySet<String>();
|
||||
private final PathMap _constraintMap = new PathMap();
|
||||
private final List<ConstraintMapping> _constraintMappings= new CopyOnWriteArrayList<>();
|
||||
private final Set<String> _roles = new CopyOnWriteArraySet<>();
|
||||
private final PathMap<Map<String, RoleInfo>> _constraintMap = new PathMap<>();
|
||||
private boolean _strict = true;
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
@ -139,7 +138,7 @@ public class ConstraintSecurityHandler extends SecurityHandler implements Constr
|
|||
|
||||
if (roles==null)
|
||||
{
|
||||
roles = new HashSet<String>();
|
||||
roles = new HashSet<>();
|
||||
for (ConstraintMapping cm : constraintMappings)
|
||||
{
|
||||
String[] cmr = cm.getConstraint().getRoles();
|
||||
|
@ -197,10 +196,10 @@ public class ConstraintSecurityHandler extends SecurityHandler implements Constr
|
|||
public void addRole(String role)
|
||||
{
|
||||
boolean modified = _roles.add(role);
|
||||
if (isStarted() && modified && _strict)
|
||||
if (isStarted() && modified && isStrict())
|
||||
{
|
||||
// Add the new role to currently defined any role role infos
|
||||
for (Map<String,RoleInfo> map : (Collection<Map<String,RoleInfo>>)_constraintMap.values())
|
||||
for (Map<String,RoleInfo> map : _constraintMap.values())
|
||||
{
|
||||
for (RoleInfo info : map.values())
|
||||
{
|
||||
|
@ -240,10 +239,10 @@ public class ConstraintSecurityHandler extends SecurityHandler implements Constr
|
|||
|
||||
protected void processConstraintMapping(ConstraintMapping mapping)
|
||||
{
|
||||
Map<String, RoleInfo> mappings = (Map<String, RoleInfo>)_constraintMap.get(mapping.getPathSpec());
|
||||
Map<String, RoleInfo> mappings = _constraintMap.get(mapping.getPathSpec());
|
||||
if (mappings == null)
|
||||
{
|
||||
mappings = new StringMap();
|
||||
mappings = new StringMap<>();
|
||||
_constraintMap.put(mapping.getPathSpec(),mappings);
|
||||
}
|
||||
RoleInfo allMethodsRoleInfo = mappings.get(ALL_METHODS);
|
||||
|
@ -323,9 +322,9 @@ public class ConstraintSecurityHandler extends SecurityHandler implements Constr
|
|||
}
|
||||
}
|
||||
|
||||
protected Object prepareConstraintInfo(String pathInContext, Request request)
|
||||
protected RoleInfo prepareConstraintInfo(String pathInContext, Request request)
|
||||
{
|
||||
Map<String, RoleInfo> mappings = (Map<String, RoleInfo>)_constraintMap.match(pathInContext);
|
||||
Map<String, RoleInfo> mappings = _constraintMap.match(pathInContext);
|
||||
|
||||
if (mappings != null)
|
||||
{
|
||||
|
@ -339,31 +338,28 @@ public class ConstraintSecurityHandler extends SecurityHandler implements Constr
|
|||
return null;
|
||||
}
|
||||
|
||||
protected boolean checkUserDataPermissions(String pathInContext, Request request, Response response, Object constraintInfo) throws IOException
|
||||
@Override
|
||||
protected boolean checkUserDataPermissions(String pathInContext, Request request, Response response, RoleInfo roleInfo) throws IOException
|
||||
{
|
||||
if (constraintInfo == null)
|
||||
if (roleInfo == null)
|
||||
return true;
|
||||
|
||||
RoleInfo roleInfo = (RoleInfo)constraintInfo;
|
||||
if (roleInfo.isForbidden())
|
||||
return false;
|
||||
|
||||
|
||||
UserDataConstraint dataConstraint = roleInfo.getUserDataConstraint();
|
||||
if (dataConstraint == null || dataConstraint == UserDataConstraint.None)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
HttpConfiguration connector = HttpChannel.getCurrentHttpChannel().getHttpConfiguration();
|
||||
|
||||
HttpConfiguration httpConfiguration = HttpChannel.getCurrentHttpChannel().getHttpConfiguration();
|
||||
|
||||
if (dataConstraint == UserDataConstraint.Integral)
|
||||
{
|
||||
if (connector.isIntegral(request))
|
||||
if (httpConfiguration.isIntegral(request))
|
||||
return true;
|
||||
if (connector.getIntegralPort() > 0)
|
||||
if (httpConfiguration.getIntegralPort() > 0)
|
||||
{
|
||||
String url = connector.getIntegralScheme() + "://" + request.getServerName() + ":" + connector.getIntegralPort() + request.getRequestURI();
|
||||
String url = httpConfiguration.getIntegralScheme() + "://" + request.getServerName() + ":" + httpConfiguration.getIntegralPort() + request.getRequestURI();
|
||||
if (request.getQueryString() != null)
|
||||
url += "?" + request.getQueryString();
|
||||
response.setContentLength(0);
|
||||
|
@ -377,12 +373,12 @@ public class ConstraintSecurityHandler extends SecurityHandler implements Constr
|
|||
}
|
||||
else if (dataConstraint == UserDataConstraint.Confidential)
|
||||
{
|
||||
if (connector.isConfidential(request))
|
||||
if (httpConfiguration.isConfidential(request))
|
||||
return true;
|
||||
|
||||
if (connector.getConfidentialPort() > 0)
|
||||
if (httpConfiguration.getConfidentialPort() > 0)
|
||||
{
|
||||
String url = connector.getConfidentialScheme() + "://" + request.getServerName() + ":" + connector.getConfidentialPort()
|
||||
String url = httpConfiguration.getConfidentialScheme() + "://" + request.getServerName() + ":" + httpConfiguration.getConfidentialPort()
|
||||
+ request.getRequestURI();
|
||||
if (request.getQueryString() != null)
|
||||
url += "?" + request.getQueryString();
|
||||
|
@ -405,11 +401,7 @@ public class ConstraintSecurityHandler extends SecurityHandler implements Constr
|
|||
|
||||
protected boolean isAuthMandatory(Request baseRequest, Response base_response, Object constraintInfo)
|
||||
{
|
||||
if (constraintInfo == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return ((RoleInfo)constraintInfo).isChecked();
|
||||
return constraintInfo != null && ((RoleInfo)constraintInfo).isChecked();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -4,11 +4,11 @@
|
|||
// All rights reserved. This program and the accompanying materials
|
||||
// are made available under the terms of the Eclipse Public License v1.0
|
||||
// and Apache License v2.0 which accompanies this distribution.
|
||||
// The Eclipse Public License is available at
|
||||
// The Eclipse Public License is available at
|
||||
// http://www.eclipse.org/legal/epl-v10.html
|
||||
// The Apache License v2.0 is available at
|
||||
// http://www.opensource.org/licenses/apache2.0.php
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
|
||||
package org.eclipse.jetty.security;
|
||||
|
@ -20,7 +20,6 @@ import java.util.HashMap;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
@ -46,11 +45,11 @@ import org.eclipse.jetty.util.log.Logger;
|
|||
* or will be create during {@link #start()} with a call to
|
||||
* either the default or set AuthenticatorFactory.
|
||||
* <p>
|
||||
* SecurityHandler has a set of initparameters that are used by the
|
||||
* SecurityHandler has a set of initparameters that are used by the
|
||||
* Authentication.Configuration. At startup, any context init parameters
|
||||
* that start with "org.eclipse.jetty.security." that do not have
|
||||
* values in the SecurityHandler init parameters, are copied.
|
||||
*
|
||||
* that start with "org.eclipse.jetty.security." that do not have
|
||||
* values in the SecurityHandler init parameters, are copied.
|
||||
*
|
||||
*/
|
||||
public abstract class SecurityHandler extends HandlerWrapper implements Authenticator.AuthConfiguration
|
||||
{
|
||||
|
@ -62,7 +61,7 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
|
|||
private Authenticator.Factory _authenticatorFactory=new DefaultAuthenticatorFactory();
|
||||
private String _realmName;
|
||||
private String _authMethod;
|
||||
private final Map<String,String> _initParameters=new HashMap<String,String>();
|
||||
private final Map<String,String> _initParameters=new HashMap<>();
|
||||
private LoginService _loginService;
|
||||
private boolean _loginServiceShared;
|
||||
private IdentityService _identityService;
|
||||
|
@ -72,7 +71,7 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
|
|||
protected SecurityHandler()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Get the identityService.
|
||||
* @return the identityService
|
||||
|
@ -195,7 +194,7 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
|
|||
throw new IllegalStateException("running");
|
||||
_authMethod = authMethod;
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* @return True if forwards to welcome files are authenticated
|
||||
|
@ -223,13 +222,13 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
|
|||
{
|
||||
return _initParameters.get(key);
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public Set<String> getInitParameterNames()
|
||||
{
|
||||
return _initParameters.keySet();
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Set an initialization parameter.
|
||||
* @param key
|
||||
|
@ -243,12 +242,12 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
|
|||
throw new IllegalStateException("running");
|
||||
return _initParameters.put(key,value);
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
protected LoginService findLoginService()
|
||||
{
|
||||
List<LoginService> list = getServer().getBeans(LoginService.class);
|
||||
|
||||
|
||||
String realm=getRealmName();
|
||||
if (realm!=null)
|
||||
{
|
||||
|
@ -260,15 +259,15 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
|
|||
return list.get(0);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
protected IdentityService findIdentityService()
|
||||
{
|
||||
return getServer().getBean(IdentityService.class);
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
/**
|
||||
*/
|
||||
@Override
|
||||
protected void doStart()
|
||||
|
@ -287,17 +286,17 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
|
|||
setInitParameter(name,context.getInitParameter(name));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// complicated resolution of login and identity service to handle
|
||||
// many different ways these can be constructed and injected.
|
||||
|
||||
|
||||
if (_loginService==null)
|
||||
{
|
||||
_loginService=findLoginService();
|
||||
if (_loginService!=null)
|
||||
_loginServiceShared=true;
|
||||
}
|
||||
|
||||
|
||||
if (_identityService==null)
|
||||
{
|
||||
if (_loginService!=null)
|
||||
|
@ -305,11 +304,11 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
|
|||
|
||||
if (_identityService==null)
|
||||
_identityService=findIdentityService();
|
||||
|
||||
|
||||
if (_identityService==null && _realmName!=null)
|
||||
_identityService=new DefaultIdentityService();
|
||||
}
|
||||
|
||||
|
||||
if (_loginService!=null)
|
||||
{
|
||||
if (_loginService.getIdentityService()==null)
|
||||
|
@ -319,11 +318,12 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
|
|||
}
|
||||
|
||||
if (!_loginServiceShared && _loginService instanceof LifeCycle)
|
||||
((LifeCycle)_loginService).start();
|
||||
|
||||
if (_authenticator==null && _authenticatorFactory!=null && _identityService!=null)
|
||||
((LifeCycle)_loginService).start();
|
||||
|
||||
Authenticator.Factory authenticatorFactory = getAuthenticatorFactory();
|
||||
if (_authenticator==null && authenticatorFactory!=null && _identityService!=null)
|
||||
{
|
||||
_authenticator=_authenticatorFactory.getAuthenticator(getServer(),ContextHandler.getCurrentContext(),this, _identityService, _loginService);
|
||||
_authenticator=authenticatorFactory.getAuthenticator(getServer(),ContextHandler.getCurrentContext(),this, _identityService, _loginService);
|
||||
if (_authenticator!=null)
|
||||
_authMethod=_authenticator.getAuthMethod();
|
||||
}
|
||||
|
@ -354,10 +354,10 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
|
|||
protected void doStop() throws Exception
|
||||
{
|
||||
super.doStop();
|
||||
|
||||
|
||||
if (!_loginServiceShared && _loginService instanceof LifeCycle)
|
||||
((LifeCycle)_loginService).stop();
|
||||
|
||||
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
@ -369,7 +369,7 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
|
|||
case ASYNC:
|
||||
return true;
|
||||
case FORWARD:
|
||||
if (_checkWelcomeFiles && request.getAttribute("org.eclipse.jetty.server.welcome") != null)
|
||||
if (isCheckWelcomeFiles() && request.getAttribute("org.eclipse.jetty.server.welcome") != null)
|
||||
{
|
||||
request.removeAttribute("org.eclipse.jetty.server.welcome");
|
||||
return true;
|
||||
|
@ -379,7 +379,7 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
|
|||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* @see org.eclipse.jetty.security.Authenticator.AuthConfiguration#isSessionRenewedOnAuthentication()
|
||||
|
@ -388,7 +388,7 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
|
|||
{
|
||||
return _renewSession;
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Set renew the session on Authentication.
|
||||
* <p>
|
||||
|
@ -399,7 +399,7 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
|
|||
{
|
||||
_renewSession=renew;
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/*
|
||||
* @see org.eclipse.jetty.server.Handler#handle(java.lang.String,
|
||||
|
@ -411,18 +411,18 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
|
|||
{
|
||||
final Response base_response = baseRequest.getResponse();
|
||||
final Handler handler=getHandler();
|
||||
|
||||
|
||||
if (handler==null)
|
||||
return;
|
||||
|
||||
final Authenticator authenticator = _authenticator;
|
||||
|
||||
|
||||
if (checkSecurity(baseRequest))
|
||||
{
|
||||
Object constraintInfo = prepareConstraintInfo(pathInContext, baseRequest);
|
||||
|
||||
RoleInfo roleInfo = prepareConstraintInfo(pathInContext, baseRequest);
|
||||
|
||||
// Check data constraints
|
||||
if (!checkUserDataPermissions(pathInContext, baseRequest, base_response, constraintInfo))
|
||||
if (!checkUserDataPermissions(pathInContext, baseRequest, base_response, roleInfo))
|
||||
{
|
||||
if (!baseRequest.isHandled())
|
||||
{
|
||||
|
@ -433,12 +433,12 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
|
|||
}
|
||||
|
||||
// is Auth mandatory?
|
||||
boolean isAuthMandatory =
|
||||
isAuthMandatory(baseRequest, base_response, constraintInfo);
|
||||
boolean isAuthMandatory =
|
||||
isAuthMandatory(baseRequest, base_response, roleInfo);
|
||||
|
||||
if (isAuthMandatory && authenticator==null)
|
||||
{
|
||||
LOG.warn("No authenticator for: "+constraintInfo);
|
||||
LOG.warn("No authenticator for: "+roleInfo);
|
||||
if (!baseRequest.isHandled())
|
||||
{
|
||||
response.sendError(Response.SC_FORBIDDEN);
|
||||
|
@ -446,7 +446,7 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
|
|||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// check authentication
|
||||
Object previousIdentity = null;
|
||||
try
|
||||
|
@ -474,7 +474,7 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
|
|||
|
||||
if (isAuthMandatory)
|
||||
{
|
||||
boolean authorized=checkWebResourcePermissions(pathInContext, baseRequest, base_response, constraintInfo, userAuth.getUserIdentity());
|
||||
boolean authorized=checkWebResourcePermissions(pathInContext, baseRequest, base_response, roleInfo, userAuth.getUserIdentity());
|
||||
if (!authorized)
|
||||
{
|
||||
response.sendError(Response.SC_FORBIDDEN, "!role");
|
||||
|
@ -482,7 +482,7 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
|
|||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
handler.handle(pathInContext, baseRequest, request, response);
|
||||
if (authenticator!=null)
|
||||
authenticator.secureResponse(request, response, isAuthMandatory, userAuth);
|
||||
|
@ -549,9 +549,8 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
|
|||
Context context = ContextHandler.getCurrentContext();
|
||||
if (context==null)
|
||||
return null;
|
||||
|
||||
SecurityHandler security = context.getContextHandler().getChildHandlerByClass(SecurityHandler.class);
|
||||
return security;
|
||||
|
||||
return context.getContextHandler().getChildHandlerByClass(SecurityHandler.class);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
@ -563,7 +562,7 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
|
|||
{
|
||||
login_service.logout(user.getUserIdentity());
|
||||
}
|
||||
|
||||
|
||||
IdentityService identity_service=getIdentityService();
|
||||
if (identity_service!=null)
|
||||
{
|
||||
|
@ -572,12 +571,12 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
|
|||
identity_service.disassociate(previous);
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
protected abstract Object prepareConstraintInfo(String pathInContext, Request request);
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
protected abstract boolean checkUserDataPermissions(String pathInContext, Request request, Response response, Object constraintInfo) throws IOException;
|
||||
protected abstract RoleInfo prepareConstraintInfo(String pathInContext, Request request);
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
protected abstract boolean checkUserDataPermissions(String pathInContext, Request request, Response response, RoleInfo constraintInfo) throws IOException;
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
protected abstract boolean isAuthMandatory(Request baseRequest, Response base_response, Object constraintInfo);
|
||||
|
@ -586,7 +585,7 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
|
|||
protected abstract boolean checkWebResourcePermissions(String pathInContext, Request request, Response response, Object constraintInfo,
|
||||
UserIdentity userIdentity) throws IOException;
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/* ------------------------------------------------------------ */
|
||||
public class NotChecked implements Principal
|
||||
|
@ -608,7 +607,7 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/* ------------------------------------------------------------ */
|
||||
public static Principal __NO_USER = new Principal()
|
||||
|
@ -624,7 +623,7 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
|
|||
return "No User";
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
|
|
|
@ -13,19 +13,14 @@
|
|||
|
||||
package org.eclipse.jetty.security;
|
||||
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.matchers.JUnitMatchers.containsString;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
@ -48,6 +43,12 @@ import org.junit.After;
|
|||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.matchers.JUnitMatchers.containsString;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1441 $ $Date: 2010-04-02 12:28:17 +0200 (Fri, 02 Apr 2010) $
|
||||
*/
|
||||
|
@ -56,7 +57,6 @@ public class ConstraintTest
|
|||
private static final String TEST_REALM = "TestRealm";
|
||||
private Server _server;
|
||||
private LocalConnector _connector;
|
||||
private SessionHandler _session;
|
||||
private ConstraintSecurityHandler _security;
|
||||
|
||||
@Before
|
||||
|
@ -67,7 +67,7 @@ public class ConstraintTest
|
|||
_server.setConnectors(new Connector[]{_connector});
|
||||
|
||||
ContextHandler _context = new ContextHandler();
|
||||
_session = new SessionHandler();
|
||||
SessionHandler _session = new SessionHandler();
|
||||
|
||||
HashLoginService _loginService = new HashLoginService(TEST_REALM);
|
||||
_loginService.putUser("user",new Password("password"));
|
||||
|
@ -79,7 +79,7 @@ public class ConstraintTest
|
|||
_context.setHandler(_session);
|
||||
|
||||
_server.addBean(_loginService);
|
||||
|
||||
|
||||
_security = new ConstraintSecurityHandler();
|
||||
_session.setHandler(_security);
|
||||
RequestHandler _handler = new RequestHandler();
|
||||
|
@ -131,16 +131,13 @@ public class ConstraintTest
|
|||
mapping5.setPathSpec("/forbid/post");
|
||||
mapping5.setConstraint(constraint5);
|
||||
mapping5.setMethod("POST");
|
||||
|
||||
|
||||
Set<String> knownRoles=new HashSet<String>();
|
||||
|
||||
|
||||
Set<String> knownRoles=new HashSet<>();
|
||||
knownRoles.add("user");
|
||||
knownRoles.add("administrator");
|
||||
|
||||
_security.setConstraintMappings(Arrays.asList(new ConstraintMapping[]
|
||||
{
|
||||
mapping0, mapping1, mapping2, mapping3, mapping4, mapping5
|
||||
}), knownRoles);
|
||||
_security.setConstraintMappings(Arrays.asList(mapping0, mapping1, mapping2, mapping3, mapping4, mapping5), knownRoles);
|
||||
}
|
||||
|
||||
@After
|
||||
|
@ -152,27 +149,27 @@ public class ConstraintTest
|
|||
@Test
|
||||
public void testConstraints() throws Exception
|
||||
{
|
||||
ConstraintMapping[] mappings =_security.getConstraintMappings().toArray(new ConstraintMapping[0]);
|
||||
List<ConstraintMapping> mappings = new ArrayList<>(_security.getConstraintMappings());
|
||||
|
||||
assertTrue (mappings[0].getConstraint().isForbidden());
|
||||
assertFalse(mappings[1].getConstraint().isForbidden());
|
||||
assertFalse(mappings[2].getConstraint().isForbidden());
|
||||
assertFalse(mappings[3].getConstraint().isForbidden());
|
||||
assertTrue (mappings.get(0).getConstraint().isForbidden());
|
||||
assertFalse(mappings.get(1).getConstraint().isForbidden());
|
||||
assertFalse(mappings.get(2).getConstraint().isForbidden());
|
||||
assertFalse(mappings.get(3).getConstraint().isForbidden());
|
||||
|
||||
assertFalse(mappings[0].getConstraint().isAnyRole());
|
||||
assertTrue (mappings[1].getConstraint().isAnyRole());
|
||||
assertFalse(mappings[2].getConstraint().isAnyRole());
|
||||
assertFalse(mappings[3].getConstraint().isAnyRole());
|
||||
assertFalse(mappings.get(0).getConstraint().isAnyRole());
|
||||
assertTrue (mappings.get(1).getConstraint().isAnyRole());
|
||||
assertFalse(mappings.get(2).getConstraint().isAnyRole());
|
||||
assertFalse(mappings.get(3).getConstraint().isAnyRole());
|
||||
|
||||
assertFalse(mappings[0].getConstraint().hasRole("administrator"));
|
||||
assertTrue (mappings[1].getConstraint().hasRole("administrator"));
|
||||
assertTrue (mappings[2].getConstraint().hasRole("administrator"));
|
||||
assertFalse(mappings[3].getConstraint().hasRole("administrator"));
|
||||
assertFalse(mappings.get(0).getConstraint().hasRole("administrator"));
|
||||
assertTrue (mappings.get(1).getConstraint().hasRole("administrator"));
|
||||
assertTrue (mappings.get(2).getConstraint().hasRole("administrator"));
|
||||
assertFalse(mappings.get(3).getConstraint().hasRole("administrator"));
|
||||
|
||||
assertTrue (mappings[0].getConstraint().getAuthenticate());
|
||||
assertTrue (mappings[1].getConstraint().getAuthenticate());
|
||||
assertTrue (mappings[2].getConstraint().getAuthenticate());
|
||||
assertFalse(mappings[3].getConstraint().getAuthenticate());
|
||||
assertTrue (mappings.get(0).getConstraint().getAuthenticate());
|
||||
assertTrue (mappings.get(1).getConstraint().getAuthenticate());
|
||||
assertTrue (mappings.get(2).getConstraint().getAuthenticate());
|
||||
assertFalse(mappings.get(3).getConstraint().getAuthenticate());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -278,7 +275,7 @@ public class ConstraintTest
|
|||
"Cookie: JSESSIONID=" + session + "\r\n" +
|
||||
"\r\n");
|
||||
assertThat(response,startsWith("HTTP/1.1 200 OK"));
|
||||
|
||||
|
||||
|
||||
response = _connector.getResponses("GET /ctx/admin/info HTTP/1.0\r\n" +
|
||||
"Cookie: JSESSIONID=" + session + "\r\n" +
|
||||
|
@ -316,9 +313,9 @@ public class ConstraintTest
|
|||
response = _connector.getResponses("POST /ctx/j_security_check HTTP/1.0\r\n" +
|
||||
"Cookie: JSESSIONID=" + session + "\r\n" +
|
||||
"Content-Type: application/x-www-form-urlencoded\r\n" +
|
||||
"Content-Length: 31\r\n" +
|
||||
"Content-Length: 32\r\n" +
|
||||
"\r\n" +
|
||||
"j_username=user&j_password=wrong\r\n");
|
||||
"j_username=user&j_password=wrong");
|
||||
assertThat(response,containsString("Location"));
|
||||
|
||||
response = _connector.getResponses("POST /ctx/j_security_check HTTP/1.0\r\n" +
|
||||
|
@ -326,7 +323,7 @@ public class ConstraintTest
|
|||
"Content-Type: application/x-www-form-urlencoded\r\n" +
|
||||
"Content-Length: 35\r\n" +
|
||||
"\r\n" +
|
||||
"j_username=user&j_password=password\r\n");
|
||||
"j_username=user&j_password=password");
|
||||
assertThat(response,startsWith("HTTP/1.1 302 "));
|
||||
assertThat(response,containsString("Location"));
|
||||
assertThat(response,containsString("/ctx/auth/info"));
|
||||
|
@ -413,7 +410,7 @@ public class ConstraintTest
|
|||
assertThat(response,startsWith("HTTP/1.1 403"));
|
||||
assertThat(response,containsString("!role"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testFormNoCookies() throws Exception
|
||||
{
|
||||
|
@ -826,10 +823,10 @@ public class ConstraintTest
|
|||
String response;
|
||||
response = _connector.getResponses("GET /ctx/forbid/somethig HTTP/1.0\r\n\r\n");
|
||||
assertThat(response,startsWith("HTTP/1.1 403 "));
|
||||
|
||||
|
||||
response = _connector.getResponses("POST /ctx/forbid/post HTTP/1.0\r\n\r\n");
|
||||
assertThat(response,startsWith("HTTP/1.1 200 "));
|
||||
|
||||
|
||||
response = _connector.getResponses("GET /ctx/forbid/post HTTP/1.0\r\n\r\n");
|
||||
assertThat(response,startsWith("HTTP/1.1 200 ")); // This is so stupid, but it is the S P E C
|
||||
}
|
||||
|
@ -878,7 +875,7 @@ public class ConstraintTest
|
|||
|
||||
public Map<String, String> getRoleRefMap()
|
||||
{
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("untranslated", "user");
|
||||
return map;
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@ import org.eclipse.jetty.util.ssl.SslContextFactory;
|
|||
*/
|
||||
public abstract class AbstractConnector extends AggregateLifeCycle implements Connector, Dumpable
|
||||
{
|
||||
private final Logger LOG = Log.getLogger(getClass());
|
||||
protected final Logger LOG = Log.getLogger(getClass());
|
||||
// Order is important on server side, so we use a LinkedHashMap
|
||||
private final Map<String, ConnectionFactory> factories = new LinkedHashMap<>();
|
||||
private final Statistics _stats = new ConnectorStatistics();
|
||||
|
@ -216,16 +216,16 @@ public abstract class AbstractConnector extends AggregateLifeCycle implements Co
|
|||
|
||||
protected abstract void accept(int acceptorID) throws IOException, InterruptedException;
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
/**
|
||||
* @return Is the connector accepting new connections
|
||||
*/
|
||||
protected boolean isAccepting()
|
||||
{
|
||||
return isRunning();
|
||||
}
|
||||
|
||||
|
||||
public ConnectionFactory getConnectionFactory(String protocol)
|
||||
{
|
||||
synchronized (factories)
|
||||
|
|
|
@ -19,7 +19,6 @@ import java.io.PrintWriter;
|
|||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
import javax.servlet.DispatcherType;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletInputStream;
|
||||
|
@ -53,24 +52,19 @@ import org.eclipse.jetty.util.resource.Resource;
|
|||
*/
|
||||
public abstract class HttpChannel
|
||||
{
|
||||
static final Logger LOG = Log.getLogger(HttpChannel.class);
|
||||
|
||||
protected static final Logger LOG = Log.getLogger(HttpChannel.class);
|
||||
private static final ThreadLocal<HttpChannel> __currentChannel = new ThreadLocal<>();
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public static HttpChannel getCurrentHttpChannel()
|
||||
{
|
||||
return __currentChannel.get();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
protected static void setCurrentHttpChannel(HttpChannel channel)
|
||||
{
|
||||
__currentChannel.set(channel);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private final Server _server;
|
||||
private final Connection _connection;
|
||||
private final HttpURI _uri;
|
||||
|
@ -312,7 +306,7 @@ public abstract class HttpChannel
|
|||
Thread.currentThread().setName(threadName+" - "+_uri);
|
||||
}
|
||||
|
||||
__currentChannel.set(this);
|
||||
setCurrentHttpChannel(this);
|
||||
try
|
||||
{
|
||||
// Loop here to handle async request redispatches.
|
||||
|
@ -373,7 +367,7 @@ public abstract class HttpChannel
|
|||
}
|
||||
finally
|
||||
{
|
||||
__currentChannel.set(null);
|
||||
setCurrentHttpChannel(null);
|
||||
if (threadName!=null)
|
||||
Thread.currentThread().setName(threadName);
|
||||
|
||||
|
@ -792,7 +786,7 @@ public abstract class HttpChannel
|
|||
}
|
||||
|
||||
public abstract Connector getConnector();
|
||||
|
||||
|
||||
public abstract HttpConfiguration getHttpConfiguration();
|
||||
|
||||
protected abstract int write(ByteBuffer content) throws IOException;
|
||||
|
|
|
@ -27,13 +27,10 @@ import org.eclipse.jetty.io.ByteBufferPool;
|
|||
import org.eclipse.jetty.io.Connection;
|
||||
import org.eclipse.jetty.util.BufferUtil;
|
||||
import org.eclipse.jetty.util.StringUtil;
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
import org.eclipse.jetty.util.log.Logger;
|
||||
import org.eclipse.jetty.util.ssl.SslContextFactory;
|
||||
|
||||
public class LocalConnector extends AbstractConnector
|
||||
{
|
||||
private static final Logger LOG = Log.getLogger(LocalConnector.class);
|
||||
private final BlockingQueue<LocalEndPoint> _connects = new LinkedBlockingQueue<>();
|
||||
|
||||
public LocalConnector(Server server)
|
||||
|
@ -123,12 +120,14 @@ public class LocalConnector extends AbstractConnector
|
|||
*/
|
||||
public ByteBuffer getResponses(ByteBuffer requestsBuffer,long idleFor,TimeUnit units) throws Exception
|
||||
{
|
||||
LOG.debug("getResponses");
|
||||
LOG.debug("requests {}", BufferUtil.toUTF8String(requestsBuffer));
|
||||
LocalEndPoint endp = new LocalEndPoint();
|
||||
endp.setInput(requestsBuffer);
|
||||
_connects.add(endp);
|
||||
endp.waitUntilClosedOrIdleFor(idleFor,units);
|
||||
return endp.takeOutput();
|
||||
ByteBuffer responses = endp.takeOutput();
|
||||
LOG.debug("responses {}", BufferUtil.toUTF8String(responses));
|
||||
return responses;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue