Skip to content

org.springframework.security.oauth2.server.authorization.JdbcOAuth2AuthorizationService should support postgres DB as an implementation #2321

@solidjb

Description

@solidjb

Expected Behavior
An application can choose a PostgresDB backend to implement org.springframework.security.oauth2.server.authorization.JdbcOAuth2AuthorizationService. This would include cloud offerings such as AWS Aurora DSQL.

Current Behavior
Postgres does not support BLOB (sql type 2004) and CLOB (sql type 2005) types, they use BYTEA (sql type -2) instead. The class JdbcOAuth2AuthorizationService does not have any support for the BYTEA column type, and therefore fails to save or retrieve data to and from the db appropriately.

Context
In order to fix this, I copied the class org.springframework.security.oauth2.server.authorization.JdbcOAuth2AuthorizationService into my project and modified two methods:

  1. org.springframework.security.oauth2.server.authorization.JdbcOAuth2AuthorizationService.AbstractOAuth2AuthorizationRowMapper#getLobValue
  2. org.springframework.security.oauth2.server.authorization.JdbcOAuth2AuthorizationService.LobCreatorArgumentPreparedStatementSetter#doSetValue

In both methods, I added support for the BYTEA column type, and treated the data similar to a BLOB type.
for getLobValue, I did this:
if (Types.BINARY == columnMetadata.getDataType()) { byte[] columnValueBytes = this.lobHandler.getBlobAsBytes(rs, columnName); if (columnValueBytes != null) { columnValue = new String(columnValueBytes, StandardCharsets.UTF_8); } }

and for doSetValue, I did this:
`
if (paramValue.getSqlType() == Types.BINARY) {
byte[] valueBytes = null;
if (paramValue.getValue() != null) {

    Object value = paramValue.getValue();

    if (value instanceof byte[] byteArray) {
        valueBytes = byteArray;
    }
    else if (value instanceof String stringValue) {
        valueBytes = stringValue.getBytes(StandardCharsets.UTF_8);
    }
}
this.lobCreator.setBlobAsBytes(ps, parameterPosition, valueBytes);
return;

}
`
There could be more to do, but I haven't discovered anything else at the moment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions