WeChat QR Login in Java: WxJava SDK vs Raw OAuth 2.0 Calls
Any Java backend integrating WeChat login has to meet the callback-domain requirement and handle state validation correctly, or users can't authenticate at all. The dual choice between a wrapper SDK and raw HTTP calls applies to any OAuth integration where you trade boilerplate for control.
WeChat's QR login flow follows OAuth 2.0 but adds WeChat-specific constraints: an approved website app on the Open Platform, an ICP-registered callback domain, and a state parameter for CSRF protection. The backend builds an authorization URL, the frontend renders it as a QR code, and after the user scans, WeChat redirects with a code. That code exchanges for an access_token and openid, which then pulls user info and completes local login.
Two implementation paths exist. The WxJava SDK (version 4.6.0 shown) wraps token management and API calls, cutting boilerplate; a couple of methods handle URL generation and callback processing. The alternative runs manual HTTP calls with RestTemplate or WebClient, building the URL and token exchange yourself, which means more code but zero external dependencies.
Security and design get clear treatment. The state parameter must be generated server-side, stored in session or Redis, and compared against the callback's state, or login is rejected. openid serves as the unique user key within a single website app, while cross-app identity needs unionid, which requires all apps under the same Open Platform account. A practical note: the callback domain must match exactly, and ngrok can provide a temporary public domain during development.
The callback domain requirement is a hard prerequisite that forces a publicly reachable endpoint before any login can work, which is unusual among OAuth providers that allow localhost redirects.
openid's per-app scoping means a user's identity is fragmented across different website apps unless explicitly bound to a unionid, so multi-app integrations need identity planning up front.
WxJava's encapsulation cuts boilerplate but also hides the token exchange, so developers who need to understand the OAuth flow will get more from the manual approach.