我目前正在使用 Spring Boot 2 实现身份验证流程,这将允许 Oauth 和基本身份验证流程。
我想使用 OAuth 表来促进将客户端 ID 与客户端 key 进行匹配的身份验证。 (我读到这是 Spring 中的默认行为,并且 ClientDetailsUserDetailsService
显然就是以这种方式实现的。
下面反射(reflect)了我的应用程序中当前拥有的两种配置之一(缺少的一项是 OAuth 特定的)。
我目前得到:
描述:
Field clientDetailsService in com.mydomain.core.Application$BasicAuthConfigurationAdapter required a bean of type 'org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
想知道如何注入(inject)这个类(我认为将其构建为 Bean 是前进的方向?),实际上这是否是针对数据库(clientid 和 secret )进行身份验证所采取的正确方法
@Order( 1 )
@Configuration
@Profile( "!test" )
public static class BasicAuthConfigurationAdapter extends WebSecurityConfigurerAdapter
{
@Autowired
private ClientDetailsUserDetailsService clientDetailsService;
@Bean
public AuthenticationManager customAuthenticationManager() throws Exception
{
return authenticationManager();
}
@Override
public void configure( HttpSecurity http ) throws Exception
{
http
.authorizeRequests()
.antMatchers( "/dummy/**" ).hasAnyRole( "ADMIN", "USER" )
.anyRequest()
.authenticated()
.and().httpBasic();
}
@Autowired
public void configAuthentication( AuthenticationManagerBuilder auth ) throws Exception
{
auth.parentAuthenticationManager( authenticationManagerBean() ).userDetailsService( clientDetailsService );
}
}
最佳答案
我们已经配置了 ClientDetailsService,如下所示:
@Configuration
@EnableAuthorizationServer
class OAuth2Configuration extends AuthorizationServerConfigurerAdapter {
// TODO: Configure clients through a clientDetails service
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
ClientDetailsService jdbcClientDetailsServiceBuilder = clients.jdbc(dataSource).build();
try {
jdbcClientDetailsServiceBuilder.loadClientByClientId(sClientName);
} catch (NoSuchClientException noSuchClientException) {
logger.error("message={Caught Exception {}}", noSuchClientException.getMessage(), noSuchClientException);
// @formatter:off
clients.jdbc(dataSource).passwordEncoder(passwordEncoder).withClient(sServiceClientName)
.authorizedGrantTypes(syntheticsServiceClientGrantTypes).authorities(sConstants.ROLE_USER)
.scopes(s.OAUTH2_WRITE_SCOPE).resourceIds(OAUTH2_DEFAULT_RESOURCE_ID)
.secret(sServiceClientSecret);
// @formatter:on
} catch (Exception e) {
logger.error("message={Caught Exception {}}", e.getMessage(), e);
}
}
}
这是一个很好的资源,可以帮助找出不同的组件:https://spring.io/guides/tutorials/spring-boot-oauth2/
关于java - Spring Boot使用ClientDetailsUserDetailsService加载ClientDetails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57724254/