java - 如何为 token 设置资源 ID?

标签 java spring spring-security oauth-2.0

我尝试使用本指南通过 OAuth 实现 RESTFul web 服务:https://spring.io/guides/tutorials/bookmarks

我可以成功获取 token :

 curl -v -u android-bookmarks:123456 -X POST http://localhost:8080/oauth/token -H "Accept: application/json" -d "password=password&username=User1&grant_type=password&scope=write&client_secret=12345&client_id=android-bookmarks"

响应:

{"access_token":"cdafc45f-924a-4f87-8bd0-e3e2bdffa540","token_type":"bearer","refresh_token":"609efba8-edd3-4ea3-be7b-78e449cec0ef","expires_in":43199,"scope":"write"}* Connection #0 to host localhost left intact

当我尝试访问资源时:

curl -G http://localhost:8080/bookmarks -H "Authorization: Bearer cdafc45f-924a-4f87-8bd0-e3e2bdffa540"

我得到以下响应:

{"error":"access_denied","error_description":"Invalid token does not contain resource id (oauth2-resource)"}

设置资源id的Java类:

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

}

当我将此代码更改为:

clients.inMemory()
        .withClient("android-" + applicationName)
        .authorizedGrantTypes("password", "authorization_code", "refresh_token")
        .authorities("ROLE_USER")
        .scopes("write")
        .secret("123456");

我可以使用前面提到的 (curl) 命令成功访问资源。

最佳答案

事实证明,我必须实现 ResourceServerConfigurerAdapter 接口(interface)。以下实现完美运行:

@Configuration
@EnableResourceServer
public class ResourceServer extends ResourceServerConfigurerAdapter{

    @Override 
    public void configure(HttpSecurity http) throws Exception {
         // @formatter:off
         http
         .requestMatchers().antMatchers("/bookmarks", "/bookmarks/**")    
         .and()
         .authorizeRequests().anyRequest().access("#oauth2.hasScope('write')");
         // @formatter:on
    }

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
         resources.resourceId(OAuth2Configuration.RESOURCE_ID);
    }

}

关于java - 如何为 token 设置资源 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28703847/

相关文章:

java - jbpm5 中的用户任务后工作流程不转发

java - postgresql:无法使用在准备好的语句上采用查询字符串的查询方法

java - GAE Servlet XML 响应。如何?

java - 由 : org. hibernate.LazyInitializationException 引起:未能延迟初始化角色集合:

spring - 如何让 Spring 连接我的 JmsComponent

java - 不是 AppWidgetProvider 中的 onUpdate

java - 验证 Jackson 中的嵌套对象

java - 如何自定义 spring-messaging websockets 用户订阅?

java - Spring Security bcrypt 编码登录不起作用

Grails 2.0 和 PayPal 插件