|
73 | 73 | import org.springframework.mock.web.MockHttpServletRequest;
|
74 | 74 | import org.springframework.security.access.prepost.PreAuthorize;
|
75 | 75 | import org.springframework.security.authentication.AbstractAuthenticationToken;
|
| 76 | +import org.springframework.security.authentication.AuthenticationEventPublisher; |
76 | 77 | import org.springframework.security.authentication.AuthenticationManager;
|
77 | 78 | import org.springframework.security.authentication.AuthenticationManagerResolver;
|
78 | 79 | import org.springframework.security.authentication.AuthenticationProvider;
|
|
88 | 89 | import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
89 | 90 | import org.springframework.security.core.userdetails.UserDetailsService;
|
90 | 91 | import org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal;
|
| 92 | +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; |
91 | 93 | import org.springframework.security.oauth2.core.OAuth2Error;
|
92 | 94 | import org.springframework.security.oauth2.core.OAuth2TokenValidator;
|
93 | 95 | import org.springframework.security.oauth2.core.OAuth2TokenValidatorResult;
|
|
99 | 101 | import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
|
100 | 102 | import org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthentication;
|
101 | 103 | import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter;
|
102 |
| -import org.springframework.security.oauth2.server.resource.authentication.JwtIssuerAuthenticationManagerResolver; |
103 | 104 | import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
|
| 105 | +import org.springframework.security.oauth2.server.resource.authentication.JwtIssuerAuthenticationManagerResolver; |
104 | 106 | import org.springframework.security.oauth2.server.resource.introspection.NimbusOpaqueTokenIntrospector;
|
105 | 107 | import org.springframework.security.oauth2.server.resource.introspection.OpaqueTokenIntrospector;
|
106 | 108 | import org.springframework.security.oauth2.server.resource.web.BearerTokenAuthenticationEntryPoint;
|
@@ -1091,6 +1093,22 @@ public void requestWhenUsingPublicKeyAlgorithmDoesNotMatchThenReturnsInvalidToke
|
1091 | 1093 | .andExpect(invalidTokenHeader("algorithm"));
|
1092 | 1094 | }
|
1093 | 1095 |
|
| 1096 | + // gh-7793 |
| 1097 | + @Test |
| 1098 | + public void requestWhenUsingCustomAuthenticationEventPublisherThenUses() throws Exception{ |
| 1099 | + this.spring.register(CustomAuthenticationEventPublisher.class).autowire(); |
| 1100 | + |
| 1101 | + when(bean(JwtDecoder.class).decode(anyString())) |
| 1102 | + .thenThrow(new JwtException("problem")); |
| 1103 | + |
| 1104 | + this.mvc.perform(get("/").with(bearerToken("token"))); |
| 1105 | + |
| 1106 | + verifyBean(AuthenticationEventPublisher.class) |
| 1107 | + .publishAuthenticationFailure( |
| 1108 | + any(OAuth2AuthenticationException.class), |
| 1109 | + any(Authentication.class)); |
| 1110 | + } |
| 1111 | + |
1094 | 1112 | @Test
|
1095 | 1113 | public void getWhenCustomJwtAuthenticationManagerThenUsed() throws Exception {
|
1096 | 1114 | this.spring.register(JwtAuthenticationManagerConfig.class, BasicController.class).autowire();
|
@@ -2015,6 +2033,31 @@ JwtDecoder decoder() throws Exception {
|
2015 | 2033 | }
|
2016 | 2034 | }
|
2017 | 2035 |
|
| 2036 | + @EnableWebSecurity |
| 2037 | + static class CustomAuthenticationEventPublisher extends WebSecurityConfigurerAdapter { |
| 2038 | + @Override |
| 2039 | + protected void configure(HttpSecurity http) throws Exception { |
| 2040 | + // @formatter:off |
| 2041 | + http |
| 2042 | + .authorizeRequests() |
| 2043 | + .anyRequest().authenticated() |
| 2044 | + .and() |
| 2045 | + .oauth2ResourceServer() |
| 2046 | + .jwt(); |
| 2047 | + // @formatter:on |
| 2048 | + } |
| 2049 | + |
| 2050 | + @Bean |
| 2051 | + JwtDecoder jwtDecoder() { |
| 2052 | + return mock(JwtDecoder.class); |
| 2053 | + } |
| 2054 | + |
| 2055 | + @Bean |
| 2056 | + AuthenticationEventPublisher authenticationEventPublisher() { |
| 2057 | + return mock(AuthenticationEventPublisher.class); |
| 2058 | + } |
| 2059 | + } |
| 2060 | + |
2018 | 2061 | @EnableWebSecurity
|
2019 | 2062 | static class OpaqueTokenConfig extends WebSecurityConfigurerAdapter {
|
2020 | 2063 | @Override
|
|
0 commit comments