Spring OAuth2 Requiring PlatformTransactionManager

放肆的年华 提交于 2019-12-05 18:08:11

Problem is that you configured in your application a usage of a in-memory database with new InMemoryTokenStore(), but your spring-boot application contains no in-memory database.

Solution: add in your spring-boot pom or gradle dependency a in-memory database.

Example for H2 and Maven pom:

<dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.187</version>
</dependency>

I had the same problem

tokenServices.createAccessToken use @Transactional . As i use mongo DB i don't need transactions .

i solved the problem by adding a PseudoTransactionManager bean .

@Bean
public PlatformTransactionManager annotationDrivenTransactionManager() {
    return new PseudoTransactionManager();
}

The problem is that methods in DefaultTokenServices are annotated with @Transactional. So even if you're not using a database, you'll need to add a transaction manager bean like this in your authorization server configuration:

    @Bean
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return new ResourceTransactionManager() {
            @Override
            public Object getResourceFactory() {
                return null;
            }

            @Override
            public TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {
                return null;
            }

            @Override
            public void commit(TransactionStatus status) throws TransactionException {

            }

            @Override
            public void rollback(TransactionStatus status) throws TransactionException {

            }
        };
    }

I faced similar issue of PlatformTransactionManager and resolved it by the following steps:

  1. Added H2 database to pom.xml (to enable storage of clients in memory)
  2. Using Mongo DB as application backend. (ensured application uses MongoRepository instead of CrudRepository)
  3. Removed exclude class in @EnableAutoConfiguration annotation (I had earlier added DataSourceAutoConfiguration.class in exclusion)

Point 1 and Point 3 are mutual. H2 configuration should have DataSourceAutoConfiguration.class enabled.

Thanks.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!