{"id":1225,"date":"2020-11-15T14:50:50","date_gmt":"2020-11-15T06:50:50","guid":{"rendered":"http:\/\/diuut.com\/?p=1225"},"modified":"2020-11-15T14:50:52","modified_gmt":"2020-11-15T06:50:52","slug":"%e5%9f%ba%e4%ba%8ewebflux%e8%bf%87%e6%bb%a4%e5%99%a8%e4%b8%8ethemleaf%e7%9a%84%e7%99%bb%e9%99%86%e6%9d%83%e9%99%90%e9%aa%8c%e8%af%81","status":"publish","type":"post","link":"https:\/\/diuut.com\/?p=1225","title":{"rendered":"\u57fa\u4e8eWebFlux\u8fc7\u6ee4\u5668\u4e0eThemleaf\u7684\u767b\u9646\u6743\u9650\u9a8c\u8bc1"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote\"><p>\u9700\u6c42:<\/p><cite>\u540e\u53f0\u7ba1\u7406\u7cfb\u7edf\u7684\u6570\u636e\u4fee\u6539\u90e8\u5206\u9700\u8981\u4e00\u4e2a\u7b80\u6613\u7684\u8fdb\u884c\u767b\u9646\u6743\u9650\u9a8c\u8bc1<\/cite><\/blockquote>\n\n\n\n<p>\u539f\u672c\u53ef\u4ee5\u7684\u8bdd\u6253\u7b97\u7528\u7684\u662fshrio\u6846\u67b6\u8fdb\u884c\u6743\u9650\u7ba1\u7406\uff0c\u4f46\u662fshrio\u6846\u67b6\u662f\u56e0\u4e3a\u9700\u8981\u5bf9HttpServletRequest\u8fdb\u884c\u914d\u7f6e\u76f8\u5173\u53c2\u6570\uff0c\u800c\u5f53\u524d\u4f7f\u7528\u7684WebFlux\u5e76\u6ca1\u6709servlet\uff0c\u6240\u4ee5\u76f4\u63a5\u6ca1\u6cd5\u4f7f\u7528shrio\u3002\u6240\u4ee5\u53ea\u6709\u81ea\u5df1\u5199\u4e00\u4e2a\u57fa\u4e8e\u8fc7\u6ee4\u5668\u7684\u5efa\u8bae\u6743\u9650\u6846\u67b6\u3002<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_66_1 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\"><p class=\"ez-toc-title\">\u76ee\u5f55<\/p>\n<\/div><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/diuut.com\/?p=1225\/#%E4%B8%BB%E8%A6%81%E6%9C%89%E4%BA%94%E4%B8%AA%E8%BF%87%E6%BB%A4%E6%B5%81%E7%A8%8B%EF%BC%9A\" title=\"\u4e3b\u8981\u6709\u4e94\u4e2a\u8fc7\u6ee4\u6d41\u7a0b\uff1a\">\u4e3b\u8981\u6709\u4e94\u4e2a\u8fc7\u6ee4\u6d41\u7a0b\uff1a<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/diuut.com\/?p=1225\/#%E8%BF%87%E6%BB%A4%E5%99%A8%EF%BC%9A\" title=\"\u8fc7\u6ee4\u5668\uff1a\">\u8fc7\u6ee4\u5668\uff1a<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/diuut.com\/?p=1225\/#Controller%E5%B1%82\" title=\"Controller\u5c42\">Controller\u5c42<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/diuut.com\/?p=1225\/#Service%E5%B1%82\" title=\"Service\u5c42\">Service\u5c42<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/diuut.com\/?p=1225\/#%E7%99%BB%E9%99%86%E9%A1%B5%E9%9D%A2\" title=\"\u767b\u9646\u9875\u9762\">\u767b\u9646\u9875\u9762<\/a><\/li><\/ul><\/nav><\/div>\n<h4><span class=\"ez-toc-section\" id=\"%E4%B8%BB%E8%A6%81%E6%9C%89%E4%BA%94%E4%B8%AA%E8%BF%87%E6%BB%A4%E6%B5%81%E7%A8%8B%EF%BC%9A\"><\/span>\u4e3b\u8981\u6709\u4e94\u4e2a\u8fc7\u6ee4\u6d41\u7a0b\uff1a<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<ol><li>\u5224\u65ad\u662f\u4e0d\u662f\u9700\u8981\u6743\u9650\u7684uri\u5b58\u5728\/admin\u4e4b\u540e\u7684\u9875\u9762\uff0c\u4ee5\u53ca\u83b7\u53d6\u9759\u6001\u8d44\u6e90\u4e0d\u8fdb\u884c\u62e6\u622a\u76f4\u63a5\u653e\u884c<\/li><li> \u5224\u65adcookie\u4e2d\u662f\u5426\u5b58\u5728\u4e00\u4e2atoken\u7684cookie\uff0c\u5982\u679c\u6ca1\u6709\u7684\u8bdd\u5219\u8df3\u8f6c\u5230\u201c\/admin\/login\/\u201c\uff0c\u5e76\u4e14\u5728\u540e\u9762\u9644\u5e26\u4e0a\u6700\u5f00\u59cb\u70b9\u51fb\u7684\u9875\u9762\uff0c\u7528\u4e8e\u4e4b\u540e \u767b\u5f55 \u6210\u529f\u540e\u76f4\u63a5\u8fdb\u5165\u8be5\u9875\u9762\uff0c\u4e0d\u7528\u91cd\u65b0\u70b9\u51fb\u3002<\/li><li>\u5982\u679c\u6709\u540d\u4e3atoken\u7684cookie\uff0c\u53d6\u51fa\u6765\uff0c\u9a8c\u8bc1\u8be5token\uff0c\u662f\u5426\u662f\u5bf9\u5e94\u7684\u6b63\u786e\u7684\u7ba1\u7406\u5458\u7528\u6237\uff0c\u5982\u679c\u6ca1\u6709\u8df3\u8f6c\u5230\u201c\/admin\/login\/\u201c,\u9644\u5e26\u8bbf\u95ee\u9875\u9762\uff0c\u7406\u7531\u540c\u4e0a\u7b2c\u4e8c\u6761\u3002<\/li><li>\u5982\u679c\u5b58\u5728\u8be5\u7528\u6237\uff0c\u5224\u65ad\u8be5\u7528\u6237\u767b\u5f55\u6700\u540e\u767b\u9646\u65f6\u95f4\u662f\u5426\u8d85\u8fc7\u4e86\u6240\u9650\u5236\u7684\u65f6\u95f4\uff0c\u5982\u679c\u8d85\u8fc7\uff0c\u4e5f\u8df3\u8f6c\u56de\u767b\u5f55\u9875\u9762\u91cd\u65b0\u767b\u5f55<\/li><li>\u4ee5\u4e0a\u56db\u4e2a\u8fc7\u6ee4\u5168\u90e8\u901a\u8fc7\u7684\u8bdd\u5c31\u8bf4\u660e\u662f\u5df2\u767b\u5f55\u7684\u6709\u6548\u7528\u6237\uff0c\u653e\u884c\u3002<\/li><\/ol>\n\n\n\n<h4><span class=\"ez-toc-section\" id=\"%E8%BF%87%E6%BB%A4%E5%99%A8%EF%BC%9A\"><\/span>\u8fc7\u6ee4\u5668\uff1a<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nimport com.miracle.qaodo.dao.ManagerUserRedisDao;\nimport com.miracle.qaodo.entity.ManagerUser;\nimport com.miracle.qaodo.util.ServerTimer;\nimport lombok.extern.slf4j.Slf4j;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.context.annotation.Configuration;\nimport org.springframework.core.annotation.Order;\nimport org.springframework.data.redis.core.RedisTemplate;\nimport org.springframework.http.HttpCookie;\nimport org.springframework.http.server.reactive.ServerHttpRequest;\nimport org.springframework.util.StringUtils;\nimport org.springframework.web.server.ServerWebExchange;\nimport org.springframework.web.server.WebFilter;\nimport org.springframework.web.server.WebFilterChain;\nimport reactor.core.publisher.Mono;\n\nimport java.net.URI;\n\n\/**\n * @Author Diuut\n * @Date 2020\/11\/5  15:54\n *\/\n@Configuration\n@Order(0)\/\/\u6570\u5b57\u8d8a\u5c0f\u8d8a\u4f18\u5148\n@Slf4j\npublic class AdminWebFilter implements WebFilter {\n    @Autowired\n    private RedisTemplate&lt;String, String&gt; redisTemplate;\n    @Autowired\n    private ManagerUserRedisDao managerUserRedisDao;\n    @Override\n    public Mono&lt;Void&gt; filter(ServerWebExchange exchange, WebFilterChain chain) {\n        ServerHttpRequest request = exchange.getRequest();\n        URI uri = request.getURI();\n        log.info(&quot;uri\uff1a{}&quot;, uri);\n        if (!uri.getPath().contains(&quot;\/admin&quot;) || uri.getPath().contains(&quot;\/static&quot;)\n                || uri.getPath().contains(&quot;.css&quot;) || uri.getPath().contains(&quot;.js&quot;)\n                || uri.getPath().contains(&quot;.jpg&quot;) || uri.getPath().contains(&quot;.ico&quot;)\n                || uri.getPath().contains(&quot;.png&quot;)) {\n            return chain.filter(exchange);\n        }\n        log.info(&quot;------AdminWebFilter-----&quot;);\n        HttpCookie cookie = exchange.getRequest().getCookies().getFirst(&quot;token&quot;);\n        String token = &quot;&quot;;\n        if (cookie != null) {\n            token = cookie.getValue();\n        }\n        log.info(&quot;token\uff1a{}&quot;, token);\n        String path = uri.getPath();\n        String newPath = path.replace(&quot;\/&quot;, &quot;_&quot;);\n        if (StringUtils.isEmpty(token) &amp;&amp; uri.getPath().contains(&quot;\/admin&quot;)) {\n            log.info(&quot;\u767b\u9646\u4fe1\u606ftoken\u4e3a\u7a7a&quot;);\n            ServerHttpRequest authErrorReq = request.mutate().path(&quot;\/admin\/login\/&quot; + newPath).build();\n            ServerWebExchange authErrorExchange = exchange.mutate().request(authErrorReq).build();\n            return chain.filter(authErrorExchange);\n        }\n        ManagerUser managerUser = managerUserRedisDao.getOneByToken(token);\n        log.info(&quot;managerUser:{}&quot;, managerUser);\n        if (managerUser == null) {\n            log.info(&quot;\u672a\u767b\u5f55\uff0c\u65e0\u6743\u8fdb\u884c\u8be5\u64cd\u4f5c&quot;);\n            ServerHttpRequest authErrorReq = request.mutate().path(&quot;\/admin\/login\/&quot; + newPath).build();\n            ServerWebExchange authErrorExchange = exchange.mutate().request(authErrorReq).build();\n            return chain.filter(authErrorExchange);\n        }\n        int nowSec = ServerTimer.distOfSecond();\n        int lastTime = managerUser.getLastTime();\n        log.info(&quot;nowSec:{} \uff0clastTime:{}&quot;, nowSec, lastTime);\n        if (nowSec - lastTime &gt; 3600) {  \/\/\u8d85\u65f6\u65f6\u95f4\u79d2\u6570\n            log.info(&quot;\u767b\u5f55\u8d85\u65f6\u91cd\u65b0\u767b\u5f55&quot;);\n            ServerHttpRequest authErrorReq = request.mutate().path(&quot;\/admin\/login\/&quot; + newPath).build();\n            ServerWebExchange authErrorExchange = exchange.mutate().request(authErrorReq).build();\n            return chain.filter(authErrorExchange);\n        }\n        return chain.filter(exchange);\n    }\n}\n<\/pre><\/div>\n\n\n<h4><span class=\"ez-toc-section\" id=\"Controller%E5%B1%82\"><\/span>Controller\u5c42<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n    @Autowired\n    private ManagerUserDao managerUserDao;\n\n    @RequestMapping(&quot;\/loginform&quot;)\n    public String loginform(@RequestParam(value = &quot;username&quot;) String username\n            , @RequestParam(value = &quot;password&quot;) String password\n            , @RequestParam(value = &quot;uri&quot;) String uri\n            , ServerWebExchange exchange, Model model) {\n        log.info(&quot;username:{},password:{},uri:{}&quot;, username, password, uri);\n        String reluri = uri.replace(&quot;_&quot;, &quot;\/&quot;);\n        String res = thymeleafService.managerUserLogin(username, password);\n\n        if (!res.contains(&quot;token&quot;)) {\n            log.info(&quot;manager==null&quot;);\n            model.addAttribute(&quot;info&quot;, new Dson().put(&quot;value&quot;, res)._Value());\n            model.addAttribute(&quot;uri&quot;, new Dson().put(&quot;value&quot;, reluri)._Value());\n            return &quot;admin\/login&quot;;\n        }\n        String&#91;] tokenSplit = res.split(&quot;=&quot;);\n        ResponseCookie cookie = ResponseCookie.from(&quot;token&quot;, tokenSplit&#91;1]).build();\n        exchange.getResponse().addCookie(cookie);\n        Object result = thymeleafService.indexdata();\n\n        model.addAttribute(&quot;result&quot;, result);\n\n        return &quot;redirect:&quot; + reluri;\n    }\n\n    @RequestMapping(&quot;\/admin\/login\/{uri}&quot;)\n    public String login(Model model, @PathVariable(&quot;uri&quot;) String uri) {\n        model.addAttribute(&quot;info&quot;, new Dson().put(&quot;value&quot;, &quot;      &quot;)._Value());\n        model.addAttribute(&quot;uri&quot;, new Dson().put(&quot;value&quot;, uri)._Value());\n        return &quot;admin\/login&quot;;\n    }\n<\/pre><\/div>\n\n\n<h4><span class=\"ez-toc-section\" id=\"Service%E5%B1%82\"><\/span>Service\u5c42<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n  \/**\n     * \u7ba1\u7406\u5458\u767b\u5f55\n     *\n     * @param username \u7528\u6237\u540d\n     * @param password \u7528\u6237\u5bc6\u7801\n     * @return token\n     *\/\n    public String managerUserLogin(String username, String password) {\n        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {\n            return &quot;\u8d26\u53f7\u6216\u5bc6\u7801\u4e3a\u7a7a&quot;;\n        }\n        ManagerUser managerUser = managerUserRedisDao.getOneByUsername(username);\n        if (managerUser == null) {\n            return &quot;\u7528\u6237\u540d\u6216\u5bc6\u7801\u6709\u8bef&quot;;\n        }\n        if (!StringUtils.pathEquals(managerUser.getPassword(), password)) {\n            return &quot;\u7528\u6237\u540d\u6216\u5bc6\u7801\u6709\u8bef&quot;;\n        }\n        String tokenMD5 = chessService.tokenMD5(username, password);\n        managerUser.setToken(tokenMD5);\n        managerUser.setLastTime(ServerTimer.distOfSecond());\n        managerUser.setLastTimeStr(ServerTimer.getFullWithS());\n        managerUserRedisDao.save(managerUser);\n        return &quot;token=&quot; + tokenMD5;\n    }\n<\/pre><\/div>\n\n\n<h4><span class=\"ez-toc-section\" id=\"%E7%99%BB%E9%99%86%E9%A1%B5%E9%9D%A2\"><\/span>\u767b\u9646\u9875\u9762<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot; xmlns:th=&quot;http:\/\/www.thymeleaf.org\/&quot;&gt;\n&lt;head&gt;\n    &lt;title&gt;Login Form&lt;\/title&gt;\n    &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text\/html; charset=utf-8&quot;\/&gt;\n    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;\n    &lt;link href=&quot;..\/..\/static\/css\/loginstyle.css&quot; rel='stylesheet' type='text\/css'\/&gt;\n    &lt;script src=&quot;https:\/\/cdn.staticfile.org\/jquery\/2.1.1\/jquery.min.js&quot;&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;div class=&quot;main&quot;&gt;\n    &lt;div class=&quot;login&quot;&gt;\n        &lt;h1&gt;\u5de7\u591a\u540e\u53f0\u6570\u636e\u7ba1\u7406&lt;\/h1&gt;\n        &lt;div class=&quot;inset&quot;&gt;\n            &lt;!--start-main--&gt;\n            &lt;form name=&quot;myForm&quot; id=&quot;myForm&quot; action=&quot;\/loginform&quot; method=&quot;get&quot; &gt;\n                &lt;div&gt;\n                    &lt;h2&gt;\u7ba1\u7406\u5458\u767b\u5f55&lt;\/h2&gt;\n                    &lt;h4 th:text=&quot;${info.value}&quot; style=&quot;text-align: center;color: #e54d42&quot;&gt;&lt;\/h4&gt;\n                    &lt;input type=&quot;hidden&quot; th:value=&quot;${uri.value}&quot; name=&quot;uri&quot;&gt;\n                    &lt;span&gt;&lt;label&gt;\u7528\u6237\u540d&lt;\/label&gt;&lt;\/span&gt;\n                    &lt;span&gt;&lt;input type=&quot;text&quot; class=&quot;textbox&quot; name=&quot;username&quot;&gt;&lt;\/span&gt;\n                &lt;\/div&gt;\n                &lt;div&gt;\n                    &lt;span&gt;&lt;label&gt;\u5bc6\u7801&lt;\/label&gt;&lt;\/span&gt;\n                    &lt;span&gt;&lt;input type=&quot;password&quot; class=&quot;password&quot; name=&quot;password&quot;&gt;&lt;\/span&gt;\n                &lt;\/div&gt;\n                &lt;div class=&quot;sign&quot;&gt;\n                    &lt;input type=&quot;submit&quot; value=&quot;\u767b\u5f55&quot; class=&quot;submit&quot; onclick=&quot;submitForm()&quot;\/&gt;\n                &lt;\/div&gt;\n            &lt;\/form&gt;\n        &lt;\/div&gt;\n    &lt;\/div&gt;\n    &lt;!--\/\/end-main--&gt;\n&lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre><\/div>\n\n\n<p>\u5404\u79cd\u8df3\u8f6c\u662f\u57fa\u4e8e\u73b0\u6709\u7684\u8df3\u8f6c\u4e60\u60ef\u8fdb\u884c\u4fee\u6539\u7684\uff0c\u672a\u767b\u5f55\u76f4\u63a5\u8fdb\u5165\u767b\u9646\uff0c\u767b\u9646\u5931\u8d25\u4e5f\u4f1a\u901a\u8fc7themleaf\u9884\u7559\u7684\u7684info\u503c\u8fdb\u884c\u663e\u793a\u539f\u56e0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42: \u540e\u53f0\u7ba1\u7406\u7cfb\u7edf\u7684\u6570\u636e\u4fee\u6539\u90e8\u5206\u9700\u8981\u4e00\u4e2a\u7b80\u6613\u7684\u8fdb\u884c\u767b\u9646\u6743\u9650\u9a8c\u8bc1 \u539f\u672c\u53ef\u4ee5\u7684\u8bdd\u6253\u7b97\u7528\u7684\u662fshrio\u6846\u67b6\u8fdb\u884c\u6743\u9650<span class=\"more-button\"><a href=\"https:\/\/diuut.com\/?p=1225\" class=\"more-link\">view all . . .<span class=\"screen-reader-text\">\u57fa\u4e8eWebFlux\u8fc7\u6ee4\u5668\u4e0eThemleaf\u7684\u767b\u9646\u6743\u9650\u9a8c\u8bc1<\/span><\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":1147,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[28,26],"_links":{"self":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/1225"}],"collection":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1225"}],"version-history":[{"count":1,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/1225\/revisions"}],"predecessor-version":[{"id":1226,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/1225\/revisions\/1226"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/media\/1147"}],"wp:attachment":[{"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}