Browse Source

Polish JdbcOAuth2AuthorizationService

pull/17969/merge
Joe Grandja 1 month ago
parent
commit
73840663b9
  1. 114
      oauth2/oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/JdbcOAuth2AuthorizationService.java

114
oauth2/oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/JdbcOAuth2AuthorizationService.java

@ -466,7 +466,7 @@ public class JdbcOAuth2AuthorizationService implements OAuth2AuthorizationServic
/** /**
* The default {@link RowMapper} that maps the current row in * The default {@link RowMapper} that maps the current row in
* {@code java.sql.ResultSet} to {@link OAuth2Authorization} using Jackson 3's * {@code java.sql.ResultSet} to {@link OAuth2Authorization} using Jackson 3's
* {@link JsonMapper} to read all {@code Map<String,Object>} within the result. * {@link JsonMapper}.
* *
* @author Rob Winch * @author Rob Winch
* @since 7.0 * @since 7.0
@ -482,6 +482,7 @@ public class JdbcOAuth2AuthorizationService implements OAuth2AuthorizationServic
public JsonMapperOAuth2AuthorizationRowMapper(RegisteredClientRepository registeredClientRepository, public JsonMapperOAuth2AuthorizationRowMapper(RegisteredClientRepository registeredClientRepository,
JsonMapper jsonMapper) { JsonMapper jsonMapper) {
super(registeredClientRepository); super(registeredClientRepository);
Assert.notNull(jsonMapper, "jsonMapper cannot be null");
this.jsonMapper = jsonMapper; this.jsonMapper = jsonMapper;
} }
@ -544,7 +545,7 @@ public class JdbcOAuth2AuthorizationService implements OAuth2AuthorizationServic
private LobHandler lobHandler = new DefaultLobHandler(); private LobHandler lobHandler = new DefaultLobHandler();
AbstractOAuth2AuthorizationRowMapper(RegisteredClientRepository registeredClientRepository) { private AbstractOAuth2AuthorizationRowMapper(RegisteredClientRepository registeredClientRepository) {
Assert.notNull(registeredClientRepository, "registeredClientRepository cannot be null"); Assert.notNull(registeredClientRepository, "registeredClientRepository cannot be null");
this.registeredClientRepository = registeredClientRepository; this.registeredClientRepository = registeredClientRepository;
} }
@ -713,42 +714,36 @@ public class JdbcOAuth2AuthorizationService implements OAuth2AuthorizationServic
} }
/** /**
* Nested class to protect from getting {@link NoClassDefFoundError} when Jackson 2 is * The default {@code Function} that maps {@link OAuth2Authorization} to a
* not on the classpath. * {@code List} of {@link SqlParameterValue} using an instance of Jackson 3's
* * {@link JsonMapper}.
* @deprecated This is used to allow transition to Jackson 3. Use {@link Jackson3}
* instead.
*/ */
@Deprecated(forRemoval = true, since = "7.0") public static class JsonMapperOAuth2AuthorizationParametersMapper
private static final class Jackson2 { extends AbstractOAuth2AuthorizationParametersMapper {
static ObjectMapper createObjectMapper() { private final JsonMapper jsonMapper;
ObjectMapper objectMapper = new ObjectMapper();
ClassLoader classLoader = Jackson2.class.getClassLoader();
List<Module> securityModules = SecurityJackson2Modules.getModules(classLoader);
objectMapper.registerModules(securityModules);
objectMapper.registerModule(new OAuth2AuthorizationServerJackson2Module());
return objectMapper;
}
} public JsonMapperOAuth2AuthorizationParametersMapper() {
this(Jackson3.createJsonMapper());
}
/** public JsonMapperOAuth2AuthorizationParametersMapper(JsonMapper jsonMapper) {
* Nested class used to get a common default instance of {@link JsonMapper}. It is in Assert.notNull(jsonMapper, "jsonMapper cannot be null");
* a nested class to protect from getting {@link NoClassDefFoundError} when Jackson 3 this.jsonMapper = jsonMapper;
* is not on the classpath. }
*/
private static final class Jackson3 {
static JsonMapper createJsonMapper() { @Override
List<JacksonModule> modules = SecurityJacksonModules.getModules(Jackson3.class.getClassLoader()); String writeValueAsString(Map<String, Object> data) throws Exception {
return JsonMapper.builder().addModules(modules).build(); return this.jsonMapper.writeValueAsString(data);
} }
} }
/** /**
* @deprecated Use {@link JsonMapperOAuth2AuthorizationParametersMapper} to migrate to * A {@code Function} that maps {@link OAuth2Authorization} to a {@code List} of
* {@link SqlParameterValue} using an instance of Jackson 2's {@link ObjectMapper}.
*
* @deprecated Use {@link JsonMapperOAuth2AuthorizationParametersMapper} to switch to
* Jackson 3. * Jackson 3.
*/ */
@Deprecated(forRemoval = true, since = "7.0") @Deprecated(forRemoval = true, since = "7.0")
@ -772,32 +767,6 @@ public class JdbcOAuth2AuthorizationService implements OAuth2AuthorizationServic
} }
/**
* The default {@code Function} that maps {@link OAuth2Authorization} to a
* {@code List} of {@link SqlParameterValue} using an instance of Jackson 3's
* {@link JsonMapper}.
*/
public static final class JsonMapperOAuth2AuthorizationParametersMapper
extends AbstractOAuth2AuthorizationParametersMapper {
private final JsonMapper mapper;
public JsonMapperOAuth2AuthorizationParametersMapper() {
this(Jackson3.createJsonMapper());
}
public JsonMapperOAuth2AuthorizationParametersMapper(JsonMapper mapper) {
Assert.notNull(mapper, "mapper cannot be null");
this.mapper = mapper;
}
@Override
String writeValueAsString(Map<String, Object> data) throws Exception {
return this.mapper.writeValueAsString(data);
}
}
/** /**
* The base {@code Function} that maps {@link OAuth2Authorization} to a {@code List} * The base {@code Function} that maps {@link OAuth2Authorization} to a {@code List}
* of {@link SqlParameterValue}. * of {@link SqlParameterValue}.
@ -805,7 +774,7 @@ public class JdbcOAuth2AuthorizationService implements OAuth2AuthorizationServic
private abstract static class AbstractOAuth2AuthorizationParametersMapper private abstract static class AbstractOAuth2AuthorizationParametersMapper
implements Function<OAuth2Authorization, List<SqlParameterValue>> { implements Function<OAuth2Authorization, List<SqlParameterValue>> {
protected AbstractOAuth2AuthorizationParametersMapper() { private AbstractOAuth2AuthorizationParametersMapper() {
} }
@Override @Override
@ -916,6 +885,41 @@ public class JdbcOAuth2AuthorizationService implements OAuth2AuthorizationServic
} }
/**
* Nested class to protect from getting {@link NoClassDefFoundError} when Jackson 2 is
* not on the classpath.
*
* @deprecated This is used to allow transition to Jackson 3. Use {@link Jackson3}
* instead.
*/
@Deprecated(forRemoval = true, since = "7.0")
private static final class Jackson2 {
private static ObjectMapper createObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
ClassLoader classLoader = Jackson2.class.getClassLoader();
List<Module> securityModules = SecurityJackson2Modules.getModules(classLoader);
objectMapper.registerModules(securityModules);
objectMapper.registerModule(new OAuth2AuthorizationServerJackson2Module());
return objectMapper;
}
}
/**
* Nested class used to get a common default instance of {@link JsonMapper}. It is in
* a nested class to protect from getting {@link NoClassDefFoundError} when Jackson 3
* is not on the classpath.
*/
private static final class Jackson3 {
private static JsonMapper createJsonMapper() {
List<JacksonModule> modules = SecurityJacksonModules.getModules(Jackson3.class.getClassLoader());
return JsonMapper.builder().addModules(modules).build();
}
}
private static final class LobCreatorArgumentPreparedStatementSetter extends ArgumentPreparedStatementSetter { private static final class LobCreatorArgumentPreparedStatementSetter extends ArgumentPreparedStatementSetter {
private final LobCreator lobCreator; private final LobCreator lobCreator;

Loading…
Cancel
Save