Spring Boot中通过Keycloak REST API获取AccessToken遇404问题
Answer
Hey there, the 404 error you're hitting is almost always related to a path mismatch or service availability—let's break down the most likely fixes:
1. Fix the Token API Path Based on Your Keycloak Version
This is the #1 culprit here. Keycloak changed its default URL structure starting in version 17 (the Quarkus-based rewrite):
- If you're using Keycloak 17 or newer: The
/authprefix was removed entirely. Your token request URL should be:http://localhost:8180/realms/springboot/protocol/openid-connect/token - If you're on Keycloak 16 or older: Your original URL with
/authis correct—skip to the next checks.
You can confirm your Keycloak version by checking the admin console login screen or running keycloak version in your terminal.
2. Make Sure Keycloak is Running and Reachable
First, test if you can access the Keycloak admin console:
- Older versions: Visit
http://localhost:8180/auth/admin - Newer versions: Visit
http://localhost:8180/admin
If the console doesn't load, your server isn't running on port 8180, or there's a network block stopping access.
3. Double-Check Your Keycloak Realm/Client Settings
Even if the path is right, these configs can break your request:
- Realm Name: Ensure the realm you created is exactly
springboot—realm names are case-sensitive! - Client Configuration:
- For your
springbootclient, make sure Direct Access Grants Enabled is turned ON (this is mandatory for thepasswordgrant type to work). - Confirm the
client_idin your Postman request matches the client ID in Keycloak (you've set it tospringboot, which aligns with your app config). - If your client is set to
confidential(notpublic), you'll need to add aclient_secretparameter to your Postman request body (you can find this secret in the client's "Credentials" tab in Keycloak).
- For your
4. Align Your Spring Boot Config with Keycloak Version
Your app's keycloak.auth-server-url uses /auth—if you're on Keycloak 17+, update this property to:
keycloak.auth-server-url=http://localhost:8180
Give these steps a try, and the 404 should disappear once the path and configs are aligned!
内容的提问来源于stack exchange,提问作者Thejus32

