|
| 1 | +/* |
| 2 | + * Copyright 2004-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.config.annotation.web.configurers; |
| 18 | + |
| 19 | +import java.time.Clock; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.Objects; |
| 22 | + |
| 23 | +import org.springframework.context.ApplicationContext; |
| 24 | +import org.springframework.core.convert.converter.Converter; |
| 25 | +import org.springframework.security.authentication.AuthenticationManager; |
| 26 | +import org.springframework.security.authentication.apikey.ApiKeyAuthenticationProvider; |
| 27 | +import org.springframework.security.authentication.apikey.ApiKeyDigest; |
| 28 | +import org.springframework.security.authentication.apikey.ApiKeySearchService; |
| 29 | +import org.springframework.security.authentication.apikey.ApiKeySimpleGrantedAuthorityConverter; |
| 30 | +import org.springframework.security.authentication.apikey.StoredApiKey; |
| 31 | +import org.springframework.security.config.annotation.web.HttpSecurityBuilder; |
| 32 | +import org.springframework.security.core.GrantedAuthority; |
| 33 | +import org.springframework.security.core.context.SecurityContextHolderStrategy; |
| 34 | +import org.springframework.security.web.authentication.AuthenticationConverter; |
| 35 | +import org.springframework.security.web.authentication.AuthenticationFailureHandler; |
| 36 | +import org.springframework.security.web.authentication.AuthenticationSuccessHandler; |
| 37 | +import org.springframework.security.web.authentication.apikey.ApiKeyAuthenticationFilter; |
| 38 | +import org.springframework.security.web.authentication.apikey.BearerTokenAuthenticationConverter; |
| 39 | +import org.springframework.security.web.context.NullSecurityContextRepository; |
| 40 | +import org.springframework.security.web.context.SecurityContextRepository; |
| 41 | + |
| 42 | +/** |
| 43 | + * Configures API key authentication. |
| 44 | + * |
| 45 | + * @author Alexey Razinkov |
| 46 | + */ |
| 47 | +public final class ApiKeyConfigurer<H extends HttpSecurityBuilder<H>> |
| 48 | + extends AbstractHttpConfigurer<ApiKeyConfigurer<H>, H> { |
| 49 | + |
| 50 | + private final ApplicationContext context; |
| 51 | + |
| 52 | + private Clock clock = Clock.systemUTC(); |
| 53 | + |
| 54 | + private Converter<StoredApiKey, Collection<GrantedAuthority>> grantedAuthorityConverter = new ApiKeySimpleGrantedAuthorityConverter(); |
| 55 | + |
| 56 | + private ApiKeySearchService searchService; |
| 57 | + |
| 58 | + private ApiKeyDigest digest; |
| 59 | + |
| 60 | + private AuthenticationConverter authnConverter = new BearerTokenAuthenticationConverter(); |
| 61 | + |
| 62 | + private SecurityContextRepository securityContextRepository = new NullSecurityContextRepository(); |
| 63 | + |
| 64 | + private AuthenticationSuccessHandler successHandler; |
| 65 | + |
| 66 | + private AuthenticationFailureHandler failureHandler; |
| 67 | + |
| 68 | + public ApiKeyConfigurer(final ApplicationContext context) { |
| 69 | + this.context = Objects.requireNonNull(context); |
| 70 | + } |
| 71 | + |
| 72 | + public ApiKeyConfigurer<H> clock(final Clock clock) { |
| 73 | + this.clock = Objects.requireNonNull(clock); |
| 74 | + return this; |
| 75 | + } |
| 76 | + |
| 77 | + public ApiKeyConfigurer<H> grantedAuthorityConverter( |
| 78 | + final Converter<StoredApiKey, Collection<GrantedAuthority>> converter) { |
| 79 | + this.grantedAuthorityConverter = Objects.requireNonNull(converter); |
| 80 | + return this; |
| 81 | + } |
| 82 | + |
| 83 | + public ApiKeyConfigurer<H> searchService(final ApiKeySearchService searchService) { |
| 84 | + this.searchService = Objects.requireNonNull(searchService); |
| 85 | + return this; |
| 86 | + } |
| 87 | + |
| 88 | + public ApiKeyConfigurer<H> digest(final ApiKeyDigest digest) { |
| 89 | + this.digest = Objects.requireNonNull(digest); |
| 90 | + return this; |
| 91 | + } |
| 92 | + |
| 93 | + public ApiKeyConfigurer<H> authenticationConverter(final AuthenticationConverter converter) { |
| 94 | + this.authnConverter = Objects.requireNonNull(converter); |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + public ApiKeyConfigurer<H> securityContextRepository(final SecurityContextRepository securityContextRepository) { |
| 99 | + this.securityContextRepository = Objects.requireNonNull(securityContextRepository); |
| 100 | + return this; |
| 101 | + } |
| 102 | + |
| 103 | + public ApiKeyConfigurer<H> authenticationSuccessHandler(final AuthenticationSuccessHandler successHandler) { |
| 104 | + this.successHandler = successHandler; |
| 105 | + return this; |
| 106 | + } |
| 107 | + |
| 108 | + public ApiKeyConfigurer<H> authenticationFailureHandler(final AuthenticationFailureHandler failureHandler) { |
| 109 | + this.failureHandler = failureHandler; |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void init(final H builder) { |
| 115 | + super.init(builder); |
| 116 | + final ApiKeySearchService searchService = getSearchService(); |
| 117 | + final ApiKeyDigest digest = getDigest(); |
| 118 | + final ApiKeyAuthenticationProvider authnProvider = new ApiKeyAuthenticationProvider(searchService, digest, |
| 119 | + this.clock, this.grantedAuthorityConverter); |
| 120 | + builder.authenticationProvider(authnProvider); |
| 121 | + } |
| 122 | + |
| 123 | + private ApiKeySearchService getSearchService() { |
| 124 | + if (this.searchService != null) { |
| 125 | + return this.searchService; |
| 126 | + } |
| 127 | + |
| 128 | + final ApiKeySearchService bean = this.context.getBean(ApiKeySearchService.class); |
| 129 | + if (bean == null) { |
| 130 | + throw new IllegalStateException("API key search service required"); |
| 131 | + } |
| 132 | + |
| 133 | + return bean; |
| 134 | + } |
| 135 | + |
| 136 | + private ApiKeyDigest getDigest() { |
| 137 | + if (this.digest != null) { |
| 138 | + return this.digest; |
| 139 | + } |
| 140 | + |
| 141 | + final ApiKeyDigest bean = this.context.getBean(ApiKeyDigest.class); |
| 142 | + if (bean == null) { |
| 143 | + throw new IllegalStateException("API key digest required"); |
| 144 | + } |
| 145 | + |
| 146 | + return bean; |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public void configure(final H http) { |
| 151 | + final AuthenticationManager authnManager = http.getSharedObject(AuthenticationManager.class); |
| 152 | + final SecurityContextHolderStrategy securityContextHolderStrategy = getSecurityContextHolderStrategy(); |
| 153 | + final ApiKeyAuthenticationFilter filter = new ApiKeyAuthenticationFilter(authnManager, this.authnConverter, |
| 154 | + securityContextHolderStrategy, this.securityContextRepository, this.successHandler, |
| 155 | + this.failureHandler); |
| 156 | + http.addFilter(postProcess(filter)); |
| 157 | + } |
| 158 | + |
| 159 | +} |
0 commit comments