简体   繁体   中英

How do I set grails.serverURL in an enterprise environment when I don't know the server name and port ahead of time?

We have an application in Grails 2.0 that we have working when we run on localhost:8080 . In Config.groovy , the application has a grails.serverURL property that has to be set for the current host and current app name. However, in our QA environment, the middleware team takes the war we give them and deploys it to a server whose name and port we don't know ahead of time.

It seems like Grails assumes that you are always going to register a domain name and then have full control over that name and the port the app is running, which is not the case at all.

What is the best way to address this problem? We have tried simply removing grails.serverURL , but Spring Security appears to use it for building up the url for redirects on login success and logout success.

development { grails.serverURL = "http://www.notused.com/${appName}" }
production { grails.serverURL = "<we don't know yet>" }

And before anyone asks, I have read numerous posts on StackOverflow and elsewhere that relate to this issue but do not answer this specific question.

No, Spring Security doesn't use it. It should always be safe to remove the serverURL property and let Grails generate urls relative to the current running app.

Typically the only place you need this property is when you're generating emails and want to embed the server url so users can click back to your site. Since these are often sent asynchronously and not during a server request, there's no way to know the server address without a config option. But if you're not doing that, it's safe to omit.

Grails has a built-in mechanism called Externalized Configuration that allows you to one or more optional Config.groovy files outside your application.

You add something like this to your Config.groovy :

grails.config.locations = [
    "classpath:${appName}-config.properties",
    "classpath:${appName}-config.groovy",
    "file:${userHome}/.grails/${appName}-config.properties",
    "file:${userHome}/.grails/${appName}-config.groovy" ]

Then include in any one of those files on your server this content:

grails.serverURL = "http://example.com/"

The external config will override the base config.

Externalize the serverURL into a property that can be set by your Middleware team. For example, place in a config.properties file and have it as part of your property config for Spring. It's generally a best practice to have properties externalized so you can pass the WAR around and have it deployed and configured without needing to recompile and repackage.

See here for better example: https://stackoverflow.com/a/973375/463226

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM