Search in sources :

Example 1 with BearerTokenResolver

use of org.springframework.security.oauth2.server.resource.web.BearerTokenResolver in project spring-security by spring-projects.

the class OAuth2ResourceServerConfigurerTests method getBearerTokenResolverWhenResolverBeanAndAnotherOnTheDslThenTheDslOneIsUsed.

@Test
public void getBearerTokenResolverWhenResolverBeanAndAnotherOnTheDslThenTheDslOneIsUsed() {
    BearerTokenResolver resolver = mock(BearerTokenResolver.class);
    BearerTokenResolver resolverBean = mock(BearerTokenResolver.class);
    GenericWebApplicationContext context = new GenericWebApplicationContext();
    context.registerBean(BearerTokenResolver.class, () -> resolverBean);
    this.spring.context(context).autowire();
    OAuth2ResourceServerConfigurer oauth2 = new OAuth2ResourceServerConfigurer(context);
    oauth2.bearerTokenResolver(resolver);
    assertThat(oauth2.getBearerTokenResolver()).isEqualTo(resolver);
}
Also used : GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) DefaultBearerTokenResolver(org.springframework.security.oauth2.server.resource.web.DefaultBearerTokenResolver) BearerTokenResolver(org.springframework.security.oauth2.server.resource.web.BearerTokenResolver) Test(org.junit.jupiter.api.Test)

Example 2 with BearerTokenResolver

use of org.springframework.security.oauth2.server.resource.web.BearerTokenResolver in project spring-security by spring-projects.

the class OAuth2ResourceServerConfigurer method configure.

@Override
public void configure(H http) {
    BearerTokenResolver bearerTokenResolver = getBearerTokenResolver();
    this.requestMatcher.setBearerTokenResolver(bearerTokenResolver);
    AuthenticationManagerResolver resolver = this.authenticationManagerResolver;
    if (resolver == null) {
        AuthenticationManager authenticationManager = getAuthenticationManager(http);
        resolver = (request) -> authenticationManager;
    }
    BearerTokenAuthenticationFilter filter = new BearerTokenAuthenticationFilter(resolver);
    filter.setBearerTokenResolver(bearerTokenResolver);
    filter.setAuthenticationEntryPoint(this.authenticationEntryPoint);
    filter = postProcess(filter);
    http.addFilter(filter);
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) AuthenticationManagerResolver(org.springframework.security.authentication.AuthenticationManagerResolver) BearerTokenAuthenticationFilter(org.springframework.security.oauth2.server.resource.web.BearerTokenAuthenticationFilter) DefaultBearerTokenResolver(org.springframework.security.oauth2.server.resource.web.DefaultBearerTokenResolver) BearerTokenResolver(org.springframework.security.oauth2.server.resource.web.BearerTokenResolver)

Example 3 with BearerTokenResolver

use of org.springframework.security.oauth2.server.resource.web.BearerTokenResolver in project spring-security by spring-projects.

the class OAuth2ResourceServerConfigurerTests method getBearerTokenResolverWhenDuplicateResolverBeansAndAnotherOnTheDslThenTheDslOneIsUsed.

@Test
public void getBearerTokenResolverWhenDuplicateResolverBeansAndAnotherOnTheDslThenTheDslOneIsUsed() {
    BearerTokenResolver resolverBean = mock(BearerTokenResolver.class);
    BearerTokenResolver resolver = mock(BearerTokenResolver.class);
    GenericWebApplicationContext context = new GenericWebApplicationContext();
    context.registerBean("resolverOne", BearerTokenResolver.class, () -> resolverBean);
    context.registerBean("resolverTwo", BearerTokenResolver.class, () -> resolverBean);
    this.spring.context(context).autowire();
    OAuth2ResourceServerConfigurer oauth2 = new OAuth2ResourceServerConfigurer(context);
    oauth2.bearerTokenResolver(resolver);
    assertThat(oauth2.getBearerTokenResolver()).isEqualTo(resolver);
}
Also used : GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) DefaultBearerTokenResolver(org.springframework.security.oauth2.server.resource.web.DefaultBearerTokenResolver) BearerTokenResolver(org.springframework.security.oauth2.server.resource.web.BearerTokenResolver) Test(org.junit.jupiter.api.Test)

Example 4 with BearerTokenResolver

use of org.springframework.security.oauth2.server.resource.web.BearerTokenResolver in project spring-security by spring-projects.

the class OAuth2ResourceServerBeanDefinitionParserTests method getWhenCustomBearerTokenResolverThenUses.

@Test
public void getWhenCustomBearerTokenResolverThenUses() throws Exception {
    this.spring.configLocations(xml("MockBearerTokenResolver"), xml("MockJwtDecoder"), xml("BearerTokenResolver")).autowire();
    JwtDecoder decoder = this.spring.getContext().getBean(JwtDecoder.class);
    given(decoder.decode("token")).willReturn(TestJwts.jwt().build());
    BearerTokenResolver bearerTokenResolver = this.spring.getContext().getBean(BearerTokenResolver.class);
    given(bearerTokenResolver.resolve(any(HttpServletRequest.class))).willReturn("token");
    this.mvc.perform(get("/")).andExpect(status().isNotFound());
    verify(decoder).decode("token");
    verify(bearerTokenResolver).resolve(any(HttpServletRequest.class));
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) NimbusJwtDecoder(org.springframework.security.oauth2.jwt.NimbusJwtDecoder) JwtDecoder(org.springframework.security.oauth2.jwt.JwtDecoder) BearerTokenResolver(org.springframework.security.oauth2.server.resource.web.BearerTokenResolver) Test(org.junit.jupiter.api.Test)

Aggregations

BearerTokenResolver (org.springframework.security.oauth2.server.resource.web.BearerTokenResolver)4 Test (org.junit.jupiter.api.Test)3 DefaultBearerTokenResolver (org.springframework.security.oauth2.server.resource.web.DefaultBearerTokenResolver)3 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 AuthenticationManager (org.springframework.security.authentication.AuthenticationManager)1 AuthenticationManagerResolver (org.springframework.security.authentication.AuthenticationManagerResolver)1 JwtDecoder (org.springframework.security.oauth2.jwt.JwtDecoder)1 NimbusJwtDecoder (org.springframework.security.oauth2.jwt.NimbusJwtDecoder)1 BearerTokenAuthenticationFilter (org.springframework.security.oauth2.server.resource.web.BearerTokenAuthenticationFilter)1