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

Java Spring Security Oauth2:处理过期AccessToken的流程,java,spring,spring-security,spring-security-oauth2,Java,Spring,Spring Security,Spring Security Oauth2,我只是一个春季安全Oauth2的初学者。 我尝试将授权服务器和资源服务器(分离并连接到JDBC)连接起来,目的是实现单点登录。 My flow成功从授权服务器获取accesstoken和refreshtoken。我的accesstoken总是用作访问资源服务器的参数,这是一个返回响应 for example http://127.0.0.1:8080/Resource/res/staff?access_token=xxxxxxxxxxxxxxxxxxxxx 我的问题是,如果accesstoke

我只是一个春季安全Oauth2的初学者。 我尝试将授权服务器和资源服务器(分离并连接到JDBC)连接起来,目的是实现单点登录。 My flow成功从授权服务器获取accesstoken和refreshtoken。我的accesstoken总是用作访问资源服务器的参数,这是一个返回响应

for example http://127.0.0.1:8080/Resource/res/staff?access_token=xxxxxxxxxxxxxxxxxxxxx
我的问题是,如果accesstoken过期,spring安全性将阻止访问该页面并给出错误异常。我何时必须使用刷新令牌来获取新令牌? 还是我的流程错了?是否有其他流程来续订accesstoken

谢谢

编辑:

供参考: 我想使用SpringSecurityOAuth2实现SSO。我有几个应用服务器(使用Spring框架),我想让一个服务器负责管理登录。我想让应用服务器变成资源服务器(也是客户端) 因此,我使用Spring Security Oauth2创建了一个授权服务器。要访问受保护资源服务器的用户必须登录到授权服务器(资源服务器授权给授权服务器)。它将获得一个代码,然后资源服务器将该代码与accessToken和refreshToken交换。这种流动就是成功

我还可以使用授权服务器提供的刷新令牌请求新的accessToken。 但是我不能调用这个过程,因为如果我访问url映射,以前spring安全性会阻止访问并返回无效令牌错误

我如何解决丢失的链接

更新:

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

@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);
    }
 }
}
这是我的资源服务器配置(也作为客户端)

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

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"
授权服务器的反馈:

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"
{
    "access_token":"08664d93-41e3-473c-b5d2-f2b30afe7053",
    "token_type":"bearer",
    "refresh_token":"436761f1-2f26-412b-ab0f-bbf2cd7459c4",
    "expires_in":43199,
    "scope":"write read"
}
资源(作为客户端)交换代码以授权服务器:

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"
{
    "access_token":"08664d93-41e3-473c-b5d2-f2b30afe7053",
    "token_type":"bearer",
    "refresh_token":"436761f1-2f26-412b-ab0f-bbf2cd7459c4",
    "expires_in":43199,
    "scope":"write read"
}
授权服务器的反馈:

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"
{
    "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
使用刷新令牌请求新的访问托克

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"
有一个关于刷新访问令牌的部分。它在SpringOAuth中以相当标准的方式实现(您只需将刷新令牌发布到/token端点)


顺便说一句,对于SSO,您通常不需要资源服务器。但这是另一个问题。

没问题。很乐意帮忙。如果我的回答有帮助,请接受它,这样其他人就会知道它可能会帮助他们。对不起,我也是Oauth2的初学者。在Ouath2规范中说“授权服务器可能会发出一个新的刷新令牌”,SpringSecurityOAuth2如何给出这个问题?我可以请求使用refreshtoken获取新的accesstoken,但我只知道accesstoken在用于访问某些资源时过期。仅供参考,我的SSO将授权和资源服务器分开,因为在此之前,我有几个资源服务器,因此我认为我可以将授权服务器与我的资源分开。如果启用它们,Spring Auth Server会发出刷新令牌(我忘了,这可能不是默认设置)。当一个访问令牌被发出时,客户机知道它大约什么时候会过期,所以它可以使用该信息或者只是等待401。我只是启用刷新令牌。我想等待401,然后我可以调用该过程来使用刷新令牌请求新的访问令牌。但我不知道如何制作401处理程序?如果您使用
OAuth2RestTemplate
它应该是自动的。否则,我想请查看源代码并复制模式。无关注释:您应该将访问令牌放在头中(作为URI查询参数不安全)。另一个无关注释:您不需要令牌密钥端点,因此不要触摸它;在生产服务器中,不应使用
permitAll()
公开检查令牌终结点(请尝试
isAuthenticated()
)。有些相关的注释(因为这会使问题产生误导)说明您没有在此处执行任何SSO。这是典型的身份验证/资源服务器,在资源服务器中具有基于令牌的安全性。我认为用于授权代码的curl命令是错误的(复制粘贴错误)?可能是clientID、clientSecret或代码不同。我可以使用curl(通过更改clientID、clientSecret和代码)