- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在研究使用 oauth2 保护的 Spring REST Web 服务。我想使用两个不同的应用程序将 AuthorizationServer 与 ResourceServer 分开 - AuthorizationServer 是 oauth2 SSO(单点登录),ResourceServer 是业务 REST 服务的第二个应用程序。这样我就无法使用内存 token 存储,因为我的应用程序将驻留在不同的主机上。我需要一些 TokenStore 的共享机制,例如数据库。 Spring 框架中有 JdbcTokenStore 实现,但在我的项目中我使用 Neo4J 图形数据库。
所以,我有一个问题 - 我应该尝试在 Neo4J 中存储 oauth2 token (例如创建 Neo4JTokenStore 的自定义实现(已经存在?))还是在其他数据库中?此案例的最佳实践是什么?
最佳答案
我成功实现了 Neo4j 的 token 存储。
AbstractDomainEntity.java
@NodeEntity
public abstract class AbstractDomainEntity implements DomainEntity {
private static final long serialVersionUID = 1L;
@GraphId
private Long nodeId;
@Indexed(unique=true)
String id;
private Date createdDate;
@Indexed
private Date lastModifiedDate;
@Indexed
private String createduser;
private String lastModifieduser;
protected AbstractDomainEntity(String id) {
this.id = id;
}
protected AbstractDomainEntity() {
}
//gets sets
OAuth2AuthenticationAccessToken.java
@NodeEntity
public class OAuth2AuthenticationAccessToken extends AbstractDomainEntity {
private static final long serialVersionUID = -3919074495651349876L;
@Indexed
private String tokenId;
@GraphProperty(propertyType = String.class)
private OAuth2AccessToken oAuth2AccessToken;
@Indexed
private String authenticationId;
@Indexed
private String userName;
@Indexed
private String clientId;
@GraphProperty(propertyType = String.class)
private OAuth2Authentication authentication;
@Indexed
private String refreshToken;
public OAuth2AuthenticationAccessToken(){
super();
}
public OAuth2AuthenticationAccessToken(final OAuth2AccessToken oAuth2AccessToken, final OAuth2Authentication authentication, final String authenticationId) {
super(UUID.randomUUID().toString());
this.tokenId = oAuth2AccessToken.getValue();
this.oAuth2AccessToken = oAuth2AccessToken;
this.authenticationId = authenticationId;
this.userName = authentication.getName();
this.clientId = authentication.getOAuth2Request().getClientId();
this.authentication = authentication;
this.refreshToken = oAuth2AccessToken.getRefreshToken() != null ?
oAuth2AccessToken.getRefreshToken().getValue() : null;
}
//getters setters
OAuth2AuthenticationRefreshToken.java
@NodeEntity
public class OAuth2AuthenticationRefreshToken extends AbstractDomainEntity {
private static final long serialVersionUID = -3269934495553717378L;
@Indexed
private String tokenId;
@GraphProperty(propertyType = String.class)
private OAuth2RefreshToken oAuth2RefreshToken;
@GraphProperty(propertyType = String.class)
private OAuth2Authentication authentication;
public OAuth2AuthenticationRefreshToken(){
super();
}
public OAuth2AuthenticationRefreshToken(final OAuth2RefreshToken oAuth2RefreshToken, final OAuth2Authentication authentication) {
super(UUID.randomUUID().toString());
this.oAuth2RefreshToken = oAuth2RefreshToken;
this.authentication = authentication;
this.tokenId = oAuth2RefreshToken.getValue();
}
//gets sets
OAuth2AccessTokenRepository.java
public interface OAuth2AccessTokenRepository extends GraphRepository<OAuth2AuthenticationAccessToken>, CypherDslRepository<OAuth2AuthenticationAccessToken>, OAuth2AccessTokenRepositoryCustom {
public OAuth2AuthenticationAccessToken findByTokenId(String tokenId);
public OAuth2AuthenticationAccessToken findByRefreshToken(String refreshToken);
public OAuth2AuthenticationAccessToken findByAuthenticationId(String authenticationId);
public List<OAuth2AuthenticationAccessToken> findByClientIdAndUserName(String clientId, String userName);
public List<OAuth2AuthenticationAccessToken> findByClientId(String clientId);
}
OAuth2RefreshTokenRepository.java
public interface OAuth2RefreshTokenRepository extends GraphRepository<OAuth2AuthenticationRefreshToken>, CypherDslRepository<OAuth2AuthenticationRefreshToken>, OAuth2RefreshTokenRepositoryCustom {
public OAuth2AuthenticationRefreshToken findByTokenId(String tokenId);
}
OAuth2TokenStoreRepositoryImpl.java
public class OAuth2TokenStoreRepositoryImpl implements TokenStore {
@Autowired
private OAuth2AccessTokenRepository oAuth2AccessTokenRepository;
@Autowired
private OAuth2RefreshTokenRepository oAuth2RefreshTokenRepository;
private AuthenticationKeyGenerator authenticationKeyGenerator = new DefaultAuthenticationKeyGenerator();
/* (non-Javadoc)
* @see org.springframework.security.oauth2.provider.token.TokenStore#readAuthentication(org.springframework.security.oauth2.common.OAuth2AccessToken)
*/
@Override
public OAuth2Authentication readAuthentication(OAuth2AccessToken token) {
return readAuthentication(token.getValue());
}
/* (non-Javadoc)
* @see org.springframework.security.oauth2.provider.token.TokenStore#readAuthentication(java.lang.String)
*/
@Override
public OAuth2Authentication readAuthentication(String tokenId) {
OAuth2AuthenticationAccessToken accessToken = oAuth2AccessTokenRepository.findByTokenId(tokenId);
OAuth2Authentication oauth2Authentication = null;
if(accessToken != null){
oauth2Authentication = accessToken.getAuthentication();
}
return oauth2Authentication;
}
/* (non-Javadoc)
* @see org.springframework.security.oauth2.provider.token.TokenStore#storeAccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken, org.springframework.security.oauth2.provider.OAuth2Authentication)
*/
@Override
public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
// remove existing token & add new
removeAccessToken(token);
OAuth2AuthenticationAccessToken oAuth2AuthenticationAccessToken = new OAuth2AuthenticationAccessToken(token,
authentication, authenticationKeyGenerator.extractKey(authentication));
oAuth2AccessTokenRepository.save(oAuth2AuthenticationAccessToken);
}
/* (non-Javadoc)
* @see org.springframework.security.oauth2.provider.token.TokenStore#readAccessToken(java.lang.String)
*/
@Override
public OAuth2AccessToken readAccessToken(String tokenValue) {
OAuth2AuthenticationAccessToken token = oAuth2AccessTokenRepository.findByTokenId(tokenValue);
if(token == null) {
return null; //let spring security handle the invalid token
}
OAuth2AccessToken accessToken = token.getoAuth2AccessToken();
return accessToken;
}
/* (non-Javadoc)
* @see org.springframework.security.oauth2.provider.token.TokenStore#removeAccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken)
*/
@Override
public void removeAccessToken(OAuth2AccessToken token) {
OAuth2AuthenticationAccessToken accessToken = oAuth2AccessTokenRepository.findByTokenId(token.getValue());
if(accessToken != null) {
oAuth2AccessTokenRepository.delete(accessToken);
}
}
/* (non-Javadoc)
* @see org.springframework.security.oauth2.provider.token.TokenStore#storeRefreshToken(org.springframework.security.oauth2.common.OAuth2RefreshToken, org.springframework.security.oauth2.provider.OAuth2Authentication)
*/
@Override
public void storeRefreshToken(OAuth2RefreshToken refreshToken, OAuth2Authentication authentication) {
oAuth2RefreshTokenRepository.save(new OAuth2AuthenticationRefreshToken(refreshToken, authentication));
}
/* (non-Javadoc)
* @see org.springframework.security.oauth2.provider.token.TokenStore#readRefreshToken(java.lang.String)
*/
@Override
public OAuth2RefreshToken readRefreshToken(String tokenValue) {
OAuth2AuthenticationRefreshToken refreshAuth = oAuth2RefreshTokenRepository.findByTokenId(tokenValue);
OAuth2RefreshToken refreshToken = null;
if(refreshAuth != null){
refreshToken = refreshAuth.getoAuth2RefreshToken();
}
return refreshToken;
}
/* (non-Javadoc)
* @see org.springframework.security.oauth2.provider.token.TokenStore#readAuthenticationForRefreshToken(org.springframework.security.oauth2.common.OAuth2RefreshToken)
*/
@Override
public OAuth2Authentication readAuthenticationForRefreshToken( OAuth2RefreshToken token) {
OAuth2AuthenticationRefreshToken refreshAuth = oAuth2RefreshTokenRepository.findByTokenId(token.getValue());
OAuth2Authentication oAuth2Authentication = null;
if(refreshAuth != null){
oAuth2Authentication = refreshAuth.getAuthentication();
}
return oAuth2Authentication;
}
/* (non-Javadoc)
* @see org.springframework.security.oauth2.provider.token.TokenStore#removeRefreshToken(org.springframework.security.oauth2.common.OAuth2RefreshToken)
*/
@Override
public void removeRefreshToken(OAuth2RefreshToken token) {
OAuth2AuthenticationRefreshToken refreshAuth = oAuth2RefreshTokenRepository.findByTokenId(token.getValue());
if(refreshAuth != null){
oAuth2RefreshTokenRepository.delete(refreshAuth);
}
}
/* (non-Javadoc)
* @see org.springframework.security.oauth2.provider.token.TokenStore#removeAccessTokenUsingRefreshToken(org.springframework.security.oauth2.common.OAuth2RefreshToken)
*/
@Override
public void removeAccessTokenUsingRefreshToken(OAuth2RefreshToken refreshToken) {
OAuth2AuthenticationAccessToken accessToken = oAuth2AccessTokenRepository.findByRefreshToken(refreshToken.getValue());
if(accessToken != null){
oAuth2AccessTokenRepository.delete(accessToken);
}
}
/* (non-Javadoc)
* @see org.springframework.security.oauth2.provider.token.TokenStore#getAccessToken(org.springframework.security.oauth2.provider.OAuth2Authentication)
*/
@Override
public OAuth2AccessToken getAccessToken(OAuth2Authentication authentication) {
OAuth2AuthenticationAccessToken token = oAuth2AccessTokenRepository.findByAuthenticationId(authenticationKeyGenerator.extractKey(authentication));
return token == null ? null : token.getoAuth2AccessToken();
}
/* (non-Javadoc)
* @see org.springframework.security.oauth2.provider.token.TokenStore#findTokensByClientIdAndUserName(java.lang.String, java.lang.String)
*/
@Override
public Collection<OAuth2AccessToken> findTokensByClientIdAndUserName(String clientId, String userName) {
List<OAuth2AuthenticationAccessToken> tokens = oAuth2AccessTokenRepository.findByClientIdAndUserName(clientId, userName);
return extractAccessTokens(tokens);
}
/* (non-Javadoc)
* @see org.springframework.security.oauth2.provider.token.TokenStore#findTokensByClientId(java.lang.String)
*/
@Override
public Collection<OAuth2AccessToken> findTokensByClientId(String clientId) {
List<OAuth2AuthenticationAccessToken> tokens = oAuth2AccessTokenRepository.findByClientId(clientId);
return extractAccessTokens(tokens);
}
private Collection<OAuth2AccessToken> extractAccessTokens(List<OAuth2AuthenticationAccessToken> tokens) {
List<OAuth2AccessToken> accessTokens = new ArrayList<OAuth2AccessToken>();
for(OAuth2AuthenticationAccessToken token : tokens) {
accessTokens.add(token.getoAuth2AccessToken());
}
return accessTokens;
}
}
我使用 GraphProperty 将 OAuth2AuthenticationAccessToken.java 和 OAuth2AuthenticationAccessToken.java 中的两个对象 OAuth2AccessToken 和 OAuth2Authentication 的 String 转换如下。
@GraphProperty(propertyType = String.class)
private OAuth2AccessToken oAuth2AccessToken;
@GraphProperty(propertyType = String.class)
private OAuth2Authentication authentication;
所以,我为它们实现了四个转换器
public class OAuth2AccessTokenToStringConverter implements Converter<OAuth2AccessToken, String> {
@Override
public String convert(final OAuth2AccessToken source) {
//some code that takes your object and returns a String
ObjectMapper mapper = new ObjectMapper();
String json = null;
try {
json = mapper.writeValueAsString(source);
//remove white space
json = json.replace("\"scope\":\" ","\"scope\":\"");
} catch (IOException e) {
e.printStackTrace();
}
return json;
}
}
public class OAuth2AuthenticationToStringConverter implements Converter<OAuth2Authentication, String> {
@Override
public String convert(final OAuth2Authentication source) {
//some code that takes your object and returns a String
ObjectMapper mapper = new ObjectMapper();
String json = null;
try {
json = mapper.writeValueAsString(source);
} catch (IOException e) {
e.printStackTrace();
}
return json;
}
}
public class StringToOAuth2AccessTokenConverter implements Converter<String, OAuth2AccessToken> {
@Override
public OAuth2AccessToken convert(final String source) {
//some code that takes a String and returns an object of your type
ObjectMapper mapper = new ObjectMapper();
OAuth2AccessToken deserialised = null;
try {
deserialised = mapper.readValue(source, OAuth2AccessToken.class);
} catch (IOException e) {
e.printStackTrace();
}
return deserialised;
}
}
public class StringToOAuth2AuthenticationConverter implements Converter<String, OAuth2Authentication> {
@Override
public OAuth2Authentication convert(final String source) {
// some code that takes a String and returns an object of your type
OAuth2Authentication oAuth2Authentication = null;
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree(source);
OAuth2Request oAuth2Request = getOAuth2Request(rootNode.get("oauth2Request"), mapper);
JsonNode userAuthentication = rootNode.get("userAuthentication");
JsonNode principal = userAuthentication.get("principal");
UserAccount userAccount = mapper.readValue(principal.get("userAccount"), UserAccount.class);
CTGUserDetails userDetails = new CTGUserDetails(userAccount);
List<Map<String, String>> authorities = mapper.readValue(userAuthentication.get("authorities"), new TypeReference<List<Map<String, String>>>() {});
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, getAuthorities(authorities));
oAuth2Authentication = new OAuth2Authentication(oAuth2Request, authentication);
} catch (IOException e) {
e.printStackTrace();
}
return oAuth2Authentication;
}
private OAuth2Request getOAuth2Request(final JsonNode jsonNode, ObjectMapper mapper) throws JsonParseException, JsonMappingException, IOException{
Map<String, String> requestParameters = mapper.readValue(jsonNode.get("requestParameters"),new TypeReference<Map<String, String>>() {});
String clientId = jsonNode.get("clientId").getTextValue();
List<Map<String, String>> authorities = mapper.readValue(jsonNode.get("authorities"), new TypeReference<List<Map<String, String>>>() {});
Set<String> scope = mapper.readValue(jsonNode.get("scope"), new TypeReference<HashSet<String>>() {});
Set<String> resourceIds = mapper.readValue(jsonNode.get("resourceIds"), new TypeReference<HashSet<String>>() {});
return new OAuth2Request(requestParameters, clientId, getAuthorities(authorities) , true, scope, resourceIds, null, null, null);
}
private Collection<? extends GrantedAuthority> getAuthorities(
List<Map<String, String>> authorities) {
List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>(authorities.size());
for (Map<String, String> authority : authorities) {
grantedAuthorities.add(new SimpleGrantedAuthority(authority.get("authority")));
}
return grantedAuthorities;
}
}
在bean xml中,我向Spring声明如下:
<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.<yourpackage>.convertor.OAuth2AuthenticationToStringConverter"/>
<bean class="com.<yourpackage>.convertor.OAuth2AccessTokenToStringConverter"/>
<bean class="com.<yourpackage>.convertor.StringToOAuth2AccessTokenConverter"/>
<bean class="com.<yourpackage>.convertor.StringToOAuth2AuthenticationConverter"/>
</set>
</property>
</bean>
结束。 :) 欢迎任何反馈。
关于java - Neo4J TokenStore Spring oauth2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28399712/
在C语言中,当有变量(假设都是int)i小于j时,我们可以用等式 i^=j^=i^=j 交换两个变量的值。例如,令int i = 3,j = 5;在计算 i^=j^=i^=j 之后,我有 i = 5,
我为以下问题编写了以下代码: 给定一个由 N 个正整数组成的序列 A,编写一个程序来查找满足 i > A[j]A[i](A[i] 的 A[j] 次方 > A[j] 的 A[i] 次方)。 我的代码通过
这个表达式是从左到右解析的吗?我试图解释解析的结果,但最后的结果是错误的。 int j=10, k=10; j+=j-=j*=j; //j=j+(j-=j*=j)=j+(j-j*j) k+=k*=
给定一个整数数组 A ,我试图找出在给定位置 j ,A[j] 从每个 i=0 到 i=j 在 A 中出现了多少次。我设计了一个如下所示的解决方案 map CF[400005]; for(int i=0
你能帮我算法吗: 给定 2 个相同大小的数组 a[]和 b[]具有大于或等于 1 的整数。 查找不相等的索引 i和 j ( i != j ) 使得值 -max(a[i]*b[i] + a[i] * b
每次用J的M.副词,性能显着下降。因为我怀疑艾弗森和许比我聪明得多,我一定是做错了什么。 考虑 Collatz conjecture .这里似乎有各种各样的内存机会,但不管我放在哪里M. ,性能太差了
假设一个包含各种类型的盒装矩阵: matrix =: ('abc';'defgh';23),:('foo';'bar';45) matrix +---+-----+--+|abc|defgh|23|+
是否有可能对于两个正整数 i 和 j,(-i)/j 不等于 -(i/j)?我不知道这是否可能......我认为这将是关于位的东西,或者 char 类型的溢出或其他东西,但我找不到它。有什么想法吗? 最
假设两个不同大小的数组: N0 =: i. 50 N1 =: i. 500 应该有一种方法可以获得唯一的对,只需将两者结合起来即可。我发现的“最简单”是: ]$R =: |:,"2 |: (,.N0)
我是 J 的新用户,我只是想知道 J 包中是否实现了三次样条插值方法? 最佳答案 我自己不熟悉,但是我确实安装了所有的包,所以 $ rg -l -i spline /usr/share/j/9.02
在 Q/kdb 中,您可以使用 ': 轻松修改动词,它代表每个优先级。它会将动词应用于一个元素及其之前的邻居。例如 =': 检查值对是否相等。在 J 中,您可以轻松折叠 /\ 但它是累积的,是否有成对
嗨,我有一个 4x4 双矩阵 A 1+2i 2-1i -3-2i -1+4i 3-1i -3+2i 1-3i -1-3i 4+3i 3+5i 1-2i -1-4i
刚刚发现 J 语言,我输入: 1+^o.*0j1 I expected the answer to be 0 ,但我得到了 0j1.22465e_16。虽然这非常接近于 0,但我想知道为什么 J 应该
这个问题在这里已经有了答案: With arrays, why is it the case that a[5] == 5[a]? (20 个答案) 关闭 3 年前。 我正在阅读“C++ 编程语言”
当第一行是 1, 1/2 , 1/3 ....这是支持该问题的图像。 是否存在比朴素的 O(n^2) 方法更有效的方法? 我在研究伯努利数时遇到了这个问题,然后在研究“Akiyama-Tanigawa
我写了一段Java代码,它在无限循环中运行。 下面是代码: public class TestProgram { public static void main(String[] args){
for (int i = n; i > 0; i /= 2) { for (int j = 0; j 0; i /= 2) 的第一个循环结果 O(log N) . 第二个循环for (int
如问题中所述,需要找到数组中 (i,j) 对的总数,使得 (1) **ia[j]** 其中 i 和 j 是数组的索引。没有空间限制。 我的问题是 1) Is there any approach w
for l in range(1,len(S)-1): for i in range(1,len(S)-l): j=i+l for X in N:
第二个for循环的复杂度是多少?会是n-i吗?根据我的理解,第一个 for 循环将执行 n 次,但第二个 for 循环中的索引设置为 i。 //where n is the number elemen
我是一名优秀的程序员,十分优秀!