6

keycloak~管理平台的查询bug与自定rest中文检索

 3 years ago
source link: https://www.cnblogs.com/lori/p/14989021.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

keycloak~管理平台的查询bug与自定rest中文检索

对于keycloak来说,它的管理平台在它的源码中的admin-client中,它会定义相关的rest接口规范;在我们使用keycloak管理平台时,其中有一个组的查询,在我们查询中文组时,它是不支持的,经过测试和mysql日志监控得到原因:

  1. keycloak rest使用javax.ws.rs包下面的注解,在使用@QueryParam注解来接收url参数时,当出现中文时,它实现是一个urlEncode的字符
  2. 它本身不会对字段进行urlDecode的操作,所以我们自己要做;而spring框架帮我们作了这事,我们自己不用做
  • urlDecode截图之前和之后
  • 手动添加urlDecode代码
@GET
@Path("search")
@NoCache
@Produces({MediaType.APPLICATION_JSON})
@Encoded
public Response search(@QueryParam("name") String name) throws UnsupportedEncodingException {
    if (StringUtils.isEmpty(name)) {
        throw new ClientErrorException("name不能为空", Response.Status.BAD_REQUEST);
    }
    TypedQuery<GroupEntity> query = em.createQuery("select u from GroupEntity u where u.realm = :realm and  lower(u.name)  = :name", GroupEntity.class);
    log.info("group name:" + name);
    name = URLDecoder.decode(name, "utf-8");
    log.info("group name 2:" + name);
    query.setParameter("name", name.trim().toLowerCase());
    query.setParameter("realm", session.getContext().getRealm().getName());
    List<GroupEntity> result = query.getResultList();
    return Response.ok(result, APPLICATION_JSON_TYPE).build();
}

查询中文组

[
    {
        "id": "d355cb9e-9557-4465-9eba-44f20b0c24ea",
        "name": "测试",
        "parentId": " ",
        "realm": "fabao",
        "attributes": []
    }
]

作者:仓储大叔,张占岭,
荣誉:微软MVP
QQ:853066980

支付宝扫一扫,为大叔打赏!
o_IMG_7144.JPG

posted @ 2021-07-09 08:50  张占岭  阅读(43)  评论(0)  编辑  收藏  举报

Copyright © 2021 张占岭
Powered by .NET 5.0 on Kubernetes


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK