OAuth2 身份验证服务的集成测试?
Integration tests of an OAuth2 authentication service?
所以我正在尝试学习如何使用 Spring 引导服务,决定从 OAuth2 身份验证服务开始。当然,我需要一些集成测试来确保我的身份验证是 运行 正确。我的问题是,我可以很好地使用 curl 获得一个令牌,但是当我尝试通过它获取一个令牌时,我得到一个 400 错误和以下 JSON
{"error":"invalid_request","error_description":"Missing grant type"}
我使用的 curl 命令是
curl -v my-trusted-client:@localhost:9999/oauth/token -d grant_type=password -d username=user -d password=password
集成测试代码为
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = AuthApplication.class)
@WebIntegrationTest
public class AuthServiceIntegrationTest {
@Value("${server.port}")
private int port;
@Value("${security.user.name}")
private String username;
@Value("${security.user.password}")
private String password;
private RestTemplate template = new TestRestTemplate("my-trusted-client","");
@Test
public void testTokenGet() throws Exception{
String url = "http://localhost:"+port+"/oauth/token";
Map<String, String> data = new HashMap<>();
data.put("grant_type", "password");
data.put("username", username);
data.put("password", password);
ResponseEntity<String> token = template.postForEntity(url, data, String.class);
assertEquals(HttpStatus.OK, token.getStatusCode());
}
}
配置是
@Configuration
@EnableAuthorizationServer
public class OAuthConfiguration extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager);
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("my-trusted-client")
.authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
.authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
.scopes("read", "write", "trust")
.resourceIds("oauth2-resource")
.accessTokenValiditySeconds(60)
.and()
.withClient("my-client-with-registered-redirect")
.authorizedGrantTypes("authorization_code")
.authorities("ROLE_CLIENT")
.scopes("read", "trust")
.resourceIds("oauth2-resource")
.redirectUris("http://anywhere?key=value")
.and()
.withClient("my-client-with-secret")
.authorizedGrantTypes("client_credentials", "password")
.authorities("ROLE_CLIENT")
.scopes("read")
.resourceIds("oauth2-resource")
.secret("secret");
}
}
复制粘贴
对我做错了什么有任何见解吗?
我认为您需要 MultiValueMap
(不是常规 Map
)来说服其余模板在请求正文中发送表单编码数据。
所以我正在尝试学习如何使用 Spring 引导服务,决定从 OAuth2 身份验证服务开始。当然,我需要一些集成测试来确保我的身份验证是 运行 正确。我的问题是,我可以很好地使用 curl 获得一个令牌,但是当我尝试通过它获取一个令牌时,我得到一个 400 错误和以下 JSON
{"error":"invalid_request","error_description":"Missing grant type"}
我使用的 curl 命令是
curl -v my-trusted-client:@localhost:9999/oauth/token -d grant_type=password -d username=user -d password=password
集成测试代码为
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = AuthApplication.class)
@WebIntegrationTest
public class AuthServiceIntegrationTest {
@Value("${server.port}")
private int port;
@Value("${security.user.name}")
private String username;
@Value("${security.user.password}")
private String password;
private RestTemplate template = new TestRestTemplate("my-trusted-client","");
@Test
public void testTokenGet() throws Exception{
String url = "http://localhost:"+port+"/oauth/token";
Map<String, String> data = new HashMap<>();
data.put("grant_type", "password");
data.put("username", username);
data.put("password", password);
ResponseEntity<String> token = template.postForEntity(url, data, String.class);
assertEquals(HttpStatus.OK, token.getStatusCode());
}
}
配置是
@Configuration
@EnableAuthorizationServer
public class OAuthConfiguration extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager);
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("my-trusted-client")
.authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
.authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
.scopes("read", "write", "trust")
.resourceIds("oauth2-resource")
.accessTokenValiditySeconds(60)
.and()
.withClient("my-client-with-registered-redirect")
.authorizedGrantTypes("authorization_code")
.authorities("ROLE_CLIENT")
.scopes("read", "trust")
.resourceIds("oauth2-resource")
.redirectUris("http://anywhere?key=value")
.and()
.withClient("my-client-with-secret")
.authorizedGrantTypes("client_credentials", "password")
.authorities("ROLE_CLIENT")
.scopes("read")
.resourceIds("oauth2-resource")
.secret("secret");
}
}
复制粘贴
对我做错了什么有任何见解吗?
我认为您需要 MultiValueMap
(不是常规 Map
)来说服其余模板在请求正文中发送表单编码数据。