public class CryptoMapper extends Object implements IRequestMapperDelegate
A request mapper that encrypts URLs generated by another mapper. This mapper encrypts the segments
and query parameters of URLs starting with IMapperContext.getNamespace()
, and just the
PageComponentInfo
parameter for mounted URLs.
Important: for better security it is recommended to use
CryptoMapper(IRequestMapper, Supplier)
constructor with ICrypt
implementation that generates a
separate key for each user. KeyInSessionSunJceCryptFactory
provides such an
implementation that stores the key in the HTTP session.
This mapper can be mounted before or after mounting other pages, but will only encrypt URLs for
pages mounted before the CryptoMapper
. If required, multiple CryptoMapper
s may be
installed in an Application
.
When encrypting URLs in the Wicket namespace (starting with IMapperContext.getNamespace()
), the entire URL,
including segments and parameters, is encrypted, with the encrypted form stored in the first segment of the encrypted URL.
To be able to handle relative URLs, like for image URLs in a CSS file, checksum segments are appended to the encrypted URL until the encrypted URL has the same number of segments as the original URL had. Each checksum segment has a precise 5 character value, calculated using a checksum. This helps in calculating the relative distance from the original URL. When a URL is returned by the browser, we iterate through these checksummed placeholder URL segments. If the segment matches the expected checksum, then the segment is deemed to be the corresponding segment in the original URL. If the segment does not match the expected checksum, then the segment is deemed a plain text sibling of the corresponding segment in the original URL, and all subsequent segments are considered plain text children of the current segment.
When encrypting mounted URLs, we look for the PageComponentInfo
parameter, and encrypt only that parameter.
CryptoMapper
can be configured to mark encrypted URLs as encrypted, and throw a PageExpiredException
exception if a encrypted URL cannot be decrypted. This can occur when using KeyInSessionSunJceCryptFactory
, and
the session has expired.
SecuritySettings.setCryptFactory(org.apache.wicket.util.crypt.ICryptFactory)
,
KeyInSessionSunJceCryptFactory
,
SunJceCrypt
Modifier and Type | Class and Description |
---|---|
static class |
CryptoMapper.HashedSegmentGenerator
A generator of hashed segments.
|
Constructor and Description |
---|
CryptoMapper(IRequestMapper wrappedMapper,
Application application)
Encrypt with
SecuritySettings.getCryptFactory() . |
CryptoMapper(IRequestMapper wrappedMapper,
Supplier<ICrypt> cryptProvider)
Construct.
|
Modifier and Type | Method and Description |
---|---|
protected Url |
decryptEntireUrl(Request request,
Url encryptedUrl)
Decrypts an entire URL, which was previously encrypted by
encryptEntireUrl(org.apache.wicket.request.Url) . |
protected Url |
decryptRequestListenerParameter(Request request,
Url encryptedUrl)
Decrypts a URL which may contain an encrypted
PageComponentInfo query parameter. |
protected Url |
decryptUrl(Request request,
Url encryptedUrl)
Decrypts a
Url . |
protected Url |
encryptEntireUrl(Url url)
Encrypts an entire URL, segments and query parameters.
|
protected Url |
encryptRequestListenerParameter(Url url)
Encrypts the
PageComponentInfo query parameter in the URL, if any is found. |
protected Url |
encryptUrl(Url url)
Encrypts a URL.
|
int |
getCompatibilityScore(Request request)
Returns the score representing how compatible this request mapper is to processing the given
request.
|
protected IMapperContext |
getContext()
Returns the applications
IMapperContext . |
protected ICrypt |
getCrypt() |
IRequestMapper |
getDelegateMapper()
Returns the delegate
IRequestMapper . |
boolean |
getMarkEncryptedUrls()
Whether or not to mark encrypted URLs as encrypted.
|
Url |
mapHandler(IRequestHandler requestHandler)
|
IRequestHandler |
mapRequest(Request request)
|
CryptoMapper |
setMarkEncryptedUrls(boolean markEncryptedUrls)
Sets whether or not to mark encrypted URLs as encrypted.
|
public CryptoMapper(IRequestMapper wrappedMapper, Application application)
SecuritySettings.getCryptFactory()
.
Important: Encryption is done with SecuritySettings.DEFAULT_ENCRYPTION_KEY
if you haven't
configured an alternative ICryptFactory
. For better security it is recommended to use
CryptoMapper(IRequestMapper, Supplier)
with a specific ICrypt
implementation
that generates a separate key for each user.
KeyInSessionSunJceCryptFactory
provides such an implementation that stores the
key in the HTTP session.
wrappedMapper
- the non-crypted request mapperapplication
- the current applicationSunJceCrypt
public CryptoMapper(IRequestMapper wrappedMapper, Supplier<ICrypt> cryptProvider)
wrappedMapper
- the non-crypted request mappercryptProvider
- the custom crypt providerpublic boolean getMarkEncryptedUrls()
PageExpiredException
is thrown when
a encrypted URL can no longer be decrypted.public CryptoMapper setMarkEncryptedUrls(boolean markEncryptedUrls)
PageExpiredException
is thrown when
a encrypted URL can no longer be decrypted.markEncryptedUrls
- whether or not to mark encrypted URLs as encrypted.this
, for chaining.public int getCompatibilityScore(Request request)
A good criteria for calculating the score is the number of matched url segments. For example
when there are two mappers for a mounted page, one mapped to /foo
another to
/foo/bar
and the incoming request URL is /foo/bar/baz, the mapping
to /foo/bar
should probably handle the request first as it has matching segments
count of 2 while the first one has only matching segments count of 1.
Note that the method can return value greater then zero even if the mapper does not recognize the request.
This implementation decrypts the URL and passes the decrypted URL to the wrapped mapper.
getCompatibilityScore
in interface IRequestMapper
request
- The request for which to get a compatibility score.public Url mapHandler(IRequestHandler requestHandler)
IRequestMapper
mapHandler
in interface IRequestMapper
null
.public IRequestHandler mapRequest(Request request)
IRequestMapper
mapRequest
in interface IRequestMapper
request
- provides access to request data (i.e. Url and Parameters)null
protected final ICrypt getCrypt()
public final IRequestMapper getDelegateMapper()
IRequestMapperDelegate
IRequestMapper
.getDelegateMapper
in interface IRequestMapperDelegate
protected IMapperContext getContext()
IMapperContext
.IMapperContext
.protected Url encryptUrl(Url url)
/wicket/
,
the entire URL is encrypted.url
- The URL to encrypt.protected Url encryptEntireUrl(Url url)
url
- The URL to encrypt.protected Url encryptRequestListenerParameter(Url url)
PageComponentInfo
query parameter in the URL, if any is found.url
- The URL to encrypt.protected Url decryptUrl(Request request, Url encryptedUrl)
Url
. This method should return null
if the URL is not decryptable, or if the
URL should have been encrypted but was not. Returning null
results in a 404 error.protected Url decryptEntireUrl(Request request, Url encryptedUrl)
encryptEntireUrl(org.apache.wicket.request.Url)
.
This method should return null
if the URL is not decryptable.request
- The request that was made.encryptedUrl
- The encrypted URL.null
if the URL is not decryptable.protected Url decryptRequestListenerParameter(Request request, Url encryptedUrl)
PageComponentInfo
query parameter.request
- The request that was made.encryptedUrl
- The (potentially) encrypted URL.Copyright © 2006–2022 Apache Software Foundation. All rights reserved.