首页 文章

Spring Security Oauth2:处理过期的AccessToken的流程

提问于
浏览
5

我只是Spring Security Oauth2的初学者 . 我尝试创建授权服务器和资源服务器(分离并连接到JDBC),目的是进行单点登录 . 我从Authorization Server获取accessstoken和refreshtoken的流程成功 . 我的accesstoken始终用作访问Resouce Server的参数,这将给出响应 .

for example http://127.0.0.1:8080/Resource/res/staff?access_token=xxxxxxxxxxxxxxxxxxxxx

我的问题是,如果accessstoken过期,spring安全性将阻止访问页面并给出错误异常 . 当我必须使用refreshtoken获取新令牌?或者我的流程错了?还有其他流程可以更新accessstoken吗?

谢谢

Edited:

仅供参考:我想使用Spring Security Oauth2制作SSO . 我有几个Apps Server(使用Spring Framework),我想制作一个负责管理登录的服务器 . 我想让Apps Server成为资源服务器(也是客户端)所以我用Spring Security Oauth2创建了一个Authorization Server . 想要访问受保护资源服务器的用户必须登录授权服务器(资源服务器授权给授权服务器) . 它将获得一个代码,然后资源服务器将使用accessToken和refreshToken交换此代码 . 这种流程是成功的 .

我也可以使用Authorization Server提供的refreshToken请求新的accessToken . 但我无法调用此过程,因为如果我访问url映射,之前spring安全性阻止访问并返回无效标记错误 .

我该如何解决缺失的链接?

Updated:

这是我的授权服务器配置:

@Configuration
public class Oauth2AuthorizationServer {

 @Configuration
 @EnableAuthorizationServer
 protected static class AuthorizationServerConfiguration extends
        AuthorizationServerConfigurerAdapter {

    @Autowired
    @Qualifier("authenticationManagerBean")
    private AuthenticationManager authenticationManager;

    @Autowired
    DataSource dataSource;


    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints)
            throws Exception {
        endpoints
                .tokenStore(new JdbcTokenStore(dataSource))
                .authenticationManager(authenticationManager);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
        oauthServer.tokenKeyAccess("isAnonymous() || permitAll()").checkTokenAccess("permitAll()");
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients
                .jdbc(dataSource);
    }
 }
}

这是我的资源服务器配置(也作为客户端)

@Configuration
public class Oauth2ResourceServer {

private static final String RESOURCE_ID = "test";

 @Configuration @Order(10)
 protected static class NonOauthResources extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/api/halo").permitAll()
                .antMatchers("/api/state/**").permitAll()
                .antMatchers("/**").permitAll()
                .and().anonymous();
    }
 }

 @Configuration
 @EnableResourceServer
 protected static class ResourceServerConfiguration extends
        ResourceServerConfigurerAdapter {

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        RemoteTokenServices tokenService = new RemoteTokenServices();
        tokenService.setClientId("jsclient");
        tokenService.setClientSecret("jspasswd");
        tokenService.setCheckTokenEndpointUrl("http://localhost:8084/Server2Auth/oauth/check_token");

        resources
                .resourceId(RESOURCE_ID)
                .tokenServices(tokenService);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .filterSecurityInterceptorOncePerRequest(true)
                .antMatchers("/res/staff").hasRole("STAFF")
                .antMatchers("/res/client").access("#oauth2.hasScope('trust')")
                .antMatchers("/res/admin").hasRole("ADMIN")
                .and()
                .exceptionHandling().accessDeniedPage("/403");
    }

 }

}

资源(也是客户端)请求授权:

curl -X POST -vu clientauthcode:123456 http://localhost:10000/auth-server/oauth/token -d "client_id=clientauthcode&grant_type=refresh_token&refresh_token=436761f1-2f26-412b-ab0f-bbf2cd7459c4"

来自Authorize Server的反馈:

http://localhost:10001/resource-server/api/state/new?code=8OppiR

资源(作为客户端)将代码交换到授权服务器:

curl -X POST -vu clientauthcode:123456 http://localhost:10000/auth-server/oauth/token -H "Accept: application/json" -d "grant_type=authorization_code&code=iMAtdP&redirect_uri=http://localhost:10001/resource-server/api/state/new"

来自Authorize Server的反馈:

{
    "access_token":"08664d93-41e3-473c-b5d2-f2b30afe7053",
    "token_type":"bearer",
    "refresh_token":"436761f1-2f26-412b-ab0f-bbf2cd7459c4",
    "expires_in":43199,
    "scope":"write read"
}

资源(作为客户端)访问URL本身

curl http://localhost:10001/resource-server/api/admin?access_token=08664d93-41e3-473c-b5d2-f2b30afe7053

使用刷新令牌请求新访问toke

curl -X POST -vu clientauthcode:123456 http://localhost:10000/auth-server/oauth/token -d "client_id=clientauthcode&grant_type=refresh_token&refresh_token=436761f1-2f26-412b-ab0f-bbf2cd7459c4"

1 回答

  • 2

    The OAuth2 Spec有一个关于刷新访问令牌的部分 . 它在Spring OAuth中以非常标准的方式实现(您只需将刷新令牌发布到/ token endpoints ) .

    对于SSO,BTW通常不需要资源服务器 . 但这是一个不同的问题 .

相关问题