Search in sources :

Example 56 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project skeleton-commons by skeleton-software-community.

the class TokenFromCookieExtractor method extractToken.

@Override
public String extractToken(String key) {
    String result = tokenFromHeaderExtractor.extractToken(key);
    if (result != null) {
        return result;
    }
    ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    Cookie[] cookies = servletRequestAttributes.getRequest().getCookies();
    for (Cookie cookie : cookies) {
        if (cookie.getName().equals(key)) {
            result = cookie.getValue();
            break;
        }
    }
    return result;
}
Also used : Cookie(javax.servlet.http.Cookie) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes)

Example 57 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project spring-security by spring-projects.

the class ServletOAuth2AuthorizedClientExchangeFilterFunction method populateDefaultRequestResponse.

private void populateDefaultRequestResponse(Map<String, Object> attrs) {
    if (attrs.containsKey(HTTP_SERVLET_REQUEST_ATTR_NAME) && attrs.containsKey(HTTP_SERVLET_RESPONSE_ATTR_NAME)) {
        return;
    }
    RequestAttributes context = RequestContextHolder.getRequestAttributes();
    if (context instanceof ServletRequestAttributes) {
        attrs.putIfAbsent(HTTP_SERVLET_REQUEST_ATTR_NAME, ((ServletRequestAttributes) context).getRequest());
        attrs.putIfAbsent(HTTP_SERVLET_RESPONSE_ATTR_NAME, ((ServletRequestAttributes) context).getResponse());
    }
}
Also used : ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes)

Example 58 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project spring-security by spring-projects.

the class SecurityReactorContextConfigurationTests method createSubscriberIfNecessaryWhenWebSecurityContextAvailableThenCreateWithParentContext.

@Test
public void createSubscriberIfNecessaryWhenWebSecurityContextAvailableThenCreateWithParentContext() {
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(this.servletRequest, this.servletResponse));
    SecurityContextHolder.getContext().setAuthentication(this.authentication);
    String testKey = "test_key";
    String testValue = "test_value";
    BaseSubscriber<Object> parent = new BaseSubscriber<Object>() {

        @Override
        public Context currentContext() {
            return Context.of(testKey, testValue);
        }
    };
    CoreSubscriber<Object> subscriber = this.subscriberRegistrar.createSubscriberIfNecessary(parent);
    Context resultContext = subscriber.currentContext();
    assertThat(resultContext.getOrEmpty(testKey)).hasValue(testValue);
    Map<Object, Object> securityContextAttributes = resultContext.getOrDefault(SecurityReactorContextSubscriber.SECURITY_CONTEXT_ATTRIBUTES, null);
    assertThat(securityContextAttributes).hasSize(3);
    assertThat(securityContextAttributes).contains(entry(HttpServletRequest.class, this.servletRequest), entry(HttpServletResponse.class, this.servletResponse), entry(Authentication.class, this.authentication));
}
Also used : Context(reactor.util.context.Context) SpringTestContext(org.springframework.security.config.test.SpringTestContext) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) BaseSubscriber(reactor.core.publisher.BaseSubscriber) Authentication(org.springframework.security.core.Authentication) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 59 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project spring-framework by spring-projects.

the class MvcUriComponentsBuilderTests method adaptRequestFromForwardedHeaders.

// SPR-16668
private void adaptRequestFromForwardedHeaders() throws Exception {
    MockFilterChain chain = new MockFilterChain();
    new ForwardedHeaderFilter().doFilter(this.request, new MockHttpServletResponse(), chain);
    HttpServletRequest adaptedRequest = (HttpServletRequest) chain.getRequest();
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(adaptedRequest));
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ForwardedHeaderFilter(org.springframework.web.filter.ForwardedHeaderFilter) MockFilterChain(org.springframework.web.testfixture.servlet.MockFilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse)

Example 60 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project spring-framework by spring-projects.

the class RequestContextFilter method doFilterInternal.

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    ServletRequestAttributes attributes = new ServletRequestAttributes(request, response);
    initContextHolders(request, attributes);
    try {
        filterChain.doFilter(request, response);
    } finally {
        resetContextHolders();
        if (logger.isTraceEnabled()) {
            logger.trace("Cleared thread-bound request context: " + request);
        }
        attributes.requestCompleted();
    }
}
Also used : ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes)

Aggregations

ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)204 HttpServletRequest (javax.servlet.http.HttpServletRequest)92 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)62 RequestAttributes (org.springframework.web.context.request.RequestAttributes)50 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)32 Test (org.junit.jupiter.api.Test)28 lombok.val (lombok.val)25 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)22 Test (org.junit.Test)18 AuthChecker (com.code.server.login.anotation.AuthChecker)17 HttpSession (javax.servlet.http.HttpSession)12 Before (org.junit.Before)12 HashMap (java.util.HashMap)11 Method (java.lang.reflect.Method)8 Date (java.util.Date)7 AbstractTracingSpan (org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan)7 TraceSegment (org.apache.skywalking.apm.agent.core.context.trace.TraceSegment)7 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)6 Map (java.util.Map)6 BeforeEach (org.junit.jupiter.api.BeforeEach)6