Keycloak集成Grails部署Tomcat根目录后访问根URL报404
解决方案
1. 调整Keycloak安全约束配置
问题核心是Keycloak拦截了根路径请求,导致无法转发到index.gsp。修改keycloak.json,将根路径标记为公开访问,绕过认证流程:
{ "auth-server-url": "http://localhost:8080/auth", // 注意:旧版Keycloak默认路径带/auth,需匹配你的实际部署路径 "realm": "你的Realm名称", "resource": "你的客户端ID", "public-client": true, "securityConstraints": [ { "authRoles": ["user"], "securityCollections": [ { "name": "受保护资源", "patterns": ["/web/*"] } ] }, { "securityCollections": [ { "name": "公开资源", "patterns": ["/", "/index.gsp"] } ] } ] }
2. 确认Grails路由配置
检查grails-app/conf/UrlMappings.groovy,确保根路径已正确映射到index.gsp:
class UrlMappings { static mappings = { "/"(view:"/index") "/web/**"(controller:"你的业务控制器") // 其他路由规则... } }
该配置在移除Keycloak时正常,说明路由本身无问题,只需让根路径绕过Keycloak拦截即可。
3. 检查Tomcat欢迎页配置
打开Tomcat的conf/web.xml,确认welcome-file-list包含index.gsp:
<welcome-file-list> <welcome-file>index.gsp</welcome-file> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
避免Tomcat因找不到默认欢迎页返回404。
4. 验证Grails上下文路径
确认application.yml中上下文路径为空(部署为ROOT.war时需保持为空):
server: servlet: context-path: "" # 不要设置为/web,否则根路径会指向Tomcat默认页而非应用
内容的提问来源于stack exchange,提问作者Hardik
相关产品推荐
相关产品推荐

