You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 02:05:16