在资源服务配置中重载资源配置方法
···
@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
// @formatter:off
http.antMatcher("/client/userinfo").authorizeRequests().anyRequest().authenticated();
// @formatter:on
}
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.resourceId("authorize-server"); //重点,设置资源id
}
}
···
把资源id加到clientdetails中
···
@Configuration
@EnableAuthorizationServer
public class OAuth2Configuration extends AuthorizationServerConfigurerAdapter {
public static final String RESOURCE_ID = "bookmarks";
@Autowired
AuthenticationManagerBuilder authenticationManager;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints)
throws Exception {
endpoints.authenticationManager(new AuthenticationManager() {
@Override
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
return authenticationManager.getOrBuild().authenticate(
authentication);
}
});
}
@Override
public void configure(ClientDetailsServiceConfigurer clients)
throws Exception {
clients.inMemory()
.withClient("android-" + RESOURCE_ID)
.authorizedGrantTypes("password", "authorization_code", "refresh_token")
.authorities("ROLE_USER")
.scopes("write")
.secret("123456")
.resourceIds(“authorize-server”); //注意这里
}
}
···
否则会报以下错误
{"error":"access_denied",
"error_description":"Invalid token does not contain resource id (oauth2-resource)"
}
参考源码见 https://gitee.com/nnsword/wy2-cloud/tree/master/wy2-springcloud-sample1x
- 如果觉得有帮忙请推荐给朋友,最好能送颗星,谢谢