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