Search in sources :

Example 46 with FilterChain

use of jakarta.servlet.FilterChain in project spring-security by spring-projects.

the class SwitchUserFilterTests method redirectToTargetUrlIsCorrect.

@Test
public void redirectToTargetUrlIsCorrect() throws Exception {
    MockHttpServletRequest request = createMockSwitchRequest();
    request.setContextPath("/webapp");
    request.addParameter(SwitchUserFilter.SPRING_SECURITY_SWITCH_USERNAME_KEY, "jacklord");
    request.setRequestURI("/webapp/login/impersonate");
    SwitchUserFilter filter = new SwitchUserFilter();
    filter.setSwitchUserUrl("/login/impersonate");
    filter.setSuccessHandler(new SimpleUrlAuthenticationSuccessHandler("/someOtherUrl"));
    filter.setUserDetailsService(new MockUserDetailsService());
    FilterChain chain = mock(FilterChain.class);
    MockHttpServletResponse response = new MockHttpServletResponse();
    filter.doFilter(request, response, chain);
    verify(chain, never()).doFilter(request, response);
    assertThat(response.getRedirectedUrl()).isEqualTo("/webapp/someOtherUrl");
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(jakarta.servlet.FilterChain) SimpleUrlAuthenticationSuccessHandler(org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 47 with FilterChain

use of jakarta.servlet.FilterChain in project spring-security by spring-projects.

the class ConcurrentSessionFilterTests method returnsExpectedMessageWhenNoExpiredUrlSet.

// As above, but with no expiredUrl set.
@Test
public void returnsExpectedMessageWhenNoExpiredUrlSet() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpSession session = new MockHttpSession();
    request.setSession(session);
    MockHttpServletResponse response = new MockHttpServletResponse();
    SessionRegistry registry = new SessionRegistryImpl();
    registry.registerNewSession(session.getId(), "principal");
    registry.getSessionInformation(session.getId()).expireNow();
    ConcurrentSessionFilter filter = new ConcurrentSessionFilter(registry);
    FilterChain fc = mock(FilterChain.class);
    filter.doFilter(request, response, fc);
    verifyZeroInteractions(fc);
    assertThat(response.getContentAsString()).isEqualTo("This session has been expired (possibly due to multiple concurrent logins being " + "attempted as the same user).");
}
Also used : SessionRegistry(org.springframework.security.core.session.SessionRegistry) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) SessionRegistryImpl(org.springframework.security.core.session.SessionRegistryImpl) MockFilterChain(org.springframework.mock.web.MockFilterChain) FilterChain(jakarta.servlet.FilterChain) MockHttpSession(org.springframework.mock.web.MockHttpSession) ConcurrentSessionFilter(org.springframework.security.web.session.ConcurrentSessionFilter) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 48 with FilterChain

use of jakarta.servlet.FilterChain in project tomcat by apache.

the class Request method getNonAsyncClassNames.

private Set<String> getNonAsyncClassNames() {
    Set<String> result = new HashSet<>();
    Wrapper wrapper = getWrapper();
    if (!wrapper.isAsyncSupported()) {
        result.add(wrapper.getServletClass());
    }
    FilterChain filterChain = getFilterChain();
    if (filterChain instanceof ApplicationFilterChain) {
        ((ApplicationFilterChain) filterChain).findNonAsyncFilters(result);
    } else {
        result.add(sm.getString("coyoteRequest.filterAsyncSupportUnknown"));
    }
    Container c = wrapper;
    while (c != null) {
        c.getPipeline().findNonAsyncValves(result);
        c = c.getParent();
    }
    return result;
}
Also used : HttpServletRequestWrapper(jakarta.servlet.http.HttpServletRequestWrapper) Wrapper(org.apache.catalina.Wrapper) Container(org.apache.catalina.Container) ApplicationFilterChain(org.apache.catalina.core.ApplicationFilterChain) FilterChain(jakarta.servlet.FilterChain) ApplicationFilterChain(org.apache.catalina.core.ApplicationFilterChain) HashSet(java.util.HashSet)

Example 49 with FilterChain

use of jakarta.servlet.FilterChain in project spring-framework by spring-projects.

the class ServletInvocableHandlerMethodTests method invokeAndHandle_VoidNotModifiedWithEtag.

@Test
public void invokeAndHandle_VoidNotModifiedWithEtag() throws Exception {
    String eTagValue = "\"deadb33f8badf00d\"";
    FilterChain chain = (req, res) -> {
        request.addHeader(HttpHeaders.IF_NONE_MATCH, eTagValue);
        webRequest.checkNotModified(eTagValue);
        try {
            ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new Handler(), "notModified");
            handlerMethod.invokeAndHandle(webRequest, mavContainer);
        } catch (Exception ex) {
            throw new IllegalStateException(ex);
        }
    };
    new ShallowEtagHeaderFilter().doFilter(this.request, this.response, chain);
    assertThat(response.getStatus()).isEqualTo(304);
    assertThat(response.getHeader(HttpHeaders.ETAG)).isEqualTo(eTagValue);
    assertThat(response.getContentAsString()).isEmpty();
}
Also used : LocaleContextHolder(org.springframework.context.i18n.LocaleContextHolder) Arrays(java.util.Arrays) RequestParam(org.springframework.web.bind.annotation.RequestParam) HandlerMethodReturnValueHandlerComposite(org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite) RequestParamMethodArgumentResolver(org.springframework.web.method.annotation.RequestParamMethodArgumentResolver) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DeferredResult(org.springframework.web.context.request.async.DeferredResult) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) Retention(java.lang.annotation.Retention) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) HandlerMethod(org.springframework.web.method.HandlerMethod) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) RedirectView(org.springframework.web.servlet.view.RedirectView) Locale(java.util.Locale) MethodParameter(org.springframework.core.MethodParameter) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) HandlerMethodArgumentResolverComposite(org.springframework.web.method.support.HandlerMethodArgumentResolverComposite) ResolvableType(org.springframework.core.ResolvableType) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) Method(java.lang.reflect.Method) ShallowEtagHeaderFilter(org.springframework.web.filter.ShallowEtagHeaderFilter) AliasFor(org.springframework.core.annotation.AliasFor) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpHeaders(org.springframework.http.HttpHeaders) FilterChain(jakarta.servlet.FilterChain) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ResolvableMethod(org.springframework.web.testfixture.method.ResolvableMethod) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) ResponseEntity(org.springframework.http.ResponseEntity) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Collections(java.util.Collections) RetentionPolicy(java.lang.annotation.RetentionPolicy) HandlerMethodReturnValueHandler(org.springframework.web.method.support.HandlerMethodReturnValueHandler) FilterChain(jakarta.servlet.FilterChain) HandlerMethodReturnValueHandler(org.springframework.web.method.support.HandlerMethodReturnValueHandler) ShallowEtagHeaderFilter(org.springframework.web.filter.ShallowEtagHeaderFilter) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) Test(org.junit.jupiter.api.Test)

Example 50 with FilterChain

use of jakarta.servlet.FilterChain in project spring-framework by spring-projects.

the class ResourceUrlEncodingFilterTests method testEncodeUrl.

private void testEncodeUrl(MockHttpServletRequest request, String url, String expected) throws ServletException, IOException {
    FilterChain chain = (req, res) -> {
        req.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, this.urlProvider);
        String result = ((HttpServletResponse) res).encodeURL(url);
        assertThat(result).isEqualTo(expected);
    };
    this.filter.doFilter(request, new MockHttpServletResponse(), chain);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FilterChain(jakarta.servlet.FilterChain) ClassPathResource(org.springframework.core.io.ClassPathResource) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) List(java.util.List) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Collections(java.util.Collections) FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse)

Aggregations

FilterChain (jakarta.servlet.FilterChain)141 Test (org.junit.jupiter.api.Test)134 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)103 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)102 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)68 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)54 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)35 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)32 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)29 ServletRequest (jakarta.servlet.ServletRequest)25 ServletResponse (jakarta.servlet.ServletResponse)25 Authentication (org.springframework.security.core.Authentication)23 MockFilterChain (org.springframework.mock.web.MockFilterChain)20 ServletException (jakarta.servlet.ServletException)16 StandardCharsets (java.nio.charset.StandardCharsets)16 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)16 IOException (java.io.IOException)15 BeforeEach (org.junit.jupiter.api.BeforeEach)14 FileCopyUtils (org.springframework.util.FileCopyUtils)14 Arrays (java.util.Arrays)11