Spring 客户端授权不适用于单元测试
好的,首先要知道的是,我可以按预期使用实际的web客户机。我可以注册一个用户并登录它 然而,当我尝试实现一些单元测试时,事情对我不起作用。这就是我在单元测试中创建用户的方式:Spring 客户端授权不适用于单元测试,spring,spring-boot,spring-junit,Spring,Spring Boot,Spring Junit,好的,首先要知道的是,我可以按预期使用实际的web客户机。我可以注册一个用户并登录它 然而,当我尝试实现一些单元测试时,事情对我不起作用。这就是我在单元测试中创建用户的方式: @Autowired private TestRestTemplate template; @Value("${security.jwt.client-id}") private String clientId; @Value("${security.jwt.client-secret}") private Strin
@Autowired
private TestRestTemplate template;
@Value("${security.jwt.client-id}")
private String clientId;
@Value("${security.jwt.client-secret}")
private String clientSecret;
private static String USERS_ENDPOINT = "http://localhost:8081/users/";
public AppUser registerUser(String username, String password) {
AppUser appUser = new AppUser();
appUser.setUsername(username);
appUser.setPassword(password);
ResponseEntity<AppUser> appUserResponseEntity = template.withBasicAuth(clientId, clientSecret)
.postForEntity(USERS_ENDPOINT, appUser, AppUser.class);
AppUser registeredAppUser = appUserResponseEntity.getBody();
assertNotNull("Trying to register new user but the user ID is null which indicates it didn't work.",
registeredAppUser.getId());
return registeredAppUser;
}
您可以在下面找到OAuth2配置的myAuthorizationServerConfigurerAdapter
@Configuration
@EnableAuthorizationServer
@EnableResourceServer
public class OAuth2Configuration extends AuthorizationServerConfigurerAdapter {
@Configuration
@EnableResourceServer
public class ResourceServer extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.requestMatchers().antMatchers("/**")
.and()
.authorizeRequests().anyRequest().access("#oauth2.hasScope('write')");
}
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.resourceId(resourceIds);
}
}
@Value("${security.jwt.client-id}")
private String clientId;
@Value("${security.jwt.client-secret}")
private String clientSecret;
@Value("${security.jwt.grant-type}")
private String grantType;
@Value("${security.jwt.scope-read}")
private String scopeRead;
@Value("${security.jwt.scope-write}")
private String scopeWrite;
@Value("${security.jwt.resource-ids}")
private String resourceIds;
@Autowired
private TokenStore tokenStore;
@Autowired
private JwtAccessTokenConverter accessTokenConverter;
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(ClientDetailsServiceConfigurer configurer) throws Exception {
configurer
.inMemory()
.withClient(clientId)
.secret(clientSecret)
.authorizedGrantTypes(grantType)
.scopes(scopeRead, scopeWrite)
.resourceIds(resourceIds);
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
TokenEnhancerChain enhancerChain = new TokenEnhancerChain();
enhancerChain.setTokenEnhancers(Arrays.asList(accessTokenConverter));
endpoints.tokenStore(tokenStore)
.accessTokenConverter(accessTokenConverter)
.tokenEnhancer(enhancerChain)
.authenticationManager(authenticationManager);
}
}
我不知道这为什么不起作用。有人知道问题出在哪里吗
我觉得奇怪的是,它在请求匿名用户的写访问权限时失败了
WebSecurity
实际上应该忽略/users
端点:
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/pub/**", "/users");
}
所以我不太清楚为什么安全过滤链在这里是活跃的
更新: 我现在尝试的是在8080上启动服务器,并在同一调试会话中使用
testrestemplate
,并尝试将AppUser
发布到8080和8081-看一下:
正如您所看到的,相同的调用在8080(一个实际运行的服务器实例)上工作,但在8081上不工作——服务器实例是为单元测试而启动的
很明显,我的配置或其他方面存在问题,但我无法确定。我没有更多关于这一点的词语:
http://localhost:8081/users
你是想注册还是验证用户?@Marcosmia我刚刚发现了问题,我不知道下一步该怎么办^^我简直说不出话来,因为这一次花费了我太多的时间。。
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/pub/**", "/users");
}