【问题标题】:Change Response in Oauth2 SpringOauth2 Spring 中的更改响应
【发布时间】:2014-03-31 02:13:41
【问题描述】:

您好,我已经在这个论坛上发布了这个问题。 我也发在这里,希望有更多的机会回复

http://forum.spring.io/forum/spring-projects/security/oauth/745627-response-of-oauth2

我需要在 Oauth 身份验证的 json 响应中添加信息2。现在我的配置返回如下响应:

{"access_token":"523dd467-e5c0-407b-95e4-ea60a403d772",
"token_type":"bearer",
"refresh_token ":"e3378c95-1ebf-419b-bf45-e734d8e94aba",
"expires_in":43199}

但我希望拥有的是像:

{"access_token":"523dd467-e5c0-407b-95e4-ea60a403d772",
"token_type":"bearer",
"refresh_token ":"e3378c95-1ebf-419b-bf45-e734d8e94aba",
"expires_in":43199, "other":"value"}

这很容易吗?

另一个问题是: 如果我想更改 expireTime 我应该实现 TokenStore 接口是正确的吗? 有相关文档吗?

最后一个问题是: 有没有一种简单的方法可以使用 json 格式的凭据(用户名和密码)进行 Oauth2 身份验证?

【问题讨论】:

  • 如果您想获得帮助,请在此处发布完整的问题。

标签: spring web-services rest spring-security oauth-2.0


【解决方案1】:

坦克的黑马。

我只找到了如何在响应中添加其他信息。 Json 格式不是当时的(目前不是高优先级)。我的解决方案如下:

实现 TokenEnhancer 并在 tokenService 配置中添加一个属性:

例子:

<bean id="tokenServices" class="org.springframework.security.oauth2.provider.token.DefaultTokenServices"> <property name="tokenStore" ref="tokenStore" /> <property name="supportRefreshToken" value="true" /> <property name="clientDetailsService" ref="clientDetailsService" /> <property name="tokenEnhancer" ref="tokenEnhancer"/> </bean>

和实施:

    public class MyTokenEnhancer implements TokenEnhancer {

        private List<TokenEnhancer> delegates = Collections.emptyList();

    public void setTokenEnhancers(List<TokenEnhancer> delegates) {
        this.delegates = delegates;
    }

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        DefaultOAuth2AccessToken tempResult = (DefaultOAuth2AccessToken) accessToken;

        final Map<String, Object> additionalInformation = new HashMap<String, Object>();
        final String infoValue = "This is my value"; 

        additionalInformation.put("myInfo", infoValue);
        tempResult.setAdditionalInformation(additionalInformation);

        OAuth2AccessToken result = tempResult;
        for (TokenEnhancer enhancer : delegates) {
            result = enhancer.enhance(result, authentication);
        }
        return result;
    }
}

如果您找到更好/优雅的解决方案....我愿意接受建议

【讨论】:

    【解决方案2】:
    <bean id="tokenServices"        class="org.springframework.security.oauth2.provider.token.DefaultTokenServices">
    <property name="accessTokenValiditySeconds" value="300000"></property>
    <bean/>
    

    使用这个你可以控制过期时间(以秒为单位)

    我也想自定义响应,需要像你上一个问题一样的 json 格式..你有没有遇到任何解决方案..?

    【讨论】:

      猜你喜欢
      • 2020-05-13
      • 2016-04-04
      • 2018-01-11
      • 1970-01-01
      • 2015-07-14
      • 1970-01-01
      • 2019-10-27
      • 2017-01-28
      • 2021-10-08
      相关资源
      最近更新 更多