简体   繁体   English

OAuth2身份验证服务的集成测试?

[英]Integration tests of an OAuth2 authentication service?

So I'm trying to learn how to use Spring Boot services, decided the place to start was an OAuth2 authentication service. 因此,我试图学习如何使用Spring Boot服务,因此决定从OAuth2身份验证服务开始。 Of course, I want some integration tests to make sure my auth is running properly. 当然,我希望进行一些集成测试以确保我的身份验证正常运行。 My problem is, I can get a token using curl just fine, however when I try to grab one through I get a 400 error and the following JSON 我的问题是,我可以使用curl获得令牌,但是当我尝试通过它获取一个令牌时,出现400错误和以下JSON

{"error":"invalid_request","error_description":"Missing grant type"}

The curl command I'm using is 我正在使用的curl命令是

curl -v my-trusted-client:@localhost:9999/oauth/token -d grant_type=password -d username=user -d password=password

Integration test code is 集成测试代码为

@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());
    }
}

And the config is 和配置是

@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");
    }
}

Pretty much copied and pasted from https://github.com/dsyer/spring-oauth2-integration-tests/blob/master/vanilla/src/main/java/demo/Application.java https://github.com/dsyer/spring-oauth2-integration-tests/blob/master/vanilla/src/main/java/demo/Application.java复制并粘贴的内容

Any insights into what I'm doing wrong? 对我在做什么错有任何见解吗?

我认为您需要一个MultiValueMap (而不是常规Map )来说服其余模板在请求正文中发送表单编码的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM