parent
31f310fd22
commit
b774e91734
|
@ -80,8 +80,8 @@ import org.springframework.web.accept.HeaderContentNegotiationStrategy;
|
||||||
* authentication failures are handled
|
* authentication failures are handled
|
||||||
* <li>{@link #bearerTokenResolver(BearerTokenResolver)} - customizes how to resolve a
|
* <li>{@link #bearerTokenResolver(BearerTokenResolver)} - customizes how to resolve a
|
||||||
* bearer token from the request</li>
|
* bearer token from the request</li>
|
||||||
* <li>{@link #bearerTokenAuthenticationConverter(AuthenticationConverter)}</li> -
|
* <li>{@link #authenticationConverter(AuthenticationConverter)}</li> - customizes how to
|
||||||
* customizes how to convert a bear token authentication from the request
|
* convert a bearer token authentication from the request
|
||||||
* <li>{@link #jwt(Customizer)} - enables Jwt-encoded bearer token support</li>
|
* <li>{@link #jwt(Customizer)} - enables Jwt-encoded bearer token support</li>
|
||||||
* <li>{@link #opaqueToken(Customizer)} - enables opaque bearer token support</li>
|
* <li>{@link #opaqueToken(Customizer)} - enables opaque bearer token support</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
|
@ -195,8 +195,7 @@ public final class OAuth2ResourceServerConfigurer<H extends HttpSecurityBuilder<
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OAuth2ResourceServerConfigurer<H> bearerTokenAuthenticationConverter(
|
public OAuth2ResourceServerConfigurer<H> authenticationConverter(AuthenticationConverter authenticationConverter) {
|
||||||
AuthenticationConverter authenticationConverter) {
|
|
||||||
Assert.notNull(authenticationConverter, "authenticationConverter cannot be null");
|
Assert.notNull(authenticationConverter, "authenticationConverter cannot be null");
|
||||||
this.authenticationConverter = authenticationConverter;
|
this.authenticationConverter = authenticationConverter;
|
||||||
return this;
|
return this;
|
||||||
|
@ -266,7 +265,7 @@ public final class OAuth2ResourceServerConfigurer<H extends HttpSecurityBuilder<
|
||||||
resolver = (request) -> authenticationManager;
|
resolver = (request) -> authenticationManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.authenticationConverter = getBearerTokenAuthenticationConverter();
|
this.authenticationConverter = getAuthenticationConverter();
|
||||||
|
|
||||||
BearerTokenAuthenticationFilter filter = new BearerTokenAuthenticationFilter(resolver);
|
BearerTokenAuthenticationFilter filter = new BearerTokenAuthenticationFilter(resolver);
|
||||||
filter.setAuthenticationConverter(this.authenticationConverter);
|
filter.setAuthenticationConverter(this.authenticationConverter);
|
||||||
|
@ -363,7 +362,7 @@ public final class OAuth2ResourceServerConfigurer<H extends HttpSecurityBuilder<
|
||||||
return this.bearerTokenResolver;
|
return this.bearerTokenResolver;
|
||||||
}
|
}
|
||||||
|
|
||||||
AuthenticationConverter getBearerTokenAuthenticationConverter() {
|
AuthenticationConverter getAuthenticationConverter() {
|
||||||
if (this.authenticationConverter == null) {
|
if (this.authenticationConverter == null) {
|
||||||
if (this.context.getBeanNamesForType(BearerTokenAuthenticationConverter.class).length > 0) {
|
if (this.context.getBeanNamesForType(BearerTokenAuthenticationConverter.class).length > 0) {
|
||||||
this.authenticationConverter = this.context.getBean(BearerTokenAuthenticationConverter.class);
|
this.authenticationConverter = this.context.getBean(BearerTokenAuthenticationConverter.class);
|
||||||
|
|
|
@ -732,8 +732,8 @@ public class OAuth2ResourceServerConfigurerTests {
|
||||||
context.registerBean("converterTwo", BearerTokenAuthenticationConverter.class, () -> converterBean);
|
context.registerBean("converterTwo", BearerTokenAuthenticationConverter.class, () -> converterBean);
|
||||||
this.spring.context(context).autowire();
|
this.spring.context(context).autowire();
|
||||||
OAuth2ResourceServerConfigurer oauth2 = new OAuth2ResourceServerConfigurer(context);
|
OAuth2ResourceServerConfigurer oauth2 = new OAuth2ResourceServerConfigurer(context);
|
||||||
oauth2.bearerTokenAuthenticationConverter(converter);
|
oauth2.authenticationConverter(converter);
|
||||||
assertThat(oauth2.getBearerTokenAuthenticationConverter()).isEqualTo(converter);
|
assertThat(oauth2.getAuthenticationConverter()).isEqualTo(converter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -751,16 +751,15 @@ public class OAuth2ResourceServerConfigurerTests {
|
||||||
context.registerBean(BearerTokenAuthenticationConverter.class, () -> converterBean);
|
context.registerBean(BearerTokenAuthenticationConverter.class, () -> converterBean);
|
||||||
this.spring.context(context).autowire();
|
this.spring.context(context).autowire();
|
||||||
OAuth2ResourceServerConfigurer oauth2 = new OAuth2ResourceServerConfigurer(context);
|
OAuth2ResourceServerConfigurer oauth2 = new OAuth2ResourceServerConfigurer(context);
|
||||||
oauth2.bearerTokenAuthenticationConverter(converter);
|
oauth2.authenticationConverter(converter);
|
||||||
assertThat(oauth2.getBearerTokenAuthenticationConverter()).isEqualTo(converter);
|
assertThat(oauth2.getAuthenticationConverter()).isEqualTo(converter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getBearerTokenAuthenticationConverterWhenNoConverterSpecifiedThenTheDefaultIsUsed() {
|
public void getBearerTokenAuthenticationConverterWhenNoConverterSpecifiedThenTheDefaultIsUsed() {
|
||||||
ApplicationContext context = this.spring.context(new GenericWebApplicationContext()).getContext();
|
ApplicationContext context = this.spring.context(new GenericWebApplicationContext()).getContext();
|
||||||
OAuth2ResourceServerConfigurer oauth2 = new OAuth2ResourceServerConfigurer(context);
|
OAuth2ResourceServerConfigurer oauth2 = new OAuth2ResourceServerConfigurer(context);
|
||||||
assertThat(oauth2.getBearerTokenAuthenticationConverter())
|
assertThat(oauth2.getAuthenticationConverter()).isInstanceOf(BearerTokenAuthenticationConverter.class);
|
||||||
.isInstanceOf(BearerTokenAuthenticationConverter.class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -770,7 +769,7 @@ public class OAuth2ResourceServerConfigurerTests {
|
||||||
context.registerBean(BearerTokenAuthenticationConverter.class, () -> converterBean);
|
context.registerBean(BearerTokenAuthenticationConverter.class, () -> converterBean);
|
||||||
this.spring.context(context).autowire();
|
this.spring.context(context).autowire();
|
||||||
OAuth2ResourceServerConfigurer oauth2 = new OAuth2ResourceServerConfigurer(context);
|
OAuth2ResourceServerConfigurer oauth2 = new OAuth2ResourceServerConfigurer(context);
|
||||||
assertThat(oauth2.getBearerTokenAuthenticationConverter()).isEqualTo(converterBean);
|
assertThat(oauth2.getAuthenticationConverter()).isEqualTo(converterBean);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -783,7 +782,7 @@ public class OAuth2ResourceServerConfigurerTests {
|
||||||
this.spring.context(context).autowire();
|
this.spring.context(context).autowire();
|
||||||
OAuth2ResourceServerConfigurer oauth2 = new OAuth2ResourceServerConfigurer(context);
|
OAuth2ResourceServerConfigurer oauth2 = new OAuth2ResourceServerConfigurer(context);
|
||||||
BearerTokenAuthenticationToken bearerTokenAuthenticationToken = (BearerTokenAuthenticationToken) oauth2
|
BearerTokenAuthenticationToken bearerTokenAuthenticationToken = (BearerTokenAuthenticationToken) oauth2
|
||||||
.getBearerTokenAuthenticationConverter().convert(servletRequest);
|
.getAuthenticationConverter().convert(servletRequest);
|
||||||
String token = bearerTokenAuthenticationToken.getToken();
|
String token = bearerTokenAuthenticationToken.getToken();
|
||||||
assertThat(token).isEqualTo("bearer customToken");
|
assertThat(token).isEqualTo("bearer customToken");
|
||||||
|
|
||||||
|
|
|
@ -36,14 +36,10 @@ import org.springframework.util.Assert;
|
||||||
*/
|
*/
|
||||||
public final class BearerTokenAuthenticationConverter implements AuthenticationConverter {
|
public final class BearerTokenAuthenticationConverter implements AuthenticationConverter {
|
||||||
|
|
||||||
|
private BearerTokenResolver bearerTokenResolver = new DefaultBearerTokenResolver();
|
||||||
|
|
||||||
private AuthenticationDetailsSource<HttpServletRequest, ?> authenticationDetailsSource = new WebAuthenticationDetailsSource();
|
private AuthenticationDetailsSource<HttpServletRequest, ?> authenticationDetailsSource = new WebAuthenticationDetailsSource();
|
||||||
|
|
||||||
private BearerTokenResolver bearerTokenResolver;
|
|
||||||
|
|
||||||
public BearerTokenAuthenticationConverter() {
|
|
||||||
this.bearerTokenResolver = new DefaultBearerTokenResolver();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BearerTokenAuthenticationToken convert(HttpServletRequest request) {
|
public BearerTokenAuthenticationToken convert(HttpServletRequest request) {
|
||||||
String token = this.bearerTokenResolver.resolve(request);
|
String token = this.bearerTokenResolver.resolve(request);
|
||||||
|
|
|
@ -153,6 +153,17 @@ public final class BearerTokenAuthenticationFilter extends OncePerRequestFilter
|
||||||
((BearerTokenAuthenticationConverter) this.authenticationConverter).setBearerTokenResolver(bearerTokenResolver);
|
((BearerTokenAuthenticationConverter) this.authenticationConverter).setBearerTokenResolver(bearerTokenResolver);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the {@link AuthenticationConverter} to use. Defaults to
|
||||||
|
* {@link BearerTokenAuthenticationConverter}.
|
||||||
|
* @param authenticationConverter the {@code AuthenticationConverter} to use
|
||||||
|
* @since 5.5
|
||||||
|
*/
|
||||||
|
public void setAuthenticationConverter(AuthenticationConverter authenticationConverter) {
|
||||||
|
Assert.notNull(authenticationConverter, "authenticationConverter cannot be null");
|
||||||
|
this.authenticationConverter = authenticationConverter;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the {@link AuthenticationEntryPoint} to use. Defaults to
|
* Set the {@link AuthenticationEntryPoint} to use. Defaults to
|
||||||
* {@link BearerTokenAuthenticationEntryPoint}.
|
* {@link BearerTokenAuthenticationEntryPoint}.
|
||||||
|
@ -174,15 +185,4 @@ public final class BearerTokenAuthenticationFilter extends OncePerRequestFilter
|
||||||
this.authenticationFailureHandler = authenticationFailureHandler;
|
this.authenticationFailureHandler = authenticationFailureHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the {@link AuthenticationConverter} to use. Defaults to
|
|
||||||
* {@link BearerTokenAuthenticationConverter}.
|
|
||||||
* @param authenticationConverter the {@code AuthenticationConverter} to use
|
|
||||||
* @since 5.5
|
|
||||||
*/
|
|
||||||
public void setAuthenticationConverter(AuthenticationConverter authenticationConverter) {
|
|
||||||
Assert.notNull(authenticationConverter, "authenticationConverter cannot be null");
|
|
||||||
this.authenticationConverter = authenticationConverter;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue