我尝试为REST服务创建自己的基于令牌的授权。这是我的TokenService:
@Component
public class TokenService {
...
private Set<Token> tokens;
private SecureRandom random;
...
public TokenService() {
this.tokens = new HashSet<Token>();
this.random = new SecureRandom();
}
public boolean has(final String token) {
clear();
Token tokenStub = new Token(token);
return this.tokens.contains(tokenStub);
}
public boolean remove(final String token) {
Token tokenStub = new Token(token);
return this.tokens.remove(tokenStub);
}
public Token retrieve(final TokenDetails tokenDetails) {
clear();
Token token = this.get(tokenDetails);
if (token == null) {
return gen(tokenDetails);
}
token.setDate(System.currentTimeMillis());
token.setTokenDetails(tokenDetails);
return token;
}
... //other methods
}
我是Spring Boot的新手,我知道Spring Security,但我想制作自己的安全服务。有没有办法让我的@Component类使用Spring Boot进行线程安全?