Grails 2.5.6 createLink does not take into account whether the application is on HTTPS server or not?

时间:2018-01-15 18:15:44

标签: grails

I have application which is being accessed in 3 different ways 1. client's internel ip which is only accessible from his machines, 2. client's external ip which is only accessible to use on our vpn server. 3. client's secure domain name which is also only accessible to his intranet.

Issue is that website when being accessed from HTTPS URL is not generating correct link starting from https rather generates link starting from http.

As a result, the AJAX call fails because of web security restrictions which prevents http calls from https server.

IP addresses works fine for now. I tried using grails.severURL to explicitly mention the https url it works for https url but that breaks the website functionality on IP addresses.

1 个答案:

答案 0 :(得分:0)

createLink将其绝对URL基于您在应用程序的Config.groovy文件中指定的设置grails.serverURL。

https://grails.github.io/grails2-doc/2.5.6/ref/Tags/createLink.html

它说:

  

absolute(可选) - 如果为true,则为链接目标地址添加前缀   来自Config.groovy的grails.serverURL属性的值,或   http://localhost:如果Config.groovy中没有设置   没有在生产中运行。

然后,如果您不想使用grails.serverURL,则必须更改gsp中的代码。 您应该使用createLink生成相对路径,例如:

// generates "/shop/book/list"
<g:createLink controller="book" action="list" />

添加参数absolue =&#39; false&#39;如果它继续生成绝对URL

然后你可以将这种网址传递给你的ajax调用它应该可以正常工作,如果你正在使用jquery进行通话。

但是如果你想在代码中使用绝对url,那么你应该将createLink的相对路径与另一个技巧结合起来:

<g:set var="url" value="${request.requestURL}" />
<g:set var="uri" value="${request.requestURI}" />
<g:set var="yourUrlForAjaxCall">${url.substring(0, url.length() - uri.length())}${request.contextPath}${createLink(action: 'list', controller: 'book')}</g:set>

然后变量yourUrlForAjaxCall给出了你需要的东西。

我的回答是基于BalusC回答here