spring security oauth2 集成异常处理

  1. Java 8 date/time type java.time.Instant not supported by default:

自己new的ObjectMapper对象没有注册JavaTimeModule。

解决方法:

java 复制代码
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
  1. 刷新token时,反序列化对象异常

异常可能有:

shell 复制代码
The class with java.lang.Long and name of java.lang.Long is not in the allowlist

相关对象添加注解

java 复制代码
@JsonDeserialize
@JsonIgnoreProperties(ignoreUnknown = true)

第二个注解使反序列化时忽略没有的属性

shell 复制代码
Unexpected token (START_OBJECT), expected START_ARRAY:need Array value to contain `As.WRAPPER_ARRAY` type information for class java.util.Map
...
you can also enable default typing.

这是由于序列化和反序列化策略不一致导致的。

  • WRAPPER_ARRAY:以数组的方式,Class Type 为第一个元素,Json字符串为第二个元素。
json 复制代码
[
    "info.leve.******.Person", 
    {
        "name": "leve", 
        "age": 18
    }
]
  • WRAPPER_OBJECT:以对象的形式,Class Type 为key, Json字符串为value。
json 复制代码
{
    "info.kleven.******.Person": {
        "name": "kleven", 
        "age": 18
    }
}
  • PROPERTY: 以属性的的方式,在Json字符串中体现为:"@class"为key,Class Type 为value。
json 复制代码
{
    "@class": "info.kleven.******.Person", 
    "name": "kleven", 
    "age": 18
}
  • EXISTING_PROPERTY: 序列化后的字符串不包含Class Type。
json 复制代码
{
    "name": "kleven", 
    "age": 18
}

spring security 使用的是PROPERTY方式,下面设置反序列化参数:

java 复制代码
/**
     * 保存token的service
     * @return 实例
     */
    @Bean
    public OAuth2AuthorizationService authorizationService() {
        var service = new JdbcOAuth2AuthorizationService(jdbcOperation, registeredClientRepository);
        JdbcOAuth2AuthorizationService.OAuth2AuthorizationRowMapper rowMapper =
                new JdbcOAuth2AuthorizationService.OAuth2AuthorizationRowMapper(registeredClientRepository);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JavaTimeModule());
        //以属性的的方式,在Json字符串中体现为:"@class"为key,Class Type 为value。
        objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(),
                ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        rowMapper.setObjectMapper(objectMapper);
        service.setAuthorizationRowMapper(rowMapper);
        return service;
    }
相关推荐
这个DBA有点耶3 天前
MVCC深入:Read View、版本链与快照读——InnoDB并发控制的内核
数据库·mysql·架构
DBA_G3 天前
从地面到云霄:GBase数据库在民航三大场景的落地实践
数据库
自由能燃气设备3 天前
商用全预混低氮冷凝锅炉免费方案vs付费方案对比+选型避坑指南
大数据·数据库·人工智能
科创致远3 天前
科创致远 ESOP 系统核心效能与实战价值展示
大数据·数据库·人工智能·精益工程
Bingo_BIG3 天前
Java Spring框架:单表的查询、新增、修改、删除、导入、导出
java·spring·前后端
2601_962218613 天前
万象生鲜系统称重自动多退少补算法解决生鲜非标品痛点
大数据·数据库·人工智能·python·算法
张洛闻Eren3 天前
k8s云原生【第十课】:水平 Pod 自动扩缩容
运维·数据库·云原生·kubernetes·github
于平安3 天前
MySQL-触发器
数据库·mysql
白远山3 天前
上海24小时自助健身房解决方案实战指南与经验分享
java·数据库·架构·需求分析
Omics Pro3 天前
斯坦福Nature+Science|广义虚拟细胞基础大模型
数据库·人工智能·算法·机器学习·自然语言处理