Class MediaTypeRequestMatcher
- java.lang.Object
-
- org.springframework.security.web.util.matcher.MediaTypeRequestMatcher
-
- All Implemented Interfaces:
RequestMatcher
public final class MediaTypeRequestMatcher extends java.lang.Object implements RequestMatcher
Allows matchingHttpServletRequestbased upon theMediaType's resolved from aContentNegotiationStrategy. By default, the matching process will perform the following:- The
ContentNegotiationStrategywill resolve theMediaType's for the current request - Each matchingMediaTypes that was passed into the constructor will be compared
against the
MediaTypeinstances resolved from theContentNegotiationStrategy. - If one of the matchingMediaTypes is compatible with one of the resolved
MediaTypereturned from theContentNegotiationStrategy, then it returns true
GET / Accept: application/json ContentNegotiationStrategy negotiationStrategy = new HeaderContentNegotiationStrategy() MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON); assert matcher.matches(request) == true // returns true
The following will also return trueGET / Accept: */* ContentNegotiationStrategy negotiationStrategy = new HeaderContentNegotiationStrategy() MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON); assert matcher.matches(request) == true // returns true
Ignoring Media Types
Sometimes you may want to ignore certain types of media types. For example, you may want to match on "application/json" but ignore "*/" sent by a web browser.GET / Accept: */* ContentNegotiationStrategy negotiationStrategy = new HeaderContentNegotiationStrategy() MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON); matcher.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL)); assert matcher.matches(request) == false // returns false
GET / Accept: application/json ContentNegotiationStrategy negotiationStrategy = new HeaderContentNegotiationStrategy() MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON); matcher.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL)); assert matcher.matches(request) == true // returns true
Exact media type comparison
By default as long as theMediaTypediscovered byContentNegotiationStrategyreturns true forMediaType.isCompatibleWith(MediaType)on the matchingMediaTypes, the result of the match is true. However, sometimes you may want to perform an exact match. This can be done with the following examples:GET / Accept: application/json ContentNegotiationStrategy negotiationStrategy = new HeaderContentNegotiationStrategy() MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON); matcher.setUseEquals(true); assert matcher.matches(request) == true // returns true
GET / Accept: application/* ContentNegotiationStrategy negotiationStrategy = new HeaderContentNegotiationStrategy() MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON); matcher.setUseEquals(true); assert matcher.matches(request) == false // returns false
GET / Accept: */* ContentNegotiationStrategy negotiationStrategy = new HeaderContentNegotiationStrategy() MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON); matcher.setUseEquals(true); assert matcher.matches(request) == false // returns false
- Since:
- 3.2
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.springframework.security.web.util.matcher.RequestMatcher
RequestMatcher.MatchResult
-
-
Constructor Summary
Constructors Constructor Description MediaTypeRequestMatcher(java.util.Collection<org.springframework.http.MediaType> matchingMediaTypes)Creates an instanceMediaTypeRequestMatcher(org.springframework.http.MediaType... matchingMediaTypes)Creates an instanceMediaTypeRequestMatcher(org.springframework.web.accept.ContentNegotiationStrategy contentNegotiationStrategy, java.util.Collection<org.springframework.http.MediaType> matchingMediaTypes)Creates an instanceMediaTypeRequestMatcher(org.springframework.web.accept.ContentNegotiationStrategy contentNegotiationStrategy, org.springframework.http.MediaType... matchingMediaTypes)Creates an instance
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanmatches(javax.servlet.http.HttpServletRequest request)Decides whether the rule implemented by the strategy matches the supplied request.voidsetIgnoredMediaTypes(java.util.Set<org.springframework.http.MediaType> ignoredMediaTypes)Set theMediaTypeto ignore from theContentNegotiationStrategy.voidsetUseEquals(boolean useEquals)If set to true, matches on exactMediaType, else usesMediaType.isCompatibleWith(MediaType).java.lang.StringtoString()-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.springframework.security.web.util.matcher.RequestMatcher
matcher
-
-
-
-
Constructor Detail
-
MediaTypeRequestMatcher
public MediaTypeRequestMatcher(org.springframework.http.MediaType... matchingMediaTypes)
Creates an instance- Parameters:
matchingMediaTypes- theMediaTypethat will make the http request.- Since:
- 5.2
-
MediaTypeRequestMatcher
public MediaTypeRequestMatcher(java.util.Collection<org.springframework.http.MediaType> matchingMediaTypes)
Creates an instance- Parameters:
matchingMediaTypes- theMediaTypethat will make the http request.- Since:
- 5.2
-
MediaTypeRequestMatcher
public MediaTypeRequestMatcher(org.springframework.web.accept.ContentNegotiationStrategy contentNegotiationStrategy, org.springframework.http.MediaType... matchingMediaTypes)Creates an instance- Parameters:
contentNegotiationStrategy- theContentNegotiationStrategyto usematchingMediaTypes- theMediaTypethat will make theRequestMatcherreturn true
-
MediaTypeRequestMatcher
public MediaTypeRequestMatcher(org.springframework.web.accept.ContentNegotiationStrategy contentNegotiationStrategy, java.util.Collection<org.springframework.http.MediaType> matchingMediaTypes)Creates an instance- Parameters:
contentNegotiationStrategy- theContentNegotiationStrategyto usematchingMediaTypes- theMediaTypethat will make theRequestMatcherreturn true
-
-
Method Detail
-
matches
public boolean matches(javax.servlet.http.HttpServletRequest request)
Description copied from interface:RequestMatcherDecides whether the rule implemented by the strategy matches the supplied request.- Specified by:
matchesin interfaceRequestMatcher- Parameters:
request- the request to check for a match- Returns:
- true if the request matches, false otherwise
-
setUseEquals
public void setUseEquals(boolean useEquals)
If set to true, matches on exactMediaType, else usesMediaType.isCompatibleWith(MediaType).- Parameters:
useEquals- specify if equals comparison should be used.
-
setIgnoredMediaTypes
public void setIgnoredMediaTypes(java.util.Set<org.springframework.http.MediaType> ignoredMediaTypes)
Set theMediaTypeto ignore from theContentNegotiationStrategy. This is useful if for example, you want to match onMediaType.APPLICATION_JSONbut want to ignoreMediaType.ALL.- Parameters:
ignoredMediaTypes- theMediaType's to ignore from theContentNegotiationStrategy
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-