Search in sources :

Example 16 with MediaTypeRequestMatcher

use of org.springframework.security.web.util.matcher.MediaTypeRequestMatcher in project spring-security by spring-projects.

the class OAuth2ResourceServerConfigurer method registerDefaultEntryPoint.

private void registerDefaultEntryPoint(H http) {
    ExceptionHandlingConfigurer<H> exceptionHandling = http.getConfigurer(ExceptionHandlingConfigurer.class);
    if (exceptionHandling != null) {
        ContentNegotiationStrategy contentNegotiationStrategy = http.getSharedObject(ContentNegotiationStrategy.class);
        if (contentNegotiationStrategy == null) {
            contentNegotiationStrategy = new HeaderContentNegotiationStrategy();
        }
        MediaTypeRequestMatcher restMatcher = new MediaTypeRequestMatcher(contentNegotiationStrategy, MediaType.APPLICATION_ATOM_XML, MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON, MediaType.APPLICATION_OCTET_STREAM, MediaType.APPLICATION_XML, MediaType.MULTIPART_FORM_DATA, MediaType.TEXT_XML);
        restMatcher.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL));
        MediaTypeRequestMatcher allMatcher = new MediaTypeRequestMatcher(contentNegotiationStrategy, MediaType.ALL);
        allMatcher.setUseEquals(true);
        RequestMatcher notHtmlMatcher = new NegatedRequestMatcher(new MediaTypeRequestMatcher(contentNegotiationStrategy, MediaType.TEXT_HTML));
        RequestMatcher restNotHtmlMatcher = new AndRequestMatcher(Arrays.<RequestMatcher>asList(notHtmlMatcher, restMatcher));
        RequestMatcher preferredMatcher = new OrRequestMatcher(Arrays.asList(this.requestMatcher, X_REQUESTED_WITH, restNotHtmlMatcher, allMatcher));
        exceptionHandling.defaultAuthenticationEntryPointFor(this.authenticationEntryPoint, preferredMatcher);
    }
}
Also used : NegatedRequestMatcher(org.springframework.security.web.util.matcher.NegatedRequestMatcher) AndRequestMatcher(org.springframework.security.web.util.matcher.AndRequestMatcher) NegatedRequestMatcher(org.springframework.security.web.util.matcher.NegatedRequestMatcher) RequestHeaderRequestMatcher(org.springframework.security.web.util.matcher.RequestHeaderRequestMatcher) RequestMatcher(org.springframework.security.web.util.matcher.RequestMatcher) OrRequestMatcher(org.springframework.security.web.util.matcher.OrRequestMatcher) MediaTypeRequestMatcher(org.springframework.security.web.util.matcher.MediaTypeRequestMatcher) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy) MediaTypeRequestMatcher(org.springframework.security.web.util.matcher.MediaTypeRequestMatcher) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy) ContentNegotiationStrategy(org.springframework.web.accept.ContentNegotiationStrategy) AndRequestMatcher(org.springframework.security.web.util.matcher.AndRequestMatcher) OrRequestMatcher(org.springframework.security.web.util.matcher.OrRequestMatcher)

Example 17 with MediaTypeRequestMatcher

use of org.springframework.security.web.util.matcher.MediaTypeRequestMatcher in project coffeenet-starter by coffeenet.

the class IntegrationCoffeeNetWebSecurityConfigurerAdapter method mediaTypeRequestMatcher.

private static MediaTypeRequestMatcher mediaTypeRequestMatcher(final ContentNegotiationStrategy contentNegotiationStrategy) {
    ContentNegotiationStrategy negotiationStrategy = contentNegotiationStrategy;
    if (negotiationStrategy == null) {
        negotiationStrategy = new HeaderContentNegotiationStrategy();
    }
    MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, APPLICATION_XHTML_XML, new MediaType("image", "*"), TEXT_HTML, TEXT_PLAIN);
    matcher.setIgnoredMediaTypes(singleton(ALL));
    return matcher;
}
Also used : MediaType(org.springframework.http.MediaType) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy) MediaTypeRequestMatcher(org.springframework.security.web.util.matcher.MediaTypeRequestMatcher) ContentNegotiationStrategy(org.springframework.web.accept.ContentNegotiationStrategy) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy)

Example 18 with MediaTypeRequestMatcher

use of org.springframework.security.web.util.matcher.MediaTypeRequestMatcher in project spring-security by spring-projects.

the class MediaTypeRequestMatcherTests method mediaAllAndTextHtmlIgnoreMediaTypeAll.

@Test
public void mediaAllAndTextHtmlIgnoreMediaTypeAll() throws HttpMediaTypeNotAcceptableException {
    when(negotiationStrategy.resolveMediaTypes(any(NativeWebRequest.class))).thenReturn(Arrays.asList(MediaType.ALL, MediaType.TEXT_HTML));
    matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.TEXT_HTML);
    matcher.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL));
    assertThat(matcher.matches(request)).isTrue();
}
Also used : MediaTypeRequestMatcher(org.springframework.security.web.util.matcher.MediaTypeRequestMatcher) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) Test(org.junit.Test)

Example 19 with MediaTypeRequestMatcher

use of org.springframework.security.web.util.matcher.MediaTypeRequestMatcher in project spring-security by spring-projects.

the class MediaTypeRequestMatcherRequestHCNSTests method javadocJsonJsonIgnoreAll.

@Test
public void javadocJsonJsonIgnoreAll() {
    request.addHeader("Accept", MediaType.APPLICATION_JSON_VALUE);
    MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON);
    matcher.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL));
    assertThat(matcher.matches(request)).isTrue();
}
Also used : MediaTypeRequestMatcher(org.springframework.security.web.util.matcher.MediaTypeRequestMatcher) Test(org.junit.Test)

Example 20 with MediaTypeRequestMatcher

use of org.springframework.security.web.util.matcher.MediaTypeRequestMatcher in project spring-security by spring-projects.

the class MediaTypeRequestMatcherRequestHCNSTests method javadocAllJsonIgnoreAll.

@Test
public void javadocAllJsonIgnoreAll() {
    request.addHeader("Accept", MediaType.ALL_VALUE);
    MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON);
    matcher.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL));
    assertThat(matcher.matches(request)).isFalse();
}
Also used : MediaTypeRequestMatcher(org.springframework.security.web.util.matcher.MediaTypeRequestMatcher) Test(org.junit.Test)

Aggregations

MediaTypeRequestMatcher (org.springframework.security.web.util.matcher.MediaTypeRequestMatcher)33 Test (org.junit.Test)21 ContentNegotiationStrategy (org.springframework.web.accept.ContentNegotiationStrategy)12 HeaderContentNegotiationStrategy (org.springframework.web.accept.HeaderContentNegotiationStrategy)12 MediaType (org.springframework.http.MediaType)11 NativeWebRequest (org.springframework.web.context.request.NativeWebRequest)11 NegatedRequestMatcher (org.springframework.security.web.util.matcher.NegatedRequestMatcher)7 RequestHeaderRequestMatcher (org.springframework.security.web.util.matcher.RequestHeaderRequestMatcher)7 AndRequestMatcher (org.springframework.security.web.util.matcher.AndRequestMatcher)6 RequestMatcher (org.springframework.security.web.util.matcher.RequestMatcher)6 HttpSecurity (org.springframework.security.config.annotation.web.builders.HttpSecurity)3 OrRequestMatcher (org.springframework.security.web.util.matcher.OrRequestMatcher)3 AntPathRequestMatcher (org.springframework.security.web.util.matcher.AntPathRequestMatcher)2 HttpStatusEntryPoint (org.springframework.security.web.authentication.HttpStatusEntryPoint)1 LoginUrlAuthenticationEntryPoint (org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint)1 BasicAuthenticationEntryPoint (org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint)1