oauth2 spring boot只读连接错误

wb1gzix0  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(525)

我不断得到下面的错误,我确实启用了@enabletransactionmanagement事务,但仍然不知何故,事务没有在defaulttokenservices中调用。
任何帮助都将不胜感激
注意:它与springboot1.5一起工作,最近我升级到了2.1

2020-11-19 18:27:12.385 ERROR 49065 [tomcat-exec-2] - o.s.s.o.provider.endpoint.TokenEndpoint  : Handling error: TransientDataAccessResourceException, PreparedStatementCallback; SQL [insert into oauth_access_token (token_id, token, authentication_id, user_name, client_id, authentication, refresh_token) values (?, ?, ?, ?, ?, ?, ?)]; Connection is read-only. Queries leading to data modification are not allowed; nested exception is java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed

org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback; SQL [insert into oauth_access_token (token_id, token, authentication_id, user_name, client_id, authentication, refresh_token) values (?, ?, ?, ?, ?, ?, ?)]; Connection is read-only. Queries leading to data modification are not allowed; nested exception is java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
    at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:110)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
pwuypxnk

pwuypxnk1#

解决方案
我可以通过手动将事务附加到OAuthJDBCtokenService来修复hack。

private static final String AOP_POINTCUT_EXPRESSION = "execution (* org.springframework.security.oauth2.provider.token.store.JdbcTokenStore.*(..))";

    @Autowired
    public void txAdvice(TransactionInterceptor txAdvice) throws NoSuchMethodException {
        DefaultTransactionAttribute required = new DefaultTransactionAttribute();
        MethodMapTransactionAttributeSource source = new MethodMapTransactionAttributeSource();
        final Method method = JdbcTokenStore.class.getMethod("storeAccessToken", OAuth2AccessToken.class, OAuth2Authentication.class);
        source.addTransactionalMethod(method, required);
        txAdvice.setTransactionAttributeSource(source);
    }

    @Bean
    public Advisor txAdviceAdvisor(TransactionInterceptor txAdvice) {
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
        return new DefaultPointcutAdvisor(pointcut, txAdvice);
    }

我还创建了一个关于springsecurityoauth的问题,但似乎它不应该支持springboot2.x。
任何聪明的人都想知道为什么事务不能在defaulttokenservices中调用。
解决方案
为defaulttokenservices创建bean并将其传递给配置程序

@Autowired
        private DefaultTokenServices tokenServices;

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

        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints)
                throws Exception {
            endpoints.authorizationCodeServices(authorizationCodeServices())
                    .tokenStore(tokenStore)
                    .authenticationManager(auth)
                    .addInterceptor(handlerInterceptor)
                    .tokenServices(tokenServices)
                    .approvalStoreDisabled();
        }

链接:https://github.com/spring-projects/spring-security-oauth/issues/1900

相关问题