我在外部庫中有基于 Spring API 的身份驗證類,
以下是外部庫中的一些類,
package com.security;
import org.springframework.security.authentication.AuthenticationProvider;
@Component
public class ApiKeyAuthenticationProvider implements AuthenticationProvider {
private static final Logger LOGGER = LoggerFactory.getLogger(ApiKeyAuthenticationProvider.class);
private ApiAuthCredentialsConfig apiAuthCredentialsConfig;
public ApiKeyAuthenticationProvider(ApiAuthCredentialsConfig apiAuthCredentialsConfig) {
this.apiAuthCredentialsConfig = apiAuthCredentialsConfig;
}
}
ApiAuthCredentialsConfig:
package com.security;
public interface ApiAuthCredentialsConfig {
String getAuthToken();
String getAuthTokenHeaderName();
}
在 repo 中,我將上述外部庫添加為 gradle 依賴項。
我在實際回購中有以下變化。
@Component
@Data
public class ApiAuthenticationConfig implements ApiAuthCredentialsConfig {
@Value("${auth-token}")
private String authToken;
@Value("${auth-token-header-name}")
private String authTokenHeaderName;
}
安全配置:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private ApiKeyAuthenticationProvider apiKeyAuthenticationProvider;
@Override
public void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.
antMatcher("/**").
csrf().disable().
sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).
and().addFilterBefore(new ApiKeyAuthenticationFilter(authenticationManager(), apiKeyAuthenticationProvider.getHeaderName()), AnonymousAuthenticationFilter.class)
.authorizeRequests().anyRequest().authenticated();
}
@Bean
public AuthenticationManager authenticationManager() {
return new ProviderManager(Collections.singletonList(apiKeyAuthenticationProvider));
}
}
應用:
@SpringBootApplication(scanBasePackages = {"com.security"})
@EnableSwagger2
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
當我運行應用程式時,它失敗并出現以下錯誤
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.security.ApiKeyAuthenticationProvider required a bean of type 'com.security.ApiAuthCredentialsConfig' that could not be found.
如何解決這個問題?
uj5u.com熱心網友回復:
在上面的評論之后,我意識到它ApiAuthenticationConfig與 不在同一個包中ApiAuthCredentialsConfig,在我轉移ApiAuthenticationConfig到com.security包之后它開始作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/429357.html
下一篇:Vite-更改靜態資產的目錄
