Neo4J TokenStore Spring oauth2

1

Я работаю над веб-сервисами Spring REST, защищенными oauth2. Я хотел бы отделить AuthorizationServer от ResourceServer с двумя разными приложениями - AuthorizationServer - это автономный SSO (single sign-on) oauth2, а ResourceServer - второе приложение для бизнес-служб REST. Таким образом, я не могу использовать inmemory tokenstore, потому что мои приложения будут жить на разных хостах. Мне нужен некоторый общий механизм для TokenStore, например базы данных. Есть реализация JdbcTokenStore в Spring Framework, но в моих проектах я использую базу данных графиков Neo4J.

Итак, у меня есть вопросы - следует ли пытаться хранить маркеры oauth2 в Neo4J (например, создать пользовательскую реализацию Neo4JTokenStore (уже существует?)) Или в какой-либо другой базе данных? Что лучше всего подходит для этого случая?

Теги:
spring-security
neo4j
oauth-2.0
spring-security-oauth2

3 ответа

1
Лучший ответ

Я успешно реализовал Token Store для Neo4j.

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 для преобразования String для двух объектов OAuth2AccessToken и OAuth2Authentication в OAuth2AuthenticationAccessToken.java и OAuth2AuthenticationAccessToken.java, как показано ниже.

@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>

Конец. :) Приветствуем любую обратную связь.

  • 0
    Спасибо большое за эту потрясающую работу !!!
0

Не нужно хранить токен в базе данных. Храните токен во внутренней памяти.

См. Мой файл Oauth2Configuration,

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
import org.springframework.web.context.request.RequestContextListener;

/**
 * OAuth2 authentication configuration
 */
@Configuration
public class OAuth2Configuration {

    @Configuration
    @EnableResourceServer
    protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

        @Autowired
        private CustomLogoutSuccessHandler customLogoutSuccessHandler;

        @Override
        public void configure(HttpSecurity http) throws Exception {

            http
            .csrf().disable()
            .exceptionHandling()
            .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and().logout().logoutUrl("/oauth/logout").logoutSuccessHandler(customLogoutSuccessHandler)
            .and().authorizeRequests()
            .antMatchers("/api/v1/**").access("#oauth2.clientHasAnyRole('ROLE_USER')");
        }
    }

    @Bean 
    public RequestContextListener requestContextListener(){
        return new RequestContextListener();
    } 

    @Configuration
    @EnableAuthorizationServer
    protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

        private static final String PROP_CLIENTID = "client";
        private static final String PROP_SECRET = "secret";
        private static final int PROP_TOKEN_VALIDITY_SECONDS = 60 * 60 * 12;

        @Bean
        public TokenStore tokenStore() {
            return new InMemoryTokenStore();
        }

        @Autowired
        @Qualifier("myAuthenticationManager")
        private AuthenticationManager authenticationManager;

        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
            endpoints
                .tokenStore(tokenStore())
                .authenticationManager(authenticationManager);

        }

        @Override
        public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
            clients
                    .inMemory()
                    .withClient(PROP_CLIENTID)
                    .scopes("read", "write", "trust")
                    .authorities("ROLE_USER")
                    .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")                    
                    .secret(PROP_SECRET)
                    .accessTokenValiditySeconds(PROP_TOKEN_VALIDITY_SECONDS);
        }

        @Bean
        @Primary
        public DefaultTokenServices tokenServices() {
            DefaultTokenServices tokenServices = new DefaultTokenServices();
            tokenServices.setSupportRefreshToken(true);
            tokenServices.setTokenStore(tokenStore());
            return tokenServices;
        }
    }

}

Это работает для меня!

  • 0
    Это не будет работать в кластере, в памяти, как предполагается, для тестирования
  • 0
    " Я могу вам помочь"?
0

Если вы хотите использовать Neo4j для своего проекта, я бы использовал Token Store для Neo4j (и с открытым исходным кодом). Думаю, это должно быть довольно тривиально.

Если вы уже используете другие базы данных, вы также можете использовать их хранилище.

Вопрос в том, нужно ли подключать токены oauth2 с чем-либо на вашем графике?

Ещё вопросы

Сообщество Overcoder
Наверх
Меню