我正在尝试配置一个能够完成基本 OAuth2 流程的 OAuth2 服务器(有关示例,请参见此处)。
为长问题道歉
我的第一次尝试是能够执行授权代码流。
我有以下配置:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfig
extends WebSecurityConfigurerAdapter {
....
@Inject
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(userDetailsService)
.passwordEncoder(passwordEncoder());
}
...
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin().loginPage("/login").permitAll()
.and()
.requestMatchers().antMatchers("/login", "/oauth/authorize", "/oauth/confirm_access")
.and()
.authorizeRequests().anyRequest().authenticated();
}
...
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
我的服务器配置
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorServerConfig
extends AuthorizationServerConfigurerAdapter {
@Inject
private TokenStore tokenStore;
@Inject
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints
.tokenStore(tokenStore)
.authenticationManager(authenticationManager);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security
.allowFormAuthenticationForClients()
.checkTokenAccess("authenticated()");
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
.inMemory()
.withClient("foo")
.secret("foo")
.authorizedGrantTypes("authorization_code","password", "refresh_token")
.scopes(new String[] { "read", "write" })
}
事实上,我的授权代码流程工作正常!当我尝试按如下方式执行密码流程时问题开始:
POST localhost:8200/oauth/token
Content-Type: application/x-www-form-urlencoded
Accept:application/json
Authorization:Basic Zm9vOmZvbw=="
username=admin&password=admin&grant_type=password&scope=read%20write&client_secret=foo&client_id=foo&
我的问题是 Authorization 标头被忽略,无论是否存在结果都是一样的,它给了我 access_token 和 refresh_token
如果我尝试按如下方式启用基本身份验证,请启用 ClientUserDetailsService,从数据库 JDBC 读取客户端:
public class SecurityConfig
extends WebSecurityConfigurerAdapter {
....
@Bean
public ClientDetailsUserDetailsService clientDetailsUserDetailsService(ClientDetailsService clientDetailsService){
// JDBC clientDetailsService
return new ClientDetailsUserDetailsService(clientDetailsService);
}
@Inject
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(clientDetailsUserDetailsService());
}
...
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
DaoAuthenticationProvider p = new DaoAuthenticationProvider();
p.setUserDetailsService(userDetailsService);
p.setPasswordEncoder(passwordEncoder());
return new ProviderManager(Lists.newArrayList(p));
// return super.authenticationManagerBean();
}
}
现在我所取得的成就是让基本身份验证正常工作但我失去了授权代码流,因为现在基本身份验证是针对客户端 ID 和机密而不是用户的实际凭据完成的
我错过了什么吗?我怎样才能同时拥有两个流程?请帮助,我现在挣扎了几天。
一些类似的问题,但没有任何运气: