Spring OAuth2针对每个对令牌端点的请求生成访问令牌

Spring OAuth2针对每个对令牌端点的请求生成访问令牌,第1张

Spring OAuth2针对每个对令牌端点的请求生成访问令牌

当我仔细检查时,发现

InMemoryTokenStore
使用
OAuth2Authentication
的哈希字符串作为serveral的键Map。当我使用相同的用户名,client_id,scope ..时,我得到了
key
。因此,这可能会导致一些问题。因此,我认为不赞成使用旧方法。以下是我为避免该问题所做的工作。

创建另一个

AuthenticationKeyGenerator
可以计算唯一密钥的密钥,称为
UniqueAuthenticationKeyGenerator

public class UniqueAuthenticationKeyGenerator implements AuthenticationKeyGenerator {    private static final String CLIENT_ID = "client_id";    private static final String SCOPE = "scope";    private static final String USERNAME = "username";    private static final String UUID_KEY = "uuid";    public String extractKey(OAuth2Authentication authentication) {        Map<String, String> values = new linkedHashMap<String, String>();        OAuth2Request authorizationRequest = authentication.getOAuth2Request();        if (!authentication.isClientonly()) { values.put(USERNAME, authentication.getName());        }        values.put(CLIENT_ID, authorizationRequest.getClientId());        if (authorizationRequest.getScope() != null) { values.put(SCOPE, OAuth2Utils.formatParameterList(authorizationRequest.getScope()));        }        Map<String, Serializable> extentions = authorizationRequest.getExtensions();        String uuid = null;        if (extentions == null) { extentions = new HashMap<String, Serializable>(1); uuid = UUID.randomUUID().toString(); extentions.put(UUID_KEY, uuid);        } else { uuid = (String) extentions.get(UUID_KEY); if (uuid == null) {     uuid = UUID.randomUUID().toString();     extentions.put(UUID_KEY, uuid); }        }        values.put(UUID_KEY, uuid);        MessageDigest digest;        try { digest = MessageDigest.getInstance("MD5");        }        catch (NoSuchAlgorithmException e) { throw new IllegalStateException("MD5 algorithm not available.  Fatal (should be in the JDK).");        }        try { byte[] bytes = digest.digest(values.toString().getBytes("UTF-8")); return String.format("%032x", new BigInteger(1, bytes));        }        catch (UnsupportedEncodingException e) { throw new IllegalStateException("UTF-8 encoding not available.  Fatal (should be in the JDK).");        }    }}

最后,将它们连接起来

<bean id="tokenStore" >    <constructor-arg ref="jdbcTemplate" />    <property name="authenticationKeyGenerator">        <bean  />    </property></bean>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5011488.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存