Invalid XSRF token at /oauth/token

后端 未结 2 736
陌清茗
陌清茗 2020-12-10 12:50

Complete code for a Spring OAuth2 implementation of Multi-Factor Authentication has been uploaded to a file sharing site at this link. Instructions are given below to recre

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-10 13:05

    Your CustomOAuth2RequestFactory is putting the previous request in-place of the current request. However, you are not updating the XSRF token in the old request when you make this switch. Here is what I would suggest for the updated CustomOAuth2Request:

    @Override
    public AuthorizationRequest createAuthorizationRequest(Map authorizationParameters) {
        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpSession session = attr.getRequest().getSession(false);
        if (session != null) {
            AuthorizationRequest authorizationRequest = (AuthorizationRequest) session.getAttribute(SAVED_AUTHORIZATION_REQUEST_SESSION_ATTRIBUTE_NAME);
            if (authorizationRequest != null) {
                session.removeAttribute(SAVED_AUTHORIZATION_REQUEST_SESSION_ATTRIBUTE_NAME);
    //UPDATE THE STATE VARIABLE WITH THE NEW TOKEN.  THIS PART IS NEW
                CsrfToken csrf = (CsrfToken) attr.getRequest().getAttribute(CsrfToken.class.getName());
                String attrToken = csrf.getToken();
                authorizationRequest.setState(attrToken);                
    
                return authorizationRequest;
            }
        }
    
        return super.createAuthorizationRequest(authorizationParameters);
    }
    

    I am revisiting this because my initial answer draft got downvoted. This version is further along the same path, which I believe is the right avenue of approach.

提交回复
热议问题