{"id":65007,"date":"2026-01-24T12:08:25","date_gmt":"2026-01-24T04:08:25","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/65007.html"},"modified":"2026-01-24T12:08:25","modified_gmt":"2026-01-24T04:08:25","slug":"%e4%bb%8ephp%e5%90%8e%e7%ab%af%e8%bd%acjava%e5%90%8e%e7%ab%af%e4%bb%a3%e7%a0%81-%e5%ae%9e%e7%8e%b0%e8%8e%b7%e5%8f%96%e5%bd%93%e5%89%8d%e7%99%bb%e5%bd%95%e4%bf%a1%e6%81%af","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/65007.html","title":{"rendered":"\u4ecePHP\u540e\u7aef\u8f6cJava\u540e\u7aef\u4ee3\u7801 \u5b9e\u73b0\u83b7\u53d6\u5f53\u524d\u767b\u5f55\u4fe1\u606f"},"content":{"rendered":"<p>\u9762\u5bf9\u4e00\u4e2a\u539f\u672c\u7531 PHP \u7f16\u5199\u7684\u540e\u7aef\u7cfb\u7edf&#xff0c;\u73b0\u5728\u9700\u8981\u5728 Java \u540e\u7aef\u4e2d\u83b7\u53d6\u5f53\u524d\u7528\u6237\u7684\u767b\u5f55\u72b6\u6001&#xff08;\u5373\u201c\u662f\u5426\u5df2\u767b\u5f55\u201d\u4ee5\u53ca\u201c\u767b\u5f55\u7528\u6237\u662f\u8c01\u201d&#xff09;&#xff0c;\u8fd9\u662f\u4e00\u4e2a\u5178\u578b\u7684\u8de8\u8bed\u8a00\u3001\u8de8\u7cfb\u7edf\u8eab\u4efd\u8ba4\u8bc1\u96c6\u6210\u95ee\u9898\u3002<\/p>\n<h2 style=\"background-color:transparent\">\u601d\u8def<\/h2>\n<h4>\u65b9\u6848\u4e00&#xff1a;\u5171\u4eab Session \u5b58\u50a8<\/h4>\n<p>\u6838\u5fc3\u601d\u60f3&#xff1a;\u8ba9 PHP \u548c Java \u5171\u4eab\u540c\u4e00\u4e2a Session \u5b58\u50a8\u540e\u7aef&#xff08;\u5982 Redis\u3001Memcached\u3001\u6570\u636e\u5e93\u7b49&#xff09;&#xff0c;Java \u901a\u8fc7\u8bfb\u53d6\u8be5\u5b58\u50a8\u4e2d\u7684 Session \u6570\u636e\u5224\u65ad\u767b\u5f55\u72b6\u6001\u3002<\/p>\n<p style=\"background-color:transparent\">\u5b9e\u73b0\u6b65\u9aa4&#xff1a;<\/p>\n<li>\u786e\u8ba4 PHP \u7684 Session \u5b58\u50a8\u65b9\u5f0f&#xff1a;\n<ul>\n<li>\u5982\u679c PHP \u4f7f\u7528\u9ed8\u8ba4\u6587\u4ef6\u5b58\u50a8&#xff0c;\u5219\u9700\u4fee\u6539\u4e3a Redis \u6216\u6570\u636e\u5e93\u3002<\/li>\n<li>\u5982\u679c\u5df2\u7ecf\u662f Redis\/Memcached&#xff0c;\u90a3\u5c31\u76f4\u63a5\u5bf9\u63a5\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u7edf\u4e00 Session ID \u4f20\u9012\u65b9\u5f0f&#xff1a;\n<ul>\n<li>\u901a\u5e38\u901a\u8fc7 Cookie&#xff08;\u5982\u00a0PHPSESSID&#xff09;\u4f20\u9012\u3002<\/li>\n<li>Java \u9700\u8981\u8bfb\u53d6\u8fd9\u4e2a Cookie&#xff0c;\u5e76\u7528\u540c\u6837\u7684 key \u53bb\u67e5\u8be2 Session \u5b58\u50a8\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u89e3\u6790 PHP \u5e8f\u5217\u5316\u7684 Session \u6570\u636e&#xff1a;\n<ul>\n<li>PHP \u9ed8\u8ba4\u4f7f\u7528\u00a0serialize()\u00a0\u5e8f\u5217\u5316 Session&#xff0c;Java \u9700\u8981\u80fd\u53cd\u5e8f\u5217\u5316&#xff08;\u53ef\u7528\u7b2c\u4e09\u65b9\u5e93\u5982\u00a0php-serialize&#xff09;\u3002<\/li>\n<li>\u6216\u8005\u8ba9 PHP \u6539\u7528 JSON \u5b58\u50a8&#xff08;\u9700\u4fee\u6539 PHP \u914d\u7f6e\u6216\u81ea\u5b9a\u4e49 Session handler&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<li>Java \u8bfb\u53d6\u5e76\u9a8c\u8bc1&#xff1a;\n<ul>\n<li>\u6839\u636e Session ID \u4ece Redis \u4e2d\u53d6\u51fa\u6570\u636e\u3002<\/li>\n<li>\u89e3\u6790\u51fa\u7528\u6237 ID\u3001\u767b\u5f55\u65f6\u95f4\u7b49\u4fe1\u606f\u3002<\/li>\n<li>\u5224\u65ad\u662f\u5426\u6709\u6548&#xff08;\u5982\u672a\u8fc7\u671f\u3001\u672a\u6ce8\u9500&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<p style=\"background-color:transparent\">\u4f18\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u65e0\u9700\u6539\u52a8\u524d\u7aef\u3002<\/li>\n<li>\u767b\u5f55\u72b6\u6001\u5b9e\u65f6\u540c\u6b65\u3002<\/li>\n<\/ul>\n<p>\u7f3a\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u9700\u8981\u4fee\u6539 PHP \u7684 Session \u5b58\u50a8\u903b\u8f91&#xff08;\u5982\u679c\u8fd8\u4e0d\u662f\u5171\u4eab\u5b58\u50a8&#xff09;\u3002<\/li>\n<li>\u9700\u5904\u7406 PHP \u5e8f\u5217\u5316\u683c\u5f0f\u517c\u5bb9\u6027\u95ee\u9898\u3002<\/li>\n<\/ul>\n<h4>\u65b9\u6848\u4e8c&#xff1a;\u901a\u8fc7 PHP \u63d0\u4f9b\u9274\u6743\u63a5\u53e3<\/h4>\n<p>\u6838\u5fc3\u601d\u60f3&#xff1a;\u8ba9 PHP \u540e\u7aef\u66b4\u9732\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u201c\u9a8c\u8bc1\u767b\u5f55\u72b6\u6001\u201d\u7684 API&#xff08;\u5982\u00a0\/api\/check-login&#xff09;&#xff0c;Java \u540e\u7aef\u5728\u9700\u8981\u65f6\u8c03\u7528\u8be5\u63a5\u53e3\u3002<\/p>\n<p style=\"background-color:transparent\">\u5b9e\u73b0\u6b65\u9aa4&#xff1a;<\/p>\n<li>\n<p>\u5728 PHP \u4e2d\u65b0\u589e\u4e00\u4e2a\u63a5\u53e3&#xff0c;\u4f8b\u5982&#xff1a;php<\/p>\n<p>\u7f16\u8f91<\/p>\n<p> \/\/ \/api\/check-login<br \/>\nsession_start();<br \/>\nif(isset($_SESSION[&#039;user_id&#039;])){<br \/>\nechojson_encode([&#039;logged_in&#039;&#061;&gt;true,&#039;user_id&#039;&#061;&gt;$_SESSION[&#039;user_id&#039;]]);<br \/>\n}else{<br \/>\nechojson_encode([&#039;logged_in&#039;&#061;&gt;false]);<br \/>\n}\n <\/li>\n<li>\n<p>Java \u540e\u7aef\u5728\u8bf7\u6c42\u4e2d\u643a\u5e26\u539f\u59cb Cookie&#xff08;\u5c24\u5176\u662f\u00a0PHPSESSID&#xff09;&#xff0c;\u5411\u8be5\u63a5\u53e3\u53d1\u8d77 HTTP \u8bf7\u6c42&#xff08;\u53ef\u4f7f\u7528 RestTemplate\u3001OkHttp \u7b49&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u6839\u636e\u8fd4\u56de\u7ed3\u679c\u5224\u65ad\u7528\u6237\u662f\u5426\u767b\u5f55\u3002<\/p>\n<\/li>\n<p style=\"background-color:transparent\">\u4f18\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u5bf9 Java \u7cfb\u7edf\u4fb5\u5165\u5c0f\u3002<\/li>\n<li>\u4e0d\u9700\u8981\u7406\u89e3 PHP Session \u683c\u5f0f\u3002<\/li>\n<li>\u5b89\u5168\u6027\u7531 PHP \u63a7\u5236\u3002<\/li>\n<\/ul>\n<p style=\"background-color:transparent\">\u7f3a\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u6bcf\u6b21\u9a8c\u8bc1\u90fd\u9700\u8981\u4e00\u6b21\u989d\u5916\u7684 HTTP \u8c03\u7528&#xff08;\u6027\u80fd\u5f00\u9500&#xff09;\u3002<\/li>\n<li>\u9700\u786e\u4fdd Java \u80fd\u6b63\u786e\u8f6c\u53d1 Cookie&#xff08;\u7279\u522b\u662f\u8de8\u57df\/\u5b50\u57df\u573a\u666f&#xff09;\u3002<\/li>\n<\/ul>\n<p>&#x1f4a1; \u53ef\u4f18\u5316&#xff1a;\u5728 Java \u7aef\u505a\u77ed\u65f6\u7f13\u5b58&#xff08;\u5982 5 \u79d2&#xff09;&#xff0c;\u907f\u514d\u9891\u7e41\u8c03\u7528\u3002<\/p>\n<h4 style=\"background-color:transparent\">\u65b9\u6848\u4e09&#xff1a;Token \u5316\u6539\u9020<\/h4>\n<p>\u6838\u5fc3\u601d\u60f3&#xff1a;\u5c06\u767b\u5f55\u6d41\u7a0b\u6539\u4e3a\u57fa\u4e8e Token&#xff08;\u5982 JWT&#xff09;&#xff0c;PHP \u767b\u5f55\u6210\u529f\u540e\u751f\u6210 Token \u5e76\u8fd4\u56de\u7ed9\u524d\u7aef&#xff0c;\u524d\u7aef\u540e\u7eed\u8bf7\u6c42\u643a\u5e26 Token&#xff0c;Java \u548c PHP \u90fd\u80fd\u72ec\u7acb\u9a8c\u8bc1\u3002<\/p>\n<p style=\"background-color:transparent\">\u5b9e\u73b0\u6b65\u9aa4&#xff1a;<\/p>\n<li>\u4fee\u6539 PHP \u767b\u5f55\u903b\u8f91&#xff1a;\u767b\u5f55\u6210\u529f\u540e\u751f\u6210 JWT&#xff08;\u5305\u542b user_id\u3001exp \u7b49&#xff09;&#xff0c;\u5199\u5165 Cookie \u6216\u8fd4\u56de\u7ed9\u524d\u7aef\u3002<\/li>\n<li>\u524d\u7aef\u6240\u6709\u8bf7\u6c42\u643a\u5e26\u8be5 Token&#xff08;Header \u6216 Cookie&#xff09;\u3002<\/li>\n<li>Java \u540e\u7aef\u4f7f\u7528\u516c\u94a5&#xff08;\u6216\u5171\u4eab\u5bc6\u94a5&#xff09;\u9a8c\u8bc1 JWT \u7b7e\u540d&#xff0c;\u89e3\u6790\u7528\u6237\u4fe1\u606f\u3002<\/li>\n<p style=\"background-color:transparent\">\u4f18\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u5f7b\u5e95\u89e3\u8026 PHP \u548c Java \u7684\u4f1a\u8bdd\u7ba1\u7406\u3002<\/li>\n<li>\u4fbf\u4e8e\u672a\u6765\u5fae\u670d\u52a1\u6269\u5c55\u3002<\/li>\n<li>\u65e0\u72b6\u6001&#xff0c;\u9002\u5408\u5206\u5e03\u5f0f\u3002<\/li>\n<\/ul>\n<p style=\"background-color:transparent\">\u7f3a\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u9700\u8981\u6539\u9020\u73b0\u6709\u767b\u5f55\u6d41\u7a0b\u3002<\/li>\n<li>Token \u65e0\u6cd5\u4e3b\u52a8\u5931\u6548&#xff08;\u9664\u975e\u5f15\u5165\u9ed1\u540d\u5355\u673a\u5236&#xff09;\u3002<\/li>\n<li>\u7532\u65b9\u53ef\u80fd\u4e0d\u613f\u6539\u52a8\u73b0\u6709 PHP \u903b\u8f91\u3002<\/li>\n<\/ul>\n<h4>\u65b9\u6848\u56db&#xff1a;\u6570\u636e\u5e93\u5171\u4eab\u7528\u6237\u72b6\u6001<\/h4>\n<p>\u6838\u5fc3\u601d\u60f3&#xff1a;PHP \u767b\u5f55\/\u767b\u51fa\u65f6\u5728\u6570\u636e\u5e93\u4e2d\u6807\u8bb0\u7528\u6237\u72b6\u6001&#xff08;\u5982\u00a0last_login_time,\u00a0is_logged_in\u00a0\u5b57\u6bb5&#xff09;&#xff0c;Java \u67e5\u8be2\u8be5\u8868\u5224\u65ad\u3002<\/p>\n<p style=\"background-color:transparent\">\u95ee\u9898&#xff1a;<\/p>\n<ul>\n<li>\u65e0\u6cd5\u51c6\u786e\u5224\u65ad\u201c\u5f53\u524d\u4f1a\u8bdd\u201d\u662f\u5426\u6709\u6548&#xff08;\u6bd4\u5982\u7528\u6237\u5173\u95ed\u6d4f\u89c8\u5668\u4f46\u672a\u767b\u51fa&#xff09;\u3002<\/li>\n<li>\u591a\u8bbe\u5907\u767b\u5f55\u65f6\u72b6\u6001\u6df7\u4e71\u3002<\/li>\n<li>\u4e0d\u7b26\u5408 HTTP \u65e0\u72b6\u6001\u539f\u5219\u3002<\/li>\n<\/ul>\n<p>\u26a0\ufe0f \u4e00\u822c\u4e0d\u63a8\u8350&#xff0c;\u4ec5\u4f5c\u5907\u9009\u3002<\/p>\n<h2 style=\"background-color:transparent\">\u5b9e\u8df5\u6b65\u9aa4<\/h2>\n<p>\u4e3a\u4e86\u5b9e\u73b0\u6539\u9020\u6700\u5c11\u539f\u5219\u9009\u62e9 \u65b9\u68482 \u589e\u52a0\u63a5\u53e3\u8fdb\u884c\u6821\u9a8c<\/p>\n<p>\u601d\u8def\u5982\u4e0b<\/p>\n<p>\u524d\u7aef\u6d4f\u89c8\u5668<br \/>\n    \u2502<br \/>\n    \u2502 (\u643a\u5e26 Token)<br \/>\n    \u25bc<br \/>\nJava \u540e\u7aef\u63a5\u53e3&#xff08;\u5982 \/api\/java\/getProfile&#xff09;<br \/>\n    \u2502<br \/>\n    \u2502 \u63d0\u53d6 Header: Token\u2192 &#034;PHPSESSID&#061;abc123&#034;<br \/>\n    \u2502<br \/>\n    \u25bc<br \/>\nJava \u5185\u90e8\u8c03\u7528 \u2192 &lt;http:\/\/php-server\/check_login.php&gt;<br \/>\n                      \u2191<br \/>\n                      \u2514\u2500\u2500 \u8bf7\u6c42\u5934\u5e26\u4e0a Token: PHPSESSID&#061;abc123<br \/>\n    \u2502<br \/>\n    \u2502 PHP \u6536\u5230\u540e&#xff1a;<br \/>\n    \u2502   &#8211; \u6839\u636e abc123 \u627e\u5230\u672c\u5730 session \u6587\u4ef6<br \/>\n    \u2502   &#8211; \u8bfb\u53d6 $_SESSION[&#039;user_id&#039;] \u7b49\u4fe1\u606f<br \/>\n    \u2502<br \/>\n    \u25bc<br \/>\nPHP \u8fd4\u56de JSON&#xff1a;<br \/>\n{<br \/>\n  &#034;logged_in&#034;: true,<br \/>\n  &#034;user_id&#034;: 10086,<br \/>\n  &#034;username&#034;: &#034;alice&#034;<br \/>\n}<br \/>\n    \u2502<br \/>\n    \u25bc<br \/>\nJava \u89e3\u6790\u7ed3\u679c \u2192 \u786e\u8ba4\u5f53\u524d\u8bf7\u6c42\u7528\u6237\u662f user_id&#061;10086<br \/>\n    \u2502<br \/>\n    \u25bc<br \/>\nJava \u7ee7\u7eed\u6267\u884c\u81ea\u5df1\u7684\u4e1a\u52a1\u903b\u8f91&#xff08;\u5982\u67e5\u6570\u636e\u5e93\u3001\u8fd4\u56de\u6570\u636e\u7b49&#xff09;<\/p>\n<h4>1. PHP \u7aef&#xff1a;\u9274\u6743\u63a5\u53e3 (check_login.php)<\/h4>\n<p>\u8fd9\u4e2a\u63a5\u53e3\u975e\u5e38\u7b80\u5355&#xff0c;\u5b83\u53ea\u9700\u8981\u542f\u52a8 Session \u5e76\u8fd4\u56de\u5f53\u524d\u7528\u6237\u7684\u552f\u4e00\u6807\u8bc6&#xff08;\u5982 user_id&#xff09;\u3002<\/p>\n<p>PHP<\/p>\n<p>&lt;?php<br \/>\n\/\/ \u5141\u8bb8 Java \u540e\u7aef\u6240\u5728\u7684\u5185\u90e8 IP \u8bbf\u95ee&#xff08;\u53ef\u9009\u5b89\u5168\u6027\u589e\u5f3a&#xff09;<br \/>\n\/\/ session_start \u4f1a\u81ea\u52a8\u6839\u636e\u4f20\u5165\u7684 PHPSESSID \u627e\u5230\u5bf9\u5e94\u7684\u6587\u4ef6 Session<br \/>\nsession_start();<\/p>\n<p>header(&#039;Content-Type: application\/json&#039;);<\/p>\n<p>if (isset($_SESSION[&#039;user_id&#039;])) {<br \/>\n    echo json_encode([<br \/>\n        &#039;status&#039; &#061;&gt; &#039;success&#039;,<br \/>\n        &#039;user_id&#039; &#061;&gt; $_SESSION[&#039;user_id&#039;],<br \/>\n        &#039;username&#039; &#061;&gt; $_SESSION[&#039;username&#039;] ?? &#039;&#039;<br \/>\n    ]);<br \/>\n} else {<br \/>\n    http_response_code(401); \/\/ \u672a\u767b\u5f55<br \/>\n    echo json_encode([<br \/>\n        &#039;status&#039; &#061;&gt; &#039;error&#039;,<br \/>\n        &#039;message&#039; &#061;&gt; &#039;Unauthorized&#039;<br \/>\n    ]);<br \/>\n}<\/p>\n<hr \/>\n<h4>2. Java \u7aef&#xff1a;\u914d\u7f6e\u4e0e\u5de5\u5177\u7c7b\u5b9e\u73b0<\/h4>\n<h5 style=\"background-color:transparent\">A. \u914d\u7f6e\u6587\u4ef6 (application-dev.yml)<\/h5>\n<p>\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u5b9a\u4e49 PHP \u63a5\u53e3\u7684\u5730\u5740\u3002<\/p>\n<p>YAML<\/p>\n<p>php:<br \/>\n  auth:<br \/>\n    check-url: &lt;http:\/\/127.0.0.1:80\/api\/check_login.php&gt;<\/p>\n<h5 style=\"background-color:transparent\">B. \u6838\u5fc3\u903b\u8f91&#xff1a;\u83b7\u53d6\u5f53\u524d\u767b\u5f55\u7528\u6237\u7684\u5c01\u88c5\u7c7b<\/h5>\n<p>\u6211\u4eec\u9700\u8981\u5229\u7528 RestTemplate \u6765\u53d1\u9001\u8bf7\u6c42&#xff0c;\u5e76\u624b\u52a8\u5c06\u524d\u7aef\u4f20\u7ed9 Java \u7684 Cookie \u653e\u5165 Header \u4e2d\u4f20\u7ed9 PHP\u3002<\/p>\n<p>Java<\/p>\n<p>import lombok.extern.slf4j.Slf4j;<br \/>\nimport org.springframework.beans.factory.annotation.Value;<br \/>\nimport org.springframework.http.*;<br \/>\nimport org.springframework.stereotype.Component;<br \/>\nimport org.springframework.web.client.RestTemplate;<br \/>\nimport org.springframework.web.context.request.RequestContextHolder;<br \/>\nimport org.springframework.web.context.request.ServletRequestAttributes;<\/p>\n<p>import javax.servlet.http.HttpServletRequest;<br \/>\nimport java.util.Map;<\/p>\n<p>&#064;Component<br \/>\n&#064;Slf4j<br \/>\npublic class PhpSessionProxy {<\/p>\n<p>    &#064;Value(&#034;${php.auth.check-url}&#034;)<br \/>\n    private String phpCheckUrl;<\/p>\n<p>    private final RestTemplate restTemplate &#061; new RestTemplate();<\/p>\n<p>    \/**<br \/>\n     * \u6838\u5fc3\u65b9\u6cd5&#xff1a;\u83b7\u53d6\u5f53\u524d PHP \u767b\u5f55\u7528\u6237\u7684\u4fe1\u606f<br \/>\n     *\/<br \/>\n    public Map&lt;String, Object&gt; getCurrentUser() {<br \/>\n        \/\/ 1. \u4ece\u5f53\u524d\u4e0a\u4e0b\u6587\u4e2d\u83b7\u53d6\u524d\u7aef\u53d1\u7ed9 Java \u7684 Request<br \/>\n        ServletRequestAttributes attributes &#061; (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();<br \/>\n        if (attributes &#061;&#061; null) return null;<br \/>\n        HttpServletRequest request &#061; attributes.getRequest();<\/p>\n<p>        \/\/ 2. \u63d0\u53d6\u6240\u6709\u7684 Cookie<br \/>\n        String cookieHeader &#061; request.getHeader(HttpHeaders.COOKIE);<br \/>\n        if (cookieHeader &#061;&#061; null || cookieHeader.isEmpty()) {<br \/>\n            log.warn(&#034;\u8bf7\u6c42\u4e2d\u672a\u53d1\u73b0 Cookie&#xff0c;\u7528\u6237\u672a\u767b\u5f55&#034;);<br \/>\n            return null;<br \/>\n        }<\/p>\n<p>        try {<br \/>\n            \/\/ 3. \u6784\u9020\u8bf7\u6c42\u5934&#xff0c;\u5c06 Cookie \u900f\u4f20\u7ed9 PHP<br \/>\n            HttpHeaders headers &#061; new HttpHeaders();<br \/>\n            headers.add(HttpHeaders.COOKIE, cookieHeader);<br \/>\n            HttpEntity&lt;String&gt; entity &#061; new HttpEntity&lt;&gt;(headers);<\/p>\n<p>            \/\/ 4. \u8c03\u7528 PHP \u63a5\u53e3<br \/>\n            ResponseEntity&lt;Map&gt; response &#061; restTemplate.exchange(<br \/>\n                    phpCheckUrl,<br \/>\n                    HttpMethod.GET,<br \/>\n                    entity,<br \/>\n                    Map.class<br \/>\n            );<\/p>\n<p>            if (response.getStatusCode() &#061;&#061; HttpStatus.OK &amp;&amp; response.getBody() !&#061; null) {<br \/>\n                return (Map&lt;String, Object&gt;) response.getBody();<br \/>\n            }<br \/>\n        } catch (Exception e) {<br \/>\n            log.error(&#034;PHP \u9274\u6743\u63a5\u53e3\u8c03\u7528\u5931\u8d25: {}&#034;, e.getMessage());<br \/>\n        }<\/p>\n<p>        return null;<br \/>\n    }<br \/>\n}<\/p>\n<hr \/>\n<h4 style=\"background-color:transparent\">3. Java \u7aef&#xff1a;\u5728\u4e1a\u52a1 Controller \u4e2d\u4f7f\u7528<\/h4>\n<p>Java<\/p>\n<p>&#064;RestController<br \/>\n&#064;RequestMapping(&#034;\/api\/orders&#034;)<br \/>\npublic class OrderController {<\/p>\n<p>    private final PhpSessionProxy phpSessionProxy;<\/p>\n<p>    public OrderController(PhpSessionProxy phpSessionProxy) {<br \/>\n        this.phpSessionProxy &#061; phpSessionProxy;<br \/>\n    }<\/p>\n<p>    &#064;GetMapping(&#034;\/list&#034;)<br \/>\n    public ResponseEntity&lt;?&gt; getMyOrders() {<br \/>\n        \/\/ \u8c03\u7528\u5c01\u88c5\u597d\u7684\u65b9\u6cd5<br \/>\n        Map&lt;String, Object&gt; userInfo &#061; phpSessionProxy.getCurrentUser();<\/p>\n<p>        if (userInfo &#061;&#061; null) {<br \/>\n            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(&#034;\u8bf7\u5148\u5728 PHP \u7aef\u767b\u5f55&#034;);<br \/>\n        }<\/p>\n<p>        \/\/ \u62ff\u5230 PHP \u4f20\u8fc7\u6765\u7684 user_id \u8fdb\u884c Java \u4fa7\u7684\u4e1a\u52a1\u903b\u8f91<br \/>\n        Object userId &#061; userInfo.get(&#034;user_id&#034;);<br \/>\n        return ResponseEntity.ok(&#034;\u5f53\u524d\u767b\u5f55\u7528\u6237 ID: &#034; &#043; userId &#043; &#034;&#xff0c;\u8fd9\u662f\u6765\u81ea Java \u540e\u7aef\u7684\u8ba2\u5355\u6570\u636e&#034;);<br \/>\n    }<br \/>\n}<\/p>\n<hr \/>\n<h3 style=\"background-color:transparent\">\u4f18\u5316<\/h3>\n<p>\u5c06\u5176\u5c01\u88c5\u4e3a\u9759\u6001\u65b9\u6cd5<\/p>\n<p>import cn.hutool.core.util.StrUtil;<br \/>\nimport cn.hutool.http.HttpRequest;<br \/>\nimport cn.hutool.json.JSONUtil;<br \/>\nimport lombok.extern.slf4j.Slf4j;<br \/>\nimport org.springframework.beans.factory.annotation.Value;<br \/>\nimport org.springframework.stereotype.Component;<br \/>\nimport org.springframework.web.context.request.RequestContextHolder;<br \/>\nimport org.springframework.web.context.request.ServletRequestAttributes;<\/p>\n<p>import javax.annotation.PostConstruct;<br \/>\nimport javax.servlet.http.HttpServletRequest;<br \/>\nimport java.util.Map;<\/p>\n<p>&#064;Slf4j<br \/>\n&#064;Component<br \/>\npublic class PhpAuthUtil {<\/p>\n<p>    private static String checkUrl;<\/p>\n<p>    &#064;Value(&#034;${php.auth.check-url}&#034;)<br \/>\n    private String configUrl;<\/p>\n<p>    &#064;PostConstruct<br \/>\n    public void init() {<br \/>\n        checkUrl &#061; this.configUrl;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u9759\u6001\u65b9\u6cd5&#xff1a;\u901a\u8fc7 Header \u4f20\u53c2\u8c03\u7528 PHP \u9274\u6743<br \/>\n     *\/<br \/>\n    public static Map&lt;String, Object&gt; getCurrentUser() {<br \/>\n        try {<br \/>\n            \/\/ 1. \u83b7\u53d6\u5f53\u524d Java \u63a5\u6536\u5230\u7684\u8bf7\u6c42\u5bf9\u8c61<br \/>\n            ServletRequestAttributes attributes &#061; (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();<br \/>\n            if (attributes &#061;&#061; null) return null;<br \/>\n            HttpServletRequest request &#061; attributes.getRequest();<\/p>\n<p>            \/\/ 2. \u83b7\u53d6\u524d\u7aef\u4f20\u7ed9 Java \u7684 Token (\u5047\u8bbe\u524d\u7aef\u4f20\u7684\u4e5f\u662f Token)<br \/>\n            String tokenValue &#061; request.getHeader(&#034;Token&#034;);<br \/>\n            if (StrUtil.isBlank(tokenValue)) {<br \/>\n                log.warn(&#034;\u8bf7\u6c42\u5934\u4e2d\u7f3a\u5931 Token&#034;);<br \/>\n                return null;<br \/>\n            }<\/p>\n<p>            \/\/ 3. \u3010\u6838\u5fc3\u6b65\u9aa4\u3011\u4f7f\u7528 Hutool \u53d1\u8d77\u5e26\u81ea\u5b9a\u4e49 Header \u7684\u8bf7\u6c42<br \/>\n            \/\/ .header(&#034;Token&#034;, tokenValue) \u5c31\u4f1a\u751f\u6210 Token: xxx \u8fd9\u79cd\u683c\u5f0f<br \/>\n            String result &#061; HttpRequest.get(checkUrl)<br \/>\n                    .header(&#034;Token&#034;, tokenValue)<br \/>\n                    .timeout(2000) \/\/ \u8bbe\u7f6e 2 \u79d2\u8d85\u65f6&#xff0c;\u9632\u6b62 PHP \u5361\u6b7b\u62d6\u57ae Java<br \/>\n                    .execute()<br \/>\n                    .body();<\/p>\n<p>            if (StrUtil.isBlank(result)) return null;<\/p>\n<p>            log.info(&#034;PHP \u63a5\u53e3\u8fd4\u56de: {}&#034;, result);<\/p>\n<p>            \/\/ 4. \u89e3\u6790\u5e76\u8fd4\u56de<br \/>\n            return JSONUtil.parseObj(result.trim());<\/p>\n<p>        } catch (Exception e) {<br \/>\n            log.error(&#034;PHP \u9274\u6743\u8c03\u7528\u5f02\u5e38: {}&#034;, e.getMessage());<br \/>\n            return null;<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<h4>\u8c03\u7528<\/h4>\n<p>&#064;GetMapping(&#034;\/api\/info&#034;)<br \/>\npublic Result getInfo() {<br \/>\n    Map&lt;String, Object&gt; user &#061; PhpAuthUtil.getCurrentUser();<br \/>\n    if (user &#061;&#061; null) {<br \/>\n        return Result.error(&#034;\u672a\u767b\u5f55&#034;);<br \/>\n    }<br \/>\n    return Result.ok(user.get(&#034;user_id&#034;));<br \/>\n}<\/p>\n<p>\u5230\u6b64\u4efb\u52a1\u7ed3\u675f\u3002<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9762\u5bf9\u4e00\u4e2a\u539f\u672c\u7531 PHP \u7f16\u5199\u7684\u540e\u7aef\u7cfb\u7edf&#xff0c;\u73b0\u5728\u9700\u8981\u5728 Java \u540e\u7aef\u4e2d\u83b7\u53d6\u5f53\u524d\u7528\u6237\u7684\u767b\u5f55\u72b6\u6001&#xff08;\u5373\u201c\u662f\u5426\u5df2\u767b\u5f55\u201d\u4ee5\u53ca\u201c\u767b\u5f55\u7528\u6237\u662f\u8c01\u201d&#xff09;&#xff0c;\u8fd9\u662f\u4e00\u4e2a\u5178\u578b\u7684\u8de8\u8bed\u8a00\u3001\u8de8\u7cfb\u7edf\u8eab\u4efd\u8ba4\u8bc1\u96c6\u6210\u95ee\u9898\u3002<br \/>\n\u601d\u8def<br \/>\n\u65b9\u6848\u4e00&#xff1a;\u5171\u4eab Session \u5b58\u50a8<br \/>\n\u6838\u5fc3\u601d\u60f3&#xff1a;\u8ba9 PHP \u548c Java \u5171\u4eab\u540c\u4e00\u4e2a Session \u5b58\u50a8\u540e\u7aef&#xff08;\u5982 Redis\u3001Memcached\u3001\u6570\u636e\u5e93\u7b49&#xff09;&#xff0c;Ja<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99,230,190],"topic":[],"class_list":["post-65007","post","type-post","status-publish","format-standard","hentry","category-server","tag-java","tag-php","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u4ecePHP\u540e\u7aef\u8f6cJava\u540e\u7aef\u4ee3\u7801 \u5b9e\u73b0\u83b7\u53d6\u5f53\u524d\u767b\u5f55\u4fe1\u606f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/65007.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4ecePHP\u540e\u7aef\u8f6cJava\u540e\u7aef\u4ee3\u7801 \u5b9e\u73b0\u83b7\u53d6\u5f53\u524d\u767b\u5f55\u4fe1\u606f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u9762\u5bf9\u4e00\u4e2a\u539f\u672c\u7531 PHP \u7f16\u5199\u7684\u540e\u7aef\u7cfb\u7edf&#xff0c;\u73b0\u5728\u9700\u8981\u5728 Java \u540e\u7aef\u4e2d\u83b7\u53d6\u5f53\u524d\u7528\u6237\u7684\u767b\u5f55\u72b6\u6001&#xff08;\u5373\u201c\u662f\u5426\u5df2\u767b\u5f55\u201d\u4ee5\u53ca\u201c\u767b\u5f55\u7528\u6237\u662f\u8c01\u201d&#xff09;&#xff0c;\u8fd9\u662f\u4e00\u4e2a\u5178\u578b\u7684\u8de8\u8bed\u8a00\u3001\u8de8\u7cfb\u7edf\u8eab\u4efd\u8ba4\u8bc1\u96c6\u6210\u95ee\u9898\u3002 \u601d\u8def \u65b9\u6848\u4e00&#xff1a;\u5171\u4eab Session \u5b58\u50a8 \u6838\u5fc3\u601d\u60f3&#xff1a;\u8ba9 PHP \u548c Java \u5171\u4eab\u540c\u4e00\u4e2a Session \u5b58\u50a8\u540e\u7aef&#xff08;\u5982 Redis\u3001Memcached\u3001\u6570\u636e\u5e93\u7b49&#xff09;&#xff0c;Ja\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/65007.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-24T04:08:25+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/65007.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/65007.html\",\"name\":\"\u4ecePHP\u540e\u7aef\u8f6cJava\u540e\u7aef\u4ee3\u7801 \u5b9e\u73b0\u83b7\u53d6\u5f53\u524d\u767b\u5f55\u4fe1\u606f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-24T04:08:25+00:00\",\"dateModified\":\"2026-01-24T04:08:25+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/65007.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/65007.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/65007.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4ecePHP\u540e\u7aef\u8f6cJava\u540e\u7aef\u4ee3\u7801 \u5b9e\u73b0\u83b7\u53d6\u5f53\u524d\u767b\u5f55\u4fe1\u606f\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u4ecePHP\u540e\u7aef\u8f6cJava\u540e\u7aef\u4ee3\u7801 \u5b9e\u73b0\u83b7\u53d6\u5f53\u524d\u767b\u5f55\u4fe1\u606f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/65007.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4ecePHP\u540e\u7aef\u8f6cJava\u540e\u7aef\u4ee3\u7801 \u5b9e\u73b0\u83b7\u53d6\u5f53\u524d\u767b\u5f55\u4fe1\u606f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u9762\u5bf9\u4e00\u4e2a\u539f\u672c\u7531 PHP \u7f16\u5199\u7684\u540e\u7aef\u7cfb\u7edf&#xff0c;\u73b0\u5728\u9700\u8981\u5728 Java \u540e\u7aef\u4e2d\u83b7\u53d6\u5f53\u524d\u7528\u6237\u7684\u767b\u5f55\u72b6\u6001&#xff08;\u5373\u201c\u662f\u5426\u5df2\u767b\u5f55\u201d\u4ee5\u53ca\u201c\u767b\u5f55\u7528\u6237\u662f\u8c01\u201d&#xff09;&#xff0c;\u8fd9\u662f\u4e00\u4e2a\u5178\u578b\u7684\u8de8\u8bed\u8a00\u3001\u8de8\u7cfb\u7edf\u8eab\u4efd\u8ba4\u8bc1\u96c6\u6210\u95ee\u9898\u3002 \u601d\u8def \u65b9\u6848\u4e00&#xff1a;\u5171\u4eab Session \u5b58\u50a8 \u6838\u5fc3\u601d\u60f3&#xff1a;\u8ba9 PHP \u548c Java \u5171\u4eab\u540c\u4e00\u4e2a Session \u5b58\u50a8\u540e\u7aef&#xff08;\u5982 Redis\u3001Memcached\u3001\u6570\u636e\u5e93\u7b49&#xff09;&#xff0c;Ja","og_url":"https:\/\/www.wsisp.com\/helps\/65007.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-24T04:08:25+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/65007.html","url":"https:\/\/www.wsisp.com\/helps\/65007.html","name":"\u4ecePHP\u540e\u7aef\u8f6cJava\u540e\u7aef\u4ee3\u7801 \u5b9e\u73b0\u83b7\u53d6\u5f53\u524d\u767b\u5f55\u4fe1\u606f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-24T04:08:25+00:00","dateModified":"2026-01-24T04:08:25+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/65007.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/65007.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/65007.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4ecePHP\u540e\u7aef\u8f6cJava\u540e\u7aef\u4ee3\u7801 \u5b9e\u73b0\u83b7\u53d6\u5f53\u524d\u767b\u5f55\u4fe1\u606f"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/65007","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=65007"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/65007\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=65007"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=65007"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=65007"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=65007"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}