[英]Oauth2 Spring-boot read-only connection error
I am continuously getting below error , I did enable transaction with @EnableTransactionManagement , but still somehow transaction is not invoked in DefaultTokenServices .我不断遇到错误,我确实使用@EnableTransactionManagement启用了事务,但仍然没有在DefaultTokenServices 中调用事务。
Any help will be much appreciated任何帮助都感激不尽
Note: it was working with spring-boot 1.5 and recently I upgraded to 2.1注意:它与 spring-boot 1.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)
- solution解决方案
I am able to fix with hack by manually attaching transaction to oauth jdbctokenservice.我可以通过手动将事务附加到 oauth jdbctokenservice 来修复 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);
}
I also created issue with spring-security-oauth but seems it is not supposed to support spring-boot 2.x.我还创建了 spring-security-oauth 问题,但似乎它不应该支持 spring-boot 2.x。
Any Brilliant mind wanna help on why Transaction was not invoked in DefaultTokenServices.任何聪明的头脑都想帮助解释为什么没有在 DefaultTokenServices 中调用 Transaction。
- Solution解决方案
Create Bean for DefaultTokenServices and pass it to configurer为 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();
}
link: https://github.com/spring-projects/spring-security-oauth/issues/1900链接: https : //github.com/spring-projects/spring-security-oauth/issues/1900
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.