2012-06-13 22 views
0

這與我尚未回答的問題https://stackoverflow.com/questions/10825422/can-grails-redirect-command-be-told-to-respect-protocol-of-the-current-request類似,但在這種情況下,我只想確保指定給控制器的某些鏈接和表單操作是明確的https。我知道我可以手動建立這些鏈接,但這會破壞標籤自動添加正確的主機名,應用程序路徑等的目的。可能與g:form和g:grails 2.x中的鏈接建立安全的https鏈接?

在完美的世界中,如果我在https頁面上,鏈接將是相對的,並且如果不是絕對的,但如果不可能的話,我會採用絕對鏈接。

感謝

回答

0

做到這將是你的grails.serverURLConfig.groovy配置爲使用HTTPS協議的最快方式。然後在您的gsp form tags和鏈接標籤中使用absolute="true"

+0

但是,這將改變所有鏈接。我只想設置特定的鏈接(因爲只有某些頁面受到https安全保護,屬於Spring Security Plugin的通道安全 – Peter

2

這個請求有一個open jira

我認爲你可以創建自己的標籤庫來完成這項工作。只需撥打g.createLink並將https替換爲https即可。

class MyTagLib { 
    def secureLink = { attrs, body -> 
    def link = g.createLink(attrs) 
    out << link.replace('http','https') 
    } 
} 
+0

很好 - 我使用自定義屬性擴展了您的方法以允許創建協議相關鏈接。 – martin

0

延伸Sergios解決方案,但迫使絕對= 「真」:

class MyTagLib { 
    def secureLink = { attrs, body -> 
    // forcing creation of an absolute url 
    attrs.absolute = 'true' 

    def link = g.createLink(attrs) 
    out << link.replaceFirst(/^http:/, 'https:') 
    } 
} 

或你的 「完美世界」 的情況下,你可以試試這個:

class MyTagLib { 
    def secureLink = { attrs, body -> 
    if (!request.isSecure()) { 
     attrs.absolute = 'true' 
     def link = g.createLink(attrs) 
     out << link.replaceFirst(/^http:/, 'https:') 
    } else { 
     out << g.createLink(attrs) 
    } 
    } 
}