gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:13:41 26 4
gpt4 key购买 nike

所以我试图学习如何使用 Spring Boot 服务,决定从 OAuth2 身份验证服务开始。当然,我需要一些集成测试来确保我的身份验证正常运行。我的问题是,我可以使用curl 很好地获取 token ,但是当我尝试通过获取 token 时,我收到 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");
}
}

几乎是从 https://github.com/dsyer/spring-oauth2-integration-tests/blob/master/vanilla/src/main/java/demo/Application.java 复制和粘贴的

对我做错了什么有什么见解吗?

最佳答案

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

关于java - OAuth2 身份验证服务的集成测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33123608/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com