{"id":165,"date":"2023-07-07T17:59:41","date_gmt":"2023-07-07T09:59:41","guid":{"rendered":"https:\/\/blessingcr.com\/?p=165"},"modified":"2023-07-07T17:59:41","modified_gmt":"2023-07-07T09:59:41","slug":"%e6%b3%a8%e8%a7%a3%e5%ae%9e%e7%8e%b0controller%e4%bb%8eheader%e4%b8%ad%e8%a7%a3%e6%9e%90jwt%e5%b9%b6%e6%b3%a8%e5%85%a5%e5%88%b0%e5%8f%82%e6%95%b0%e4%b8%ad","status":"publish","type":"post","link":"https:\/\/blessingcr.com\/?p=165","title":{"rendered":"\u6ce8\u89e3\u5b9e\u73b0Controller\u4eceheader\u4e2d\u89e3\u6790Jwt\u5e76\u6ce8\u5165\u5230\u53c2\u6570\u4e2d"},"content":{"rendered":"<h3>\u573a\u666f\u9700\u6c42\u4e3a\uff1a gateway\u7ed9header\u4e2d\u5b58\u4e86jwt\uff0cjwt\u5b58\u4e86\u591a\u4e2a\u4fe1\u606f\uff0c\u53ef\u80fd\u6709\u89d2\u8272\uff0cuid\uff0c\u6743\u9650\u7b49\uff0c\u9700\u8981\u4e00\u4e2a\u6ce8\u89e3\uff0c\u5728Controller\u4e0a\uff0c\u4f7f\u7528@JwtClaimParam(&quot;userId&quot;)\u00a0Long userId\u00a0\u5c06jwt\u4e2d\u7684userId\u89e3\u6790\u51fa\u6765\u5e76\u6ce8\u5165Controller\u53c2\u6570\u7684userId\u4e2d\u3002<\/h3>\n<h2>1. \u5b9a\u4e49\u6ce8\u89e3<\/h2>\n<pre><code>import java.lang.annotation.*;\n\/**\n* @author : eben@hi.want.net\n* @date : 2023\/7\/6 17:34\n* @description:\n*\/\n@Target(ElementType.PARAMETER)\n@Retention(RetentionPolicy.RUNTIME)\n@Documented\npublic @interface JwtClaimParam {\nString value() default &amp;quot;&amp;quot;;\n}<\/code><\/pre>\n<h2>2. \u62e6\u622a\u6ce8\u89e3<\/h2>\n<pre><code>package net.want.common.annotation.handler;\n\nimport cn.hutool.jwt.JWT;\nimport cn.hutool.jwt.JWTUtil;\nimport net.want.common.annotation.JwtClaimParam;\nimport org.springframework.core.MethodParameter;\nimport org.springframework.util.StringUtils;\nimport org.springframework.web.bind.support.WebDataBinderFactory;\nimport org.springframework.web.context.request.NativeWebRequest;\nimport org.springframework.web.method.support.HandlerMethodArgumentResolver;\nimport org.springframework.web.method.support.ModelAndViewContainer;\n\nimport java.util.List;\n\n\/**\n * @author : eben@hi.want.net\n * @date : 2023\/7\/7 15:19\n * @description:\n *\/\npublic class ProcessedHeaderArgumentResolver implements HandlerMethodArgumentResolver {\n\n    @Override\n    public boolean supportsParameter(MethodParameter parameter) {\n        return parameter.getParameterAnnotation(JwtClaimParam.class) != null;\n    }\n\n    @Override\n    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {\n        JwtClaimParam annotation = parameter.getParameterAnnotation(JwtClaimParam.class);\n        String claimName = StringUtils.hasText(annotation.value()) ? annotation.value() : parameter.getParameterName();\n        String token = webRequest.getHeader(&amp;quot;jwt&amp;quot;);\n\n        \/\/ \u89e3\u6790JWT\u5e76\u83b7\u53d6\u58f0\u660e\u503c\n        JWT jwt = JWTUtil.parseToken(token);\n        Class&amp;lt;?&amp;gt; dynamicClass = Class.forName(parameter.getParameter().getParameterizedType().getTypeName());\n        Object convertedValue = jwt.getPayload(claimName);\n        if (dynamicClass.equals(List.class)) {\n            convertedValue = List.of(parameter);\n        } else {\n            \/\/ \u666e\u901a\u7c7b\u578b\uff0c INT , LONG, String \u4e4b\u7c7b\u7684\n            convertedValue = dynamicClass.getDeclaredMethod(&amp;quot;valueOf&amp;quot;, String.class).invoke(null, convertedValue.toString());\n        }\n        return convertedValue;\n    }\n\n}\n<\/code><\/pre>\n<p>\u5176\u4e2d convertedValue = dynamicClass.getDeclaredMethod(&quot;valueOf&quot;, String.class).invoke(null, convertedValue.toString());<br \/>\n\u610f\u4e49\u4e3a\uff1a \u6267\u884cdynamicClass\u7684valueOf\u65b9\u6cd5\uff0c\u5bf9\u5e94\u7684\u5165\u53c2\u662fconvertedValue.toString()\uff0c\u5165\u53c2\u7c7b\u578b\u662f String.class<\/p>\n<p>\u8fd9\u91cc\u53ea\u5b9e\u73b0\u4e86 \u5bf9\u4e8eList\u548cInt\uff0cLong\uff0cString\u7b49\u57fa\u672c\u7c7b\u578b\uff0c\u4e0d\u77e5\u9053\u600e\u4e48\u5168\u81ea\u52a8\u53cd\u5c04\u3002<\/p>\n<h2>3. \u5c06\u62e6\u622a\u5668\u6ce8\u5165WebConfig<\/h2>\n<pre><code>@Configuration\npublic class WebConfig implements WebMvcConfigurer {\n\n    \/**\n     * \u6ce8\u518c\u81ea\u5b9a\u4e49\u7684\u53c2\u6570\u89e3\u6790\u5668\n     *\/\n    @Override\n    public void addArgumentResolvers(List&amp;lt;HandlerMethodArgumentResolver&amp;gt; argumentResolvers){\n        argumentResolvers.add(new RequestStringParamHandlerMethodArgumentResolver());\n        argumentResolvers.add(new ProcessedHeaderArgumentResolver());\n        WebMvcConfigurer.super.addArgumentResolvers(argumentResolvers);\n\n    }\n}<\/code><\/pre>\n<h2>4. \u4efb\u610fController\u4e0a\u4f7f\u7528<\/h2>\n<pre><code>public ResultVO&amp;lt;list&amp;gt; userUploadFile(MultipartFile[] files, @JwtClaimParam(&amp;quot;userId&amp;quot;) Long userId)&amp;lt;\/list<\/code><\/pre>\n<h2>ps:Gateway\u4e2d\u6dfb\u52a0jwt\u4ee3\u7801\u5982\u4e0b<\/h2>\n<pre><code>                    webclientBuilder.build()\n                            .get()\n                            .uri(&amp;quot;lb:\/\/xxxxxxxx\/open\/auth\/api\/auth\/user\/info&amp;quot;)\n                            .header(&amp;quot;token&amp;quot;, token)\n                            .exchange()\n                            .flatMap(resp -&amp;gt; resp.bodyToMono(ResultVO.class)\n                                    .doOnSuccess(\n                                            body -&amp;gt; {\n                                                System.out.println(&amp;quot;success&amp;quot;);\n                                                System.out.println(body);\n                                                UserInfoResponse userInfoResponse = new UserInfoResponse();\n                                                BeanUtil.copyProperties(body.getData(), userInfoResponse);\n                                                if (body.getCode() != null &amp;amp;&amp;amp; ResultCode.SUCCESS.getCode().equals(body.getCode()) &amp;amp;&amp;amp; body.getData() != null) {\n                                                    Optional.ofNullable(userInfoResponse.getId()).map(i -&amp;gt; exchange.getRequest().mutate().header(&amp;quot;userId&amp;quot;, i.toString()));\n                                                    HashMap&amp;lt;String, Object&amp;gt; tokenMap = new HashMap&amp;lt;&amp;gt;();\n                                                    tokenMap.put(&amp;quot;userId&amp;quot;, userInfoResponse.getId());\n                                                    String jwt = JWT.create()\n                                                                    .setPayload(&amp;quot;userId&amp;quot;, userInfoResponse.getId())\n                                                                    .setKey(&amp;quot;testRes&amp;quot;.getBytes())\n                                                                    .sign();\n                                                    exchange.getRequest().mutate().header(&amp;quot;jwt&amp;quot;, jwt);\n                                                } else {\n                                                    log.error(&amp;quot;\u83b7\u53d6admin-uid\u5931\u8d25-1, \u8003\u8651token\u662f\u5426\u8fc7\u671f\/\u6b63\u786e&amp;quot;);\n                                                    throw new RuntimeException(&amp;quot;\u83b7\u53d6admin-uid\u5931\u8d25&amp;quot;);\n                                                }\n                                            }\n                                    ))\n                            .doOnError(i -&amp;gt; {\n                                log.error(&amp;quot;\u83b7\u53d6admin-uid\u5931\u8d25-2, \u8003\u8651token\u662f\u5426\u8fc7\u671f\/\u6b63\u786e&amp;quot;);\n                                throw new RuntimeException(&amp;quot;\u83b7\u53d6admin-uid\u5931\u8d25&amp;quot;);\n                            })\n                            .block();<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u573a\u666f\u9700\u6c42\u4e3a\uff1a gateway\u7ed9header\u4e2d\u5b58\u4e86jwt\uff0cjwt\u5b58\u4e86\u591a\u4e2a\u4fe1\u606f\uff0c\u53ef\u80fd\u6709\u89d2\u8272\uff0cuid\uff0c\u6743\u9650\u7b49\uff0c\u9700\u8981\u4e00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[72,73,70,71],"class_list":["post-165","post","type-post","status-publish","format-standard","hentry","category-spring","tag-jwt","tag-73","tag-70","tag-71"],"_links":{"self":[{"href":"https:\/\/blessingcr.com\/index.php?rest_route=\/wp\/v2\/posts\/165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blessingcr.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blessingcr.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blessingcr.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blessingcr.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=165"}],"version-history":[{"count":3,"href":"https:\/\/blessingcr.com\/index.php?rest_route=\/wp\/v2\/posts\/165\/revisions"}],"predecessor-version":[{"id":168,"href":"https:\/\/blessingcr.com\/index.php?rest_route=\/wp\/v2\/posts\/165\/revisions\/168"}],"wp:attachment":[{"href":"https:\/\/blessingcr.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blessingcr.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blessingcr.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}