Class CryptoMapper
- java.lang.Object
-
- org.apache.wicket.core.request.mapper.CryptoMapper
-
- All Implemented Interfaces:
org.apache.wicket.request.IRequestMapper,org.apache.wicket.request.mapper.IRequestMapperDelegate
public class CryptoMapper extends java.lang.Object implements org.apache.wicket.request.mapper.IRequestMapperDelegateA 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 thePageComponentInfoparameter for mounted URLs.Important: for better security it is recommended to use
CryptoMapper(IRequestMapper, Supplier)constructor withICryptimplementation that generates a separate key for each user.KeyInSessionSunJceCryptFactoryprovides 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, multipleCryptoMappers may be installed in anApplication.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
PageComponentInfoparameter, and encrypt only that parameter.CryptoMappercan be configured to mark encrypted URLs as encrypted, and throw aPageExpiredExceptionexception if a encrypted URL cannot be decrypted. This can occur when usingKeyInSessionSunJceCryptFactory, and the session has expired.- Author:
- igor.vaynberg, Jesse Long, svenmeier
- See Also:
SecuritySettings.setCryptFactory(org.apache.wicket.util.crypt.ICryptFactory),KeyInSessionSunJceCryptFactory,SunJceCrypt
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCryptoMapper.HashedSegmentGeneratorA generator of hashed segments.
-
Constructor Summary
Constructors Constructor Description CryptoMapper(org.apache.wicket.request.IRequestMapper wrappedMapper, java.util.function.Supplier<org.apache.wicket.util.crypt.ICrypt> cryptProvider)Construct.CryptoMapper(org.apache.wicket.request.IRequestMapper wrappedMapper, Application application)Encrypt withSecuritySettings.getCryptFactory().
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected org.apache.wicket.request.UrldecryptEntireUrl(org.apache.wicket.request.Request request, org.apache.wicket.request.Url encryptedUrl)Decrypts an entire URL, which was previously encrypted byencryptEntireUrl(org.apache.wicket.request.Url).protected org.apache.wicket.request.UrldecryptRequestListenerParameter(org.apache.wicket.request.Request request, org.apache.wicket.request.Url encryptedUrl)Decrypts a URL which may contain an encryptedPageComponentInfoquery parameter.protected org.apache.wicket.request.UrldecryptUrl(org.apache.wicket.request.Request request, org.apache.wicket.request.Url encryptedUrl)Decrypts aUrl.protected org.apache.wicket.request.UrlencryptEntireUrl(org.apache.wicket.request.Url url)Encrypts an entire URL, segments and query parameters.protected org.apache.wicket.request.UrlencryptRequestListenerParameter(org.apache.wicket.request.Url url)Encrypts thePageComponentInfoquery parameter in the URL, if any is found.protected org.apache.wicket.request.UrlencryptUrl(org.apache.wicket.request.Url url)Encrypts a URL.intgetCompatibilityScore(org.apache.wicket.request.Request request)protected IMapperContextgetContext()Returns the applicationsIMapperContext.protected org.apache.wicket.util.crypt.ICryptgetCrypt()org.apache.wicket.request.IRequestMappergetDelegateMapper()booleangetMarkEncryptedUrls()Whether or not to mark encrypted URLs as encrypted.org.apache.wicket.request.UrlmapHandler(org.apache.wicket.request.IRequestHandler requestHandler)org.apache.wicket.request.IRequestHandlermapRequest(org.apache.wicket.request.Request request)CryptoMappersetMarkEncryptedUrls(boolean markEncryptedUrls)Sets whether or not to mark encrypted URLs as encrypted.
-
-
-
Constructor Detail
-
CryptoMapper
public CryptoMapper(org.apache.wicket.request.IRequestMapper wrappedMapper, Application application)Encrypt withSecuritySettings.getCryptFactory().Important: Encryption is done with
SecuritySettings.DEFAULT_ENCRYPTION_KEYif you haven't configured an alternativeICryptFactory. For better security it is recommended to useCryptoMapper(IRequestMapper, Supplier)with a specificICryptimplementation that generates a separate key for each user.KeyInSessionSunJceCryptFactoryprovides such an implementation that stores the key in the HTTP session.- Parameters:
wrappedMapper- the non-crypted request mapperapplication- the current application- See Also:
SunJceCrypt
-
CryptoMapper
public CryptoMapper(org.apache.wicket.request.IRequestMapper wrappedMapper, java.util.function.Supplier<org.apache.wicket.util.crypt.ICrypt> cryptProvider)Construct.- Parameters:
wrappedMapper- the non-crypted request mappercryptProvider- the custom crypt provider
-
-
Method Detail
-
getMarkEncryptedUrls
public boolean getMarkEncryptedUrls()
Whether or not to mark encrypted URLs as encrypted. If set, aPageExpiredExceptionis thrown when a encrypted URL can no longer be decrypted.- Returns:
- whether or not to mark encrypted URLs as encrypted.
-
setMarkEncryptedUrls
public CryptoMapper setMarkEncryptedUrls(boolean markEncryptedUrls)
Sets whether or not to mark encrypted URLs as encrypted. If set, aPageExpiredExceptionis thrown when a encrypted URL can no longer be decrypted.- Parameters:
markEncryptedUrls- whether or not to mark encrypted URLs as encrypted.- Returns:
this, for chaining.
-
getCompatibilityScore
public int getCompatibilityScore(org.apache.wicket.request.Request request)
This implementation decrypts the URL and passes the decrypted URL to the wrapped mapper.
- Specified by:
getCompatibilityScorein interfaceorg.apache.wicket.request.IRequestMapper- Parameters:
request- The request for which to get a compatibility score.- Returns:
- The compatibility score.
-
mapHandler
public org.apache.wicket.request.Url mapHandler(org.apache.wicket.request.IRequestHandler requestHandler)
- Specified by:
mapHandlerin interfaceorg.apache.wicket.request.IRequestMapper
-
mapRequest
public org.apache.wicket.request.IRequestHandler mapRequest(org.apache.wicket.request.Request request)
- Specified by:
mapRequestin interfaceorg.apache.wicket.request.IRequestMapper
-
getCrypt
protected final org.apache.wicket.util.crypt.ICrypt getCrypt()
- Returns:
- the
ICryptimplementation that may be used to encrypt/decryptUrl's segments and/or query string
-
getDelegateMapper
public final org.apache.wicket.request.IRequestMapper getDelegateMapper()
- Specified by:
getDelegateMapperin interfaceorg.apache.wicket.request.mapper.IRequestMapperDelegate- Returns:
- the wrapped root request mapper
-
getContext
protected IMapperContext getContext()
Returns the applicationsIMapperContext.- Returns:
- The applications
IMapperContext.
-
encryptUrl
protected org.apache.wicket.request.Url encryptUrl(org.apache.wicket.request.Url url)
Encrypts a URL. This method should return a new, encrypted instance of the URL. If the URL starts with/wicket/, the entire URL is encrypted.- Parameters:
url- The URL to encrypt.- Returns:
- A new, encrypted version of the URL.
-
encryptEntireUrl
protected org.apache.wicket.request.Url encryptEntireUrl(org.apache.wicket.request.Url url)
Encrypts an entire URL, segments and query parameters.- Parameters:
url- The URL to encrypt.- Returns:
- An encrypted form of the URL.
-
encryptRequestListenerParameter
protected org.apache.wicket.request.Url encryptRequestListenerParameter(org.apache.wicket.request.Url url)
Encrypts thePageComponentInfoquery parameter in the URL, if any is found.- Parameters:
url- The URL to encrypt.- Returns:
- An encrypted form of the URL.
-
decryptUrl
protected org.apache.wicket.request.Url decryptUrl(org.apache.wicket.request.Request request, org.apache.wicket.request.Url encryptedUrl)Decrypts aUrl. This method should returnnullif the URL is not decryptable, or if the URL should have been encrypted but was not. Returningnullresults in a 404 error.- Parameters:
request- TheRequest.encryptedUrl- The encryptedUrl.- Returns:
- Returns a decrypted
Url.
-
decryptEntireUrl
protected org.apache.wicket.request.Url decryptEntireUrl(org.apache.wicket.request.Request request, org.apache.wicket.request.Url encryptedUrl)Decrypts an entire URL, which was previously encrypted byencryptEntireUrl(org.apache.wicket.request.Url). This method should returnnullif the URL is not decryptable.- Parameters:
request- The request that was made.encryptedUrl- The encrypted URL.- Returns:
- A decrypted form of the URL, or
nullif the URL is not decryptable.
-
decryptRequestListenerParameter
protected org.apache.wicket.request.Url decryptRequestListenerParameter(org.apache.wicket.request.Request request, org.apache.wicket.request.Url encryptedUrl)Decrypts a URL which may contain an encryptedPageComponentInfoquery parameter.- Parameters:
request- The request that was made.encryptedUrl- The (potentially) encrypted URL.- Returns:
- A decrypted form of the URL.
-
-