Search in sources :

Example 1 with Builder

use of org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest.Builder in project service-authorization by reportportal.

the class OAuthSecurityConfig method configure.

@Override
protected final void configure(HttpSecurity http) throws Exception {
    // @formatter:off
    http.antMatcher("/**").authorizeRequests().antMatchers(SSO_LOGIN_PATH + "/**", "/webjars/**", "/index.html", "/epam/**", "/info", "/health", "/api-docs/**").permitAll().anyRequest().authenticated().and().csrf().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    CompositeFilter authCompositeFilter = new CompositeFilter();
    List<OAuth2ClientAuthenticationProcessingFilter> additionalFilters = ImmutableList.<OAuth2ClientAuthenticationProcessingFilter>builder().addAll(getDefaultFilters(oauth2ClientContext)).addAll(getAdditionalFilters(oauth2ClientContext)).build();
    /* make sure filters have correct exception handler */
    additionalFilters.forEach(filter -> filter.setAuthenticationFailureHandler(OAUTH_ERROR_HANDLER));
    authCompositeFilter.setFilters(additionalFilters);
    // install additional OAuth Authentication filters
    http.addFilterAfter(authCompositeFilter, BasicAuthenticationFilter.class);
// @formatter:on
}
Also used : CompositeFilter(org.springframework.web.filter.CompositeFilter) OAuth2ClientAuthenticationProcessingFilter(org.springframework.security.oauth2.client.filter.OAuth2ClientAuthenticationProcessingFilter)

Example 2 with Builder

use of org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest.Builder in project spring-security by spring-projects.

the class OAuth2ClientConfigurerUtils method getAuthorizedClientRepository.

static <B extends HttpSecurityBuilder<B>> OAuth2AuthorizedClientRepository getAuthorizedClientRepository(B builder) {
    OAuth2AuthorizedClientRepository authorizedClientRepository = builder.getSharedObject(OAuth2AuthorizedClientRepository.class);
    if (authorizedClientRepository == null) {
        authorizedClientRepository = getAuthorizedClientRepositoryBean(builder);
        if (authorizedClientRepository == null) {
            authorizedClientRepository = new AuthenticatedPrincipalOAuth2AuthorizedClientRepository(getAuthorizedClientService((builder)));
        }
        builder.setSharedObject(OAuth2AuthorizedClientRepository.class, authorizedClientRepository);
    }
    return authorizedClientRepository;
}
Also used : AuthenticatedPrincipalOAuth2AuthorizedClientRepository(org.springframework.security.oauth2.client.web.AuthenticatedPrincipalOAuth2AuthorizedClientRepository) OAuth2AuthorizedClientRepository(org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository) AuthenticatedPrincipalOAuth2AuthorizedClientRepository(org.springframework.security.oauth2.client.web.AuthenticatedPrincipalOAuth2AuthorizedClientRepository)

Example 3 with Builder

use of org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest.Builder in project spring-security by spring-projects.

the class OAuth2ClientConfigurerUtils method getClientRegistrationRepository.

static <B extends HttpSecurityBuilder<B>> ClientRegistrationRepository getClientRegistrationRepository(B builder) {
    ClientRegistrationRepository clientRegistrationRepository = builder.getSharedObject(ClientRegistrationRepository.class);
    if (clientRegistrationRepository == null) {
        clientRegistrationRepository = getClientRegistrationRepositoryBean(builder);
        builder.setSharedObject(ClientRegistrationRepository.class, clientRegistrationRepository);
    }
    return clientRegistrationRepository;
}
Also used : ClientRegistrationRepository(org.springframework.security.oauth2.client.registration.ClientRegistrationRepository)

Example 4 with Builder

use of org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest.Builder in project spring-security by spring-projects.

the class ServletOAuth2AuthorizedClientExchangeFilterFunction method authorizeClient.

private Mono<OAuth2AuthorizedClient> authorizeClient(String clientRegistrationId, ClientRequest request) {
    if (this.authorizedClientManager == null) {
        return Mono.empty();
    }
    Map<String, Object> attrs = request.attributes();
    Authentication authentication = getAuthentication(attrs);
    if (authentication == null) {
        authentication = ANONYMOUS_AUTHENTICATION;
    }
    HttpServletRequest servletRequest = getRequest(attrs);
    HttpServletResponse servletResponse = getResponse(attrs);
    OAuth2AuthorizeRequest.Builder builder = OAuth2AuthorizeRequest.withClientRegistrationId(clientRegistrationId).principal(authentication);
    builder.attributes((attributes) -> addToAttributes(attributes, servletRequest, servletResponse));
    OAuth2AuthorizeRequest authorizeRequest = builder.build();
    // blocking I/O operation using RestTemplate internally
    return Mono.fromSupplier(() -> this.authorizedClientManager.authorize(authorizeRequest)).subscribeOn(Schedulers.boundedElastic());
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) Authentication(org.springframework.security.core.Authentication) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) OAuth2AuthorizeRequest(org.springframework.security.oauth2.client.OAuth2AuthorizeRequest)

Example 5 with Builder

use of org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest.Builder in project spring-security by spring-projects.

the class JwtAuthenticationTokenTests method getNameWhenJwtHasSubjectThenReturnsSubject.

@Test
public void getNameWhenJwtHasSubjectThenReturnsSubject() {
    Jwt jwt = builder().subject("Carl").build();
    JwtAuthenticationToken token = new JwtAuthenticationToken(jwt);
    assertThat(token.getName()).isEqualTo("Carl");
}
Also used : Jwt(org.springframework.security.oauth2.jwt.Jwt) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)9 Jwt (org.springframework.security.oauth2.jwt.Jwt)8 ClientRegistration (org.springframework.security.oauth2.client.registration.ClientRegistration)6 GrantedAuthority (org.springframework.security.core.GrantedAuthority)5 Authentication (org.springframework.security.core.Authentication)3 Builder (org.springframework.security.oauth2.client.registration.ClientRegistration.Builder)3 ClientRegistrationException (org.springframework.security.oauth2.provider.ClientRegistrationException)3 EncryptionException (com.evolveum.midpoint.prism.crypto.EncryptionException)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)2 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)2 HashMap (java.util.HashMap)2 Base64Exception (org.apache.cxf.common.util.Base64Exception)2 CommonOAuth2Provider (org.springframework.security.config.oauth2.client.CommonOAuth2Provider)2 AuthenticationException (org.springframework.security.core.AuthenticationException)2 OAuth2AuthorizeRequest (org.springframework.security.oauth2.client.OAuth2AuthorizeRequest)2 ClientDetails (org.springframework.security.oauth2.provider.ClientDetails)2 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)2 DefaultExceptionMessageBuilder (com.epam.ta.reportportal.commons.exception.message.DefaultExceptionMessageBuilder)1 DefaultErrorResolver (com.epam.ta.reportportal.commons.exception.rest.DefaultErrorResolver)1 ReportPortalExceptionResolver (com.epam.ta.reportportal.commons.exception.rest.ReportPortalExceptionResolver)1