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);
}
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;
}
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);
}
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()));
}
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);
}
Aggregations