Search in sources :

Example 16 with DefaultResponseErrorHandler

use of org.springframework.web.client.DefaultResponseErrorHandler in project spring-security-oauth by spring-projects.

the class ClientCredentialsProviderTests method testInvalidCredentialsWithFormAuthentication.

@Test
@OAuth2ContextConfiguration(resource = InvalidClientCredentials.class, initialize = false)
public void testInvalidCredentialsWithFormAuthentication() throws Exception {
    context.setAccessTokenProvider(new ClientCredentialsAccessTokenProvider() {

        @Override
        protected ResponseErrorHandler getResponseErrorHandler() {
            return new DefaultResponseErrorHandler() {

                public void handleError(ClientHttpResponse response) throws IOException {
                    responseHeaders = response.getHeaders();
                    responseStatus = response.getStatusCode();
                }
            };
        }
    });
    try {
        context.getAccessToken();
        fail("Expected ResourceAccessException");
    } catch (Exception e) {
    // ignore
    }
    // System.err.println(responseHeaders);
    String header = responseHeaders.getFirst("WWW-Authenticate");
    assertTrue("Wrong header: " + header, header.contains("Form realm"));
    assertEquals(HttpStatus.UNAUTHORIZED, responseStatus);
}
Also used : DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) ResponseErrorHandler(org.springframework.web.client.ResponseErrorHandler) IOException(java.io.IOException) ClientCredentialsAccessTokenProvider(org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) IOException(java.io.IOException) OAuth2ContextConfiguration(org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration) Test(org.junit.Test)

Example 17 with DefaultResponseErrorHandler

use of org.springframework.web.client.DefaultResponseErrorHandler in project apollo by ctripcorp.

the class AbstractControllerTest method postConstruct.

@PostConstruct
protected void postConstruct() {
    restTemplate.setErrorHandler(new DefaultResponseErrorHandler());
    restTemplate.setMessageConverters(httpMessageConverters.getConverters());
}
Also used : DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) PostConstruct(javax.annotation.PostConstruct)

Example 18 with DefaultResponseErrorHandler

use of org.springframework.web.client.DefaultResponseErrorHandler in project apollo by ctripcorp.

the class AbstractIntegrationTest method postConstruct.

@PostConstruct
private void postConstruct() {
    System.setProperty("spring.profiles.active", "test");
    restTemplate.setErrorHandler(new DefaultResponseErrorHandler());
}
Also used : DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) PostConstruct(javax.annotation.PostConstruct)

Example 19 with DefaultResponseErrorHandler

use of org.springframework.web.client.DefaultResponseErrorHandler in project webofneeds by researchstudio-sat.

the class LinkedDataRestBridge method createRestTemplateForReadingLinkedData.

private RestTemplate createRestTemplateForReadingLinkedData(String webID) throws Exception {
    String privateKeyAlias = keyPairAliasDerivationStrategy.getAliasForAtomUri(webID);
    RestTemplate template = CryptographyUtils.createSslRestTemplate(privateKeyAlias, this.keyStoreService.getUnderlyingKeyStore(), this.keyStoreService.getPassword(), this.trustStoreService.getUnderlyingKeyStore(), this.trustStrategy, readTimeout, connectionTimeout, true);
    // prevent the RestTemplate from throwing an exception when the server responds
    // with 4xx or 5xx status
    // because we want to hand the orginal response back to the original caller in
    // BridgeForLinkedDataController
    template.setErrorHandler(new DefaultResponseErrorHandler() {

        @Override
        protected boolean hasError(final HttpStatus statusCode) {
            return false;
        }
    });
    return template;
}
Also used : DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) HttpStatus(org.springframework.http.HttpStatus) RestTemplate(org.springframework.web.client.RestTemplate)

Example 20 with DefaultResponseErrorHandler

use of org.springframework.web.client.DefaultResponseErrorHandler in project OsmAnd-tools by osmandapp.

the class WebSecurityConfiguration method oauthGithubUserService.

private DefaultOAuth2UserService oauthGithubUserService() {
    // authorize with admin for specific group
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {

        @Override
        public void handleError(ClientHttpResponse response) throws IOException {
        }
    });
    DefaultOAuth2UserService service = new DefaultOAuth2UserService() {

        @Override
        public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
            OAuth2User user = super.loadUser(userRequest);
            if (user == null) {
                return null;
            }
            Set<GrantedAuthority> authorities = new LinkedHashSet<>();
            if (!Algorithms.isEmpty(adminOauth2Url) && user.getAttribute("url") != null && user.getAttribute("url").toString().contains("github.com")) {
                Map<String, Object> orgs = checkPermissionAccess(adminOauth2Url, userRequest, user);
                // orgs.get("privacy").equals("closed");
                if (orgs != null) {
                    authorities.add(new SimpleGrantedAuthority(ROLE_ADMIN));
                }
            }
            String userNameAttributeName = userRequest.getClientRegistration().getProviderDetails().getUserInfoEndpoint().getUserNameAttributeName();
            return new DefaultOAuth2User(authorities, user.getAttributes(), userNameAttributeName);
        }

        private Map<String, Object> checkPermissionAccess(Object orgUrl, OAuth2UserRequest userRequest, OAuth2User user) {
            String organizationUrl = String.valueOf(orgUrl);
            HttpHeaders headers = new HttpHeaders();
            headers.setBearerAuth(userRequest.getAccessToken().getTokenValue());
            headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
            URI uri = UriComponentsBuilder.fromUriString(organizationUrl).build().toUri();
            RequestEntity<?> request = new RequestEntity<>(headers, HttpMethod.GET, uri);
            ResponseEntity<Map<String, Object>> res = restTemplate.exchange(request, new ParameterizedTypeReference<Map<String, Object>>() {
            });
            if (!res.getStatusCode().is2xxSuccessful()) {
                return null;
            }
            return res.getBody();
        }
    };
    return service;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DefaultOAuth2User(org.springframework.security.oauth2.core.user.DefaultOAuth2User) OAuth2User(org.springframework.security.oauth2.core.user.OAuth2User) HttpHeaders(org.springframework.http.HttpHeaders) DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) GrantedAuthority(org.springframework.security.core.GrantedAuthority) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) OAuth2UserRequest(org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest) IOException(java.io.IOException) DefaultOAuth2UserService(org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService) URI(java.net.URI) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) RestTemplate(org.springframework.web.client.RestTemplate) DefaultOAuth2User(org.springframework.security.oauth2.core.user.DefaultOAuth2User) RequestEntity(org.springframework.http.RequestEntity) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Map(java.util.Map)

Aggregations

DefaultResponseErrorHandler (org.springframework.web.client.DefaultResponseErrorHandler)20 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)16 IOException (java.io.IOException)11 ResponseErrorHandler (org.springframework.web.client.ResponseErrorHandler)10 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)6 ResponseExtractor (org.springframework.web.client.ResponseExtractor)6 BeforeOAuth2Context (org.springframework.security.oauth2.client.test.BeforeOAuth2Context)5 RestTemplate (org.springframework.web.client.RestTemplate)5 Test (org.junit.Test)4 ClientCredentialsAccessTokenProvider (org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider)4 PostConstruct (javax.annotation.PostConstruct)3 ResponseEntity (org.springframework.http.ResponseEntity)3 OAuth2ContextConfiguration (org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration)3 AuthorizationCodeAccessTokenProvider (org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider)3 ResourceOwnerPasswordAccessTokenProvider (org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordAccessTokenProvider)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 URI (java.net.URI)2 HttpHeaders (org.springframework.http.HttpHeaders)2 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)2 ResourceAccessException (org.springframework.web.client.ResourceAccessException)2