Search in sources :

Example 81 with ServletRequestAttributes

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

the class OAuth20AuthenticationServiceSelectionStrategyTests method verifyGrantType.

@Test
public void verifyGrantType() {
    val request = new MockHttpServletRequest();
    request.addHeader("X-" + CasProtocolConstants.PARAMETER_SERVICE, RegisteredServiceTestUtils.CONST_TEST_URL2);
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request, new MockHttpServletResponse()));
    val service = strategy.resolveServiceFrom(RegisteredServiceTestUtils.getService("https://example.org?" + OAuth20Constants.CLIENT_ID + '=' + CLIENT_ID + '&' + OAuth20Constants.GRANT_TYPE + '=' + OAuth20GrantTypes.CLIENT_CREDENTIALS.getType()));
    assertNotNull(service);
    assertTrue(service.getAttributes().containsKey(OAuth20Constants.CLIENT_ID));
    assertTrue(service.getAttributes().containsKey(OAuth20Constants.GRANT_TYPE));
    assertEquals(Ordered.HIGHEST_PRECEDENCE, strategy.getOrder());
}
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 82 with ServletRequestAttributes

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

the class OAuth20ServicesManagerRegisteredServiceLocatorTests method verifyWithCallback.

@Test
public void verifyWithCallback() throws Exception {
    val callbackUrl = "http://localhost:8443/cas" + OAuth20Constants.BASE_OAUTH20_URL + '/' + OAuth20Constants.CALLBACK_AUTHORIZE_URL;
    val service0 = RegisteredServiceTestUtils.getRegisteredService(callbackUrl + ".*");
    service0.setEvaluationOrder(0);
    val service1 = getRegisteredService("http://localhost:8080/app1", "application1", "secret1");
    service1.setEvaluationOrder(100);
    val service2 = getRegisteredService(".+", "application2", "secret2");
    service2.setEvaluationOrder(1000);
    val candidateServices = CollectionUtils.wrapList(service0, service1, service2);
    servicesManager.save(candidateServices.toArray(new RegisteredService[0]));
    Collections.sort(candidateServices);
    val url = new URIBuilder(callbackUrl + '?' + OAuth20Constants.CLIENT_ID + "=application1");
    val request = new MockHttpServletRequest();
    request.setRequestURI(callbackUrl);
    url.getQueryParams().forEach(param -> request.addParameter(param.getName(), param.getValue()));
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request, new MockHttpServletResponse()));
    val service = serviceFactory.createService(url.toString());
    val result = servicesManager.findServiceBy(service);
    assertEquals(service1, result);
}
Also used : lombok.val(lombok.val) RegisteredService(org.apereo.cas.services.RegisteredService) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) URIBuilder(org.apache.http.client.utils.URIBuilder) Test(org.junit.jupiter.api.Test)

Example 83 with ServletRequestAttributes

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

the class AopNamespaceHandlerScopeIntegrationTests method testSessionScoping.

@Test
void testSessionScoping() throws Exception {
    MockHttpSession oldSession = new MockHttpSession();
    MockHttpSession newSession = new MockHttpSession();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(oldSession);
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
    assertThat(AopUtils.isAopProxy(sessionScoped)).as("Should be AOP proxy").isTrue();
    boolean condition1 = sessionScoped instanceof TestBean;
    assertThat(condition1).as("Should not be target class proxy").isFalse();
    assertThat(sessionScopedAlias).isSameAs(sessionScoped);
    assertThat(AopUtils.isAopProxy(testBean)).as("Should be AOP proxy").isTrue();
    boolean condition = testBean instanceof TestBean;
    assertThat(condition).as("Regular bean should be JDK proxy").isFalse();
    String rob = "Rob Harrop";
    String bram = "Bram Smeets";
    assertThat(sessionScoped.getName()).isEqualTo(rob);
    sessionScoped.setName(bram);
    request.setSession(newSession);
    assertThat(sessionScoped.getName()).isEqualTo(rob);
    request.setSession(oldSession);
    assertThat(sessionScoped.getName()).isEqualTo(bram);
    assertThat(((Advised) sessionScoped).getAdvisors().length > 0).as("Should have advisors").isTrue();
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) MockHttpSession(org.springframework.mock.web.MockHttpSession) Test(org.junit.jupiter.api.Test)

Example 84 with ServletRequestAttributes

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

the class ClassPathBeanDefinitionScannerScopeIntegrationTests method setup.

@BeforeEach
void setup() {
    MockHttpServletRequest oldRequestWithSession = new MockHttpServletRequest();
    oldRequestWithSession.setSession(new MockHttpSession());
    this.oldRequestAttributesWithSession = new ServletRequestAttributes(oldRequestWithSession);
    MockHttpServletRequest newRequestWithSession = new MockHttpServletRequest();
    newRequestWithSession.setSession(new MockHttpSession());
    this.newRequestAttributesWithSession = new ServletRequestAttributes(newRequestWithSession);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) MockHttpSession(org.springframework.mock.web.MockHttpSession) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 85 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project ontrack by nemerosa.

the class DefaultURIBuilder method build.

@Override
public URI build(Object methodInvocation) {
    // Default builder
    UriComponentsBuilder builder = MvcUriComponentsBuilder.fromMethodCall(methodInvocation);
    // Default URI
    UriComponents uriComponents = builder.build();
    // TODO #251 Workaround for SPR-12771
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    String portHeader = httpRequest.getHeaders().getFirst("X-Forwarded-Port");
    if (StringUtils.hasText(portHeader)) {
        int port = Integer.parseInt(portHeader);
        String scheme = uriComponents.getScheme();
        if (("https".equals(scheme) && port == 443) || ("http".equals(scheme) && port == 80)) {
            port = -1;
        }
        builder.port(port);
    }
    // OK
    return builder.build().toUri();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpRequest(org.springframework.http.HttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) UriComponents(org.springframework.web.util.UriComponents) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) ServletUriComponentsBuilder(org.springframework.web.servlet.support.ServletUriComponentsBuilder) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes)

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