Search in sources :

Example 66 with ServletRequestAttributes

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

the class RequestContextHolderTests method assertRequestAttributes.

private static void assertRequestAttributes(boolean withinMockMvc) {
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    assertThat(requestAttributes).isInstanceOf(ServletRequestAttributes.class);
    assertRequestAttributes(((ServletRequestAttributes) requestAttributes).getRequest(), withinMockMvc);
}
Also used : RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes)

Example 67 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project midpoint by Evolveum.

the class SecurityUtil method getCurrentConnectionInformation.

/**
 * Returns current connection information, as derived from HTTP request stored in current thread.
 * May be null if the thread is not associated with any HTTP request (e.g. task threads, operations invoked from GUI but executing in background).
 */
public static HttpConnectionInformation getCurrentConnectionInformation() {
    RequestAttributes attr = RequestContextHolder.getRequestAttributes();
    if (!(attr instanceof ServletRequestAttributes)) {
        return null;
    }
    ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) attr;
    HttpServletRequest request = servletRequestAttributes.getRequest();
    HttpConnectionInformation rv = new HttpConnectionInformation();
    HttpSession session = request.getSession(false);
    if (session != null) {
        rv.setSessionId(session.getId());
    }
    long start = System.currentTimeMillis();
    rv.setLocalHostName(request.getLocalName());
    long delta = System.currentTimeMillis() - start;
    if (delta > GET_LOCAL_NAME_THRESHOLD) {
        LOGGER.warn("getLocalName() on HTTP request took {} milliseconds that is too long; " + "please check your DNS configuration. Local name = {}, local address = {}", delta, request.getLocalName(), request.getLocalAddr());
    }
    rv.setRemoteHostAddress(getRemoteHostAddress(request));
    rv.setServerName(request.getServerName());
    return rv;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSession(javax.servlet.http.HttpSession) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Example 68 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project cas by apereo.

the class WebApplicationServiceFactoryTests method verifyServiceCreationSuccessfullyById.

@Test
public void verifyServiceCreationSuccessfullyById() {
    val request = new MockHttpServletRequest();
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request, new MockHttpServletResponse()));
    val factory = new WebApplicationServiceFactory();
    val service = factory.createService("testservice");
    assertNotNull(service);
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 69 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project cas by apereo.

the class SamlResponseArtifactEncoderTests method verifyOperation.

@Test
public void verifyOperation() throws Exception {
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    val registeredService = getSamlRegisteredServiceForTestShib();
    val authnRequest = getAuthnRequestFor(registeredService);
    val adaptor = SamlRegisteredServiceServiceProviderMetadataFacade.get(samlRegisteredServiceCachingMetadataResolver, registeredService, authnRequest).get();
    val encoder = new SamlResponseArtifactEncoder(velocityEngine, adaptor, request, response, samlArtifactMap);
    assertEquals(SAMLConstants.SAML2_ARTIFACT_BINDING_URI, encoder.getBinding());
    val tgt = new MockTicketGrantingTicket("casuser");
    ticketRegistry.addTicket(tgt);
    ticketGrantingTicketCookieGenerator.addCookie(response, tgt.getId());
    request.setCookies(response.getCookies());
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request, new MockHttpServletResponse()));
    val issuer = mock(Issuer.class);
    when(issuer.getValue()).thenReturn("cas");
    val samlResponse = mock(Response.class);
    when(samlResponse.getIssuer()).thenReturn(issuer);
    assertNotNull(encoder.encode(authnRequest, samlResponse, "relay-state", new MessageContext()));
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) MessageContext(org.opensaml.messaging.context.MessageContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 70 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project cas by apereo.

the class SamlProfileSaml2ResponseBuilderTests method verifySamlResponseWithAttributeQuery.

@Test
public void verifySamlResponseWithAttributeQuery() throws Exception {
    val request = buildHttpRequest();
    val response = new MockHttpServletResponse();
    val tgt = new MockTicketGrantingTicket("casuser");
    ticketRegistry.addTicket(tgt);
    val webContext = new JEEContext(request, response);
    samlIdPDistributedSessionStore.set(webContext, WebUtils.PARAMETER_TICKET_GRANTING_TICKET_ID, tgt.getId());
    val service = getSamlRegisteredServiceForTestShib(true, true);
    service.setIssuerEntityId("https://issuer.example.org");
    service.getAttributeValueTypes().put("permissions", XSObject.class.getSimpleName());
    val adaptor = SamlRegisteredServiceServiceProviderMetadataFacade.get(samlRegisteredServiceCachingMetadataResolver, service, service.getServiceId()).get();
    val authnRequest = getAuthnRequestFor(service);
    val assertion = getAssertion();
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request, response));
    val samlResponse = buildResponse(request, response, service, adaptor, authnRequest, assertion, SAMLConstants.SAML2_ARTIFACT_BINDING_URI);
    assertNotNull(samlResponse);
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) JEEContext(org.pac4j.core.context.JEEContext) XSObject(org.apache.xerces.xs.XSObject) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

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