Search in sources :

Example 76 with ServletRequestAttributes

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

the class RestfulUrlTemplateResolverTests method verifyAction.

@Test
public void verifyAction() {
    val request = new MockHttpServletRequest();
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request, new MockHttpServletResponse()));
    try (val webServer = new MockWebServer(9302, new ByteArrayResource("template".getBytes(StandardCharsets.UTF_8), "REST Output"), MediaType.APPLICATION_JSON_VALUE)) {
        webServer.start();
        val props = new CasConfigurationProperties();
        props.getView().getRest().setUrl("http://localhost:9302");
        var themeResolver = new FixedThemeResolver();
        themeResolver.setDefaultThemeName("sample-theme");
        val r = new RestfulUrlTemplateResolver(props, themeResolver);
        val res = r.resolveTemplate(mock(IEngineConfiguration.class), "cas", "template", new LinkedHashMap<>());
        assertNotNull(res);
    }
}
Also used : lombok.val(lombok.val) IEngineConfiguration(org.thymeleaf.IEngineConfiguration) FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) MockWebServer(org.apereo.cas.util.MockWebServer) ByteArrayResource(org.springframework.core.io.ByteArrayResource) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 77 with ServletRequestAttributes

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

the class ThemeBasedViewResolver method resolveViewName.

@Override
public View resolveViewName(final String viewName, final Locale locale) {
    try {
        val theme = Optional.of(RequestContextHolder.currentRequestAttributes()).filter(ServletRequestAttributes.class::isInstance).map(ServletRequestAttributes.class::cast).map(ServletRequestAttributes::getRequest).map(themeResolver::resolveThemeName);
        val delegate = theme.map(this::getViewResolver);
        if (delegate.isPresent()) {
            return delegate.get().resolveViewName(viewName, locale);
        }
    } catch (final Exception e) {
        LOGGER.debug("error resolving view [{}] for theme", viewName, e);
    }
    return null;
}
Also used : lombok.val(lombok.val) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes)

Example 78 with ServletRequestAttributes

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

the class OidcServicesManagerRegisteredServiceLocatorTests 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 = getOidcRegisteredService("application1");
    service1.setEvaluationOrder(100);
    val service2 = getOidcRegisteredService("application-catch-all", ".*");
    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 = webApplicationServiceFactory.createService(url.toString());
    val result = servicesManager.findServiceBy(service);
    assertEquals(result, service1);
}
Also used : lombok.val(lombok.val) RegexRegisteredService(org.apereo.cas.services.RegexRegisteredService) RegisteredService(org.apereo.cas.services.RegisteredService) OidcRegisteredService(org.apereo.cas.services.OidcRegisteredService) 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 79 with ServletRequestAttributes

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

the class WsFederationServicesManagerRegisteredServiceLocatorTests method verifyWithCallback.

@Test
public void verifyWithCallback() throws Exception {
    val callbackUrl = "http://localhost:8443/cas" + WSFederationConstants.ENDPOINT_FEDERATION_REQUEST_CALLBACK;
    val service0 = RegisteredServiceTestUtils.getRegisteredService(callbackUrl + ".*");
    service0.setEvaluationOrder(0);
    val service1 = getWsFederationRegisteredService("application1", "CAS");
    service1.setEvaluationOrder(100);
    val service2 = getWsFederationRegisteredService(".*", "CAS");
    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 + '?' + WSFederationConstants.WTREALM + "=CAS&" + WSFederationConstants.WREPLY + "=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 = webApplicationServiceFactory.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 80 with ServletRequestAttributes

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

the class OAuth20AuthenticationServiceSelectionStrategyTests method verifyJwtRequest.

@Test
public void verifyJwtRequest() {
    val claims = new JWTClaimsSet.Builder().subject("cas").claim("scope", new String[] { "profile" }).claim("redirect_uri", REDIRECT_URI).claim("grant_type", OAuth20GrantTypes.CLIENT_CREDENTIALS.getType()).claim("client_id", CLIENT_ID).build();
    val jwt = new PlainJWT(claims);
    val jwtRequest = jwt.serialize();
    val request = new MockHttpServletRequest();
    request.addParameter(OAuth20Constants.REQUEST, jwtRequest);
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request, new MockHttpServletResponse()));
    val service = strategy.resolveServiceFrom(RegisteredServiceTestUtils.getService("https://example.org?" + OAuth20Constants.REQUEST + '=' + jwtRequest));
    assertNotNull(service);
    assertTrue(service.getAttributes().containsKey(OAuth20Constants.CLIENT_ID));
}
Also used : lombok.val(lombok.val) PlainJWT(com.nimbusds.jwt.PlainJWT) 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)

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