通过Facebook成功验证后,Spring启动授权服务器重定向问题

时间:2018-03-09 08:46:29

标签: spring-boot oauth-2.0 facebook-oauth

我正在尝试设置一个弹簧启动Authorizaiton服务器,它将具有内部用户登录和OAuth2与facebook。我面临以下问题 -

  1. 如果我创建我的授权服务器SessionCreationPolicy.STATELESS,那么在从facebook控件成功验证后,卡在授权服务器本身(它没有返回到我的客户端应用程序,而如果SessionCreationPolicy.IF_REQUIRED则控制返回到我的客户端应用程序)

  2. 当我使用SessionCreationPolicy.IF_REQUIRED然后控制返回并且我可以执行authorization_code流程但是spring-security-jwt生成的jwt令牌只给出了我在facebook用户ID中的user_name信息(甚至不是名称) )。

  3. 我的本​​地用户身份验证代码可以正常使用代码流,我可以使用自定义令牌增强器自定义我的令牌并添加其他属性但是当我尝试自定义facebook主要对象到我的自定义用户时,我得到错误,该字符串可以不能转换为自定义用户对象。

  4. 请参阅回购详情 - dev repo

    我使用下面的代码进行setup / jwt生成 -

    @EnableOAuth2Client // for Oauth setup
    
    // jwt enhancer which gives me error when principal is converted to custom user 
    class CustomTokenEnhancer implements TokenEnhancer {
        @Override
        public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
            Map<String, Object> additionalInfo = new HashMap<>();
            Authentication auth = authentication.getUserAuthentication();
           /* additionalInfo.put("email", ((CustomPrincipal)auth.getPrincipal()).getEmail());
            additionalInfo.put("roles", ((CustomPrincipal)auth.getPrincipal()).getRoles());
            additionalInfo.put("id", ((CustomPrincipal)auth.getPrincipal()).getId());*/
            ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
            return accessToken;
        }
    }
    

    //我使用的SSO过滤器 -

    private Filter ssoFilter(ClientResources client, String path) {
    
    
        OAuth2ClientAuthenticationProcessingFilter filter = new OAuth2ClientAuthenticationProcessingFilter(path);
        OAuth2RestTemplate template = new OAuth2RestTemplate(client.getClient(), oauth2ClientContext);
        filter.setRestTemplate(template);
        UserInfoTokenServices tokenServices = new UserInfoTokenServices(
                client.getResource().getUserInfoUri(), client.getClient().getClientId());
        tokenServices.setRestTemplate(template);
        filter.setTokenServices(tokenServices);
        // filter.setAuthenticationSuccessHandler(authenticationHandler);
        return filter;
    }
    

    感谢任何帮助。 谢谢!

1 个答案:

答案 0 :(得分:0)

我得到了第2和第3点的解释 -

自从Facebook成功验证后; Spring启动授权服务器将身份验证对象存储为以下格式 -

 {
"authorities": [
  {
    "authority": "ROLE_USER"
  }
],
"details": {
  "remoteAddress": "0:0:0:0:0:0:0:1",
  "sessionId": "xyzxyzxyzxyzxyz",
  "tokenValue": "xyzxyzxyzxyzxyz",
  "tokenType": "bearer",
  "decodedDetails": null
},
"authenticated": true,
"userAuthentication": {
  "authorities": [
    {
      "authority": "ROLE_USER"
    }
  ],
  "details": {
    "id": "xyzxyzxyzxyzxyz",
    "name": "xyzxyzxyzxyzxyz",
    "email": "xyzxyzxyzxyzxyz"
  },
  "authenticated": true,
  "principal": "xyzxyzxyzxyzxyz",
  "credentials": "N/A",
  "name": "xyzxyzxyzxyzxyz"
},
"principal": "xyzxyzxyzxyzxyz",
"oauth2Request": {
  "clientId": "xyzxyzxyzxyzxyz",
  "scope": [],
  "requestParameters": {},
  "resourceIds": [],
  "authorities": [],
  "approved": true,
  "refresh": false,
  "redirectUri": null,
  "responseTypes": [],
  "extensions": {},
  "grantType": null,
  "refreshTokenRequest": null
},

所以当我将我的主体转换为自定义主体时,我得到了错误,因为上面的模型主体只是一个字符串。

注意 - 我仍然不知道如何将上述身份验证对象自定义为我的customuser对象。

相关问题