Spring OAuth2 在发送刷新令牌时要求输入密码
Spring OAuth2 asking for password when sending refresh token
我一直在尝试通过 Spring Boot 首次使用 OAuth2。
基础架构正在运行,但是,当授权令牌过期并且我发送刷新令牌以获取新令牌时,我还被要求提供用户名和密码。
问题:刷新令牌是否足以获取新的授权令牌?否则我需要存储用户凭据,这听起来不对。
我完全搞错了吗?
谢谢!
我的 ResourceServer 配置:
@Configuration
@EnableResourceServer
public class OAuth2ConfigResource extends
ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.requestMatchers()
.antMatchers("/**")
.and()
.authorizeRequests()
.antMatchers("/api/secure/**")
.access("#oauth2.clientHasRole('ADMIN')")
.antMatchers("/api/public/**")
.access("#oauth2.clientHasRole('USER')");
}
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.tokenStore(tokenStore());
}
@Bean
public TokenStore tokenStore() {
return new JwtTokenStore(jwtAccessTokenConverter());
}
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
JwtAccessTokenConverter tokenConverter = new JwtAccessTokenConverter();
return tokenConverter;
}
}
我的 AuthServer 配置:
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationConfig 扩展了 AuthorizationServerConfigurerAdapter {
@Autowired
@Qualifier("userDetailsService")
private UserDetailsService userDetailsService;
@Autowired
private AuthenticationManager authenticationManager;
private int expiration = 60;
@Bean
@Autowired
public PasswordEncoder passwordEncoder(@Value("${jwt.secret}") String secret) {
return new StandardPasswordEncoder(secret);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer.allowFormAuthenticationForClients();
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer configurer) throws Exception {
configurer.authenticationManager(authenticationManager);
configurer.userDetailsService(userDetailsService);
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("user1")
.secret("pass1")
.accessTokenValiditySeconds(expiration)
.authorities("ADMIN", "USER")
.scopes("read")
.authorizedGrantTypes("password", "refresh_token", "client_credentials")
.and()
.withClient("user2")
.secret("pass2")
.accessTokenValiditySeconds(expiration)
.authorities("USER")
.scopes("read")
.authorizedGrantTypes("password", "refresh_token", "client_credentials");
}
}
用法:
1.获取代币
curl -X POST --user 'user2:pass2' -d 'grant_type=password&username=user2&password=pass2' http://localhost:8080/oauth/token
2。到达 API
curl -i -H "Accept: application/json" -H "Authorization: Bearer 260480f7-3e94-4811-a49d-b385dff83f4a" http://localhost:8080/api/public/request/felipe
3。使用刷新令牌获取新的授权令牌
curl -X POST --user 'user2:pass2' -d 'grant_type=refresh_token&refresh_token=48c9f1db-9252-47a0-9a28-091caa775e4c' http://localhost:8080/oauth/token
如前所述,此基础架构运行良好,但是,我不明白为什么我应该在使用刷新令牌时传递凭据(假设没有解决方法),因为此令牌的存在只是为了防止 - 尽我所能理解 - 证书的阐述。
1。用户凭据
username
和 password
是指定 用户凭据 (用户 ID 和密码)的参数。
grant_type=password&username={User-ID}&password={Password}
2。客户端凭据
Authorization
header 包含在对令牌端点的请求中包含 客户端凭据 (客户端 ID 和客户端密码)。
--user '{Client-ID}:{Client-Secret}'
3。推荐
您应该更改以下代码:
.withClient("user2")
.secret("pass2")
到
.withClient("clientId2")
.secret("clientSecret2")
以避免混淆。
我一直在尝试通过 Spring Boot 首次使用 OAuth2。
基础架构正在运行,但是,当授权令牌过期并且我发送刷新令牌以获取新令牌时,我还被要求提供用户名和密码。
问题:刷新令牌是否足以获取新的授权令牌?否则我需要存储用户凭据,这听起来不对。
我完全搞错了吗?
谢谢!
我的 ResourceServer 配置:
@Configuration
@EnableResourceServer
public class OAuth2ConfigResource extends
ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.requestMatchers()
.antMatchers("/**")
.and()
.authorizeRequests()
.antMatchers("/api/secure/**")
.access("#oauth2.clientHasRole('ADMIN')")
.antMatchers("/api/public/**")
.access("#oauth2.clientHasRole('USER')");
}
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.tokenStore(tokenStore());
}
@Bean
public TokenStore tokenStore() {
return new JwtTokenStore(jwtAccessTokenConverter());
}
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
JwtAccessTokenConverter tokenConverter = new JwtAccessTokenConverter();
return tokenConverter;
}
}
我的 AuthServer 配置:
@Configuration
@EnableAuthorizationServer public class OAuth2AuthorizationConfig 扩展了 AuthorizationServerConfigurerAdapter {
@Autowired
@Qualifier("userDetailsService")
private UserDetailsService userDetailsService;
@Autowired
private AuthenticationManager authenticationManager;
private int expiration = 60;
@Bean
@Autowired
public PasswordEncoder passwordEncoder(@Value("${jwt.secret}") String secret) {
return new StandardPasswordEncoder(secret);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer.allowFormAuthenticationForClients();
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer configurer) throws Exception {
configurer.authenticationManager(authenticationManager);
configurer.userDetailsService(userDetailsService);
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("user1")
.secret("pass1")
.accessTokenValiditySeconds(expiration)
.authorities("ADMIN", "USER")
.scopes("read")
.authorizedGrantTypes("password", "refresh_token", "client_credentials")
.and()
.withClient("user2")
.secret("pass2")
.accessTokenValiditySeconds(expiration)
.authorities("USER")
.scopes("read")
.authorizedGrantTypes("password", "refresh_token", "client_credentials");
}
}
用法:
1.获取代币
curl -X POST --user 'user2:pass2' -d 'grant_type=password&username=user2&password=pass2' http://localhost:8080/oauth/token
2。到达 API
curl -i -H "Accept: application/json" -H "Authorization: Bearer 260480f7-3e94-4811-a49d-b385dff83f4a" http://localhost:8080/api/public/request/felipe
3。使用刷新令牌获取新的授权令牌
curl -X POST --user 'user2:pass2' -d 'grant_type=refresh_token&refresh_token=48c9f1db-9252-47a0-9a28-091caa775e4c' http://localhost:8080/oauth/token
如前所述,此基础架构运行良好,但是,我不明白为什么我应该在使用刷新令牌时传递凭据(假设没有解决方法),因为此令牌的存在只是为了防止 - 尽我所能理解 - 证书的阐述。
1。用户凭据
username
和 password
是指定 用户凭据 (用户 ID 和密码)的参数。
grant_type=password&username={User-ID}&password={Password}
2。客户端凭据
Authorization
header 包含在对令牌端点的请求中包含 客户端凭据 (客户端 ID 和客户端密码)。
--user '{Client-ID}:{Client-Secret}'
3。推荐
您应该更改以下代码:
.withClient("user2")
.secret("pass2")
到
.withClient("clientId2")
.secret("clientSecret2")
以避免混淆。