我需要创建一个AuthorizationServer来使用 client_credentials 授权类型处理OAuth2请求。客户端身份验证需要使用 LDAP 完成。我一直无法将所有部分组合在一起。
以下是我尝试过的一个代码版本:
application.yml
logging:
level:
org.springframework.security: DEBUG
server:
port: 8080
ldap:
... omitted ...
主要应用类
@SpringBootApplication
@RestController
@EnableResourceServer
public class AuthServer
{
public static void main(String[] args)
{
SpringApplication.run(AuthServer.class, args);
}
private static final Log logger = LogFactory.getLog(AuthServer.class);
@RequestMapping("/user")
public Principal user(Principal user)
{
logger.info("/user has been called");
logger.debug("user info: " + user.toString());
return user;
}
}
OAuth配置 - 请注意,此内存中的ClientDetailsService仅用于演示目的 - 我将使用从以后从LDAP获取详细信息的impl进行交换。
@Configuration
@EnableAuthorizationServer
public class OAuth2Config extends AuthorizationServerConfigurerAdapter
{
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception
{
clients.inMemory()
.withClient("client")
.secret("password")
.authorizedGrantTypes("client_credentials")
.scopes("read", "write");
}
}
如果我对LDAP使用以下配置,则实际上并未使用LDAP身份验证提供程序,即访问/ oauth / token时使用ClientDetailsService而非LDAP提供程序时发生的基本身份验证。
@Configuration
public class LdapConfig extends
GlobalAuthenticationConfigurerAdapter
{
@Value("${ldap.url}")
private String url;
@Value("${ldap.username}")
private String username;
@Value("${ldap.password}")
private String password;
@Value("${ldap.search_filter}")
private String searchFilter;
@Value("${ldap.domain}")
private String domain;
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception
{
auth.ldapAuthentication().userSearchFilter(
searchFilter)
.contextSource().url(url)
.managerDn(username)
.managerPassword(password);
}
}
但是,使用/ oauth / token返回的令牌对/ user的调用按预期工作。
现在,如果我更改LDAP配置(如下所示)(手动配置HttpSecurity),则基本身份验证过滤器在访问/ oauth / token时正确使用LDAP提供程序进行身份验证。但是,对/ user的调用不再尝试验证令牌 - 我相信由于过滤器链中缺少OAuth2AuthenticationProcessingFilter这一事实。我收到401"访问此资源需要完全身份验证"错误。
@Configuration
@EnableWebSecurity
@Order(Ordered.HIGHEST_PRECEDENCE)
public class LdapConfig2 extends WebSecurityConfigurerAdapter
{
@Value("${ldap.url}")
private String url;
@Value("${ldap.username}")
private String username;
@Value("${ldap.password}")
private String password;
@Value("${ldap.search_filter}")
private String searchFilter;
@Value("${ldap.domain}")
private String domain;
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.csrf().disable();
http.authorizeRequests().anyRequest().authenticated();
http.httpBasic();
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception
{
auth.ldapAuthentication().userSearchFilter(
searchFilter)
.contextSource().url(url)
.managerDn(username)
.managerPassword(password);
}
}
我已经尝试过这些配置的几十种变体。我错过了哪些配置可以让我拥有一个AuthroizationServer,也就是一个ResourceServer,利用LDAP进行客户端身份验证?