HTTPS Grails application URL - grails

My Grails app runs over HTTPS and in Config.groovy I've set:
environments {
development {
grails.serverURL = "https://localhost:8443/foo"
}
}
When I execute grails run-app to start the app in dev mode, the last message printed on the console is:
Server running. Browse to http://localhost:8082/foo
If I accidentally click on this URL to access the application, I get various errors due to the same-origin security policy (because https://localhost:8443 is a different host to http://localhost:8082).
Why is Grails prompting me to access my app via http://localhost:8082/foo when I've set grails.serverURL = "https://localhost:8443/foo"
Update
I changed the startup command to grails run-app -https and the last message printed on the console is now:
Server running. Browse to http://localhost:8082/foo or https://localhost:8443/foo
Why am I given the option of either HTTP or HTTPS, rather than just the latter? Also, I get this exception during startup:
http11.Http11Protocol Failed to initialize end point associated with ProtocolHandler ["http-bio-8443"]
java.net.BindException: Address already in use <null>:8443
at org.apache.tomcat.util.net.JIoEndpoint.bind(JIoEndpoint.java:407)
I've checked if port 8443 is in use before I run this command (it isn't), but the server seems to startup despite this exception, so this is not a major concern.

Replace grails.serverURL with the code below.
Its possible to set the port with the following system properties:
grails.server.port = 8082
That should work for both http and https. To configure for just one:
grails.server.port.https = 8082

You need to use the --https parameter to run-app for Grails to respond to HTTPS and HTTP in development mode. At least according to the documentation.
Try using
grails run-app --https
Further more, the grails.serverURL is typically used by taglibs and plugins, and not the launching process as far as I can tell.

You can set the ssl port you want to run you application on via https in BuildConfig.groovy. You can add the following to your BuildConfig.groovy
//You can specify another port here to get rid of your startup exception
grails.server.port.https="8443"
grails.server.host="localhost"
Then try grails run-app -https and you should be able to run your app on the ssl port you defined or the default 8080 port

Related

Configuration for grails-redis plugin

I'm attempting to use the grails-redis plugin so that I can implement a distributed object cache. I've got both cache and cache-redis in my BuildConfig:
compile ':cache:1.1.8'
compile ":cache-redis:1.1.0"
And I have redis running locally. I know it is working because if I kill redis, the grails app can't connect to it. What I'm not finding is how to modify the configuration for redis when I'm not running locally.
There is nothing in the docs here. I also looked through the source here and found nothing of note.
How do I configure the redis settings (host, port, timeout, etc) ?
Take a closer look at the CacheRedisGrailsPlugin.groovy for the plugin itself. You will see that redis itself is configured in the doWithSpring phase of the plugin setup and reads from the redisCacheConfig configuration file for such things as host, port, etc.
Thus, adding something like the following to your Config.groovy would be perfectly acceptable:
grails {
cache {
redis {
hostName = 'localhost'
port = 6379
timeout = 2000
}
}
}

How to have grails run-app use https by default (without the -https command line option)

I would like to be able to run
grails run-app
and have it, by default, use ssl as if I ran
grails run-app -https
I would like this behavior for a couple reasons. The most important is that I want the Netbeans IDE grails integration to start up the https connection by default. But also, running on the command line, I won't have to remember or type the https since I always want that in my development environment.
The most important is that I want the Netbeans IDE grails integration to start up the https connection by default.
Surely this can be configured within the IDE?
also, running on the command line, I won't have to remember or type the https since I always want that in my development environment.
If you're using Linux or Mac, I would define an alias
alias run-app='grails run-app -https'
so you can get the behaviour you want simply by typing run-app on the command line. You could similarly achieve this on windows by creating a file run-app.bat whose contents are
grails run-app -https
and put this file in a directory that's on the PATH

How to solve cross domain errors when running two grails apps on localhost

I am trying to run-app my project with other wars/wars also running. So for example we have a war/ear that has legacy web services that I would like to access. My goal is to deploy those existing wars/ears with my run-app. I can deploy both separately, as grails run-app on port 8081 and jboss on port 8080. The problem was when I tried to do a ajax POST, DELETE, or PUT I get a cross-domain error because each app is on its own port. Is there a way to use run-app so that my grails project is at localhost:8080/grails and the webservices are at localhost:8080/webservices
Env: Grails 2.1.1
Server: Jboss (perfered) or vFabric tc Server (by dropping in existing war/war)
I don't believe that you can "run-app" both projects on the same port because you're running an embedded container. However, what you can do is check out the Grails CORS plugin which will allow other domains to access your grails app during run-app or otherwise.
http://grails.org/plugin/cors

grails 2.0 - deploying WAR to remote server (Tomcat), not connecting

I generally run Grails 2.0 from my IDE but have been seeking to deploy an app to a remote Virtual Private Server (linux) where I installed Tomcat and Mysql. I create a production WAR file and copy it to the Tomcat webapps dir, where I can see Tomcat restart and after some "fixing" I see the application come up, adding minimal bootstrap entries into the MySql database fine.
However, I cannot connect to the Grails app (remotely). If I specify www.mydomain.com, I am (finally) able to reach the main Tomcat server "welcome page". I also reach Tomcat if I add a "/" at the end, not my grails app.
As such I'm tried putting in full controller paths that I know work on my dev machine, and then accessing them on the remote machine (note grails.serverURL is configured as http://www.mydomain.com). Doing this, I get
HTTP Status 404 -- The requested resource (/BareBones/bare/create) is not available.
This error is from a BareBones application I created, as I was having some problems with VPS available memory. In any case, on my dev machine I can reach the this BareBones app URL
http://localhost:8080/BareBones/bare/create
as expected. When I deploy, I get the same HTTP Status 404 error (resource not available) when I do:
http://www.mydomain.com/bare/create
In Config.groovy, in this BareBones app I've got the minimal change:
environments {
development {
grails.logging.jul.usebridge = true
}
production {
grails.logging.jul.usebridge = false
grails.serverURL = "http://www.mydomain.com"
}
}
In my Tomcat server.xml file I changed over from the default 8080/8443 ports to 80/443, but using either either set results in the same problem.
I'm probably missing an easy step, just don't know what it is.
P.S. When I deploy the sample.war file that comes with Tomcat (isn't Grails, just a hello-world servlet), it works. I'm able to access that at
www.mydomain.com/sample
Since I FTP'd sample.war from my computer to the server, it would appear to indicate my FTPs are good, and the routing to the server is right, narrowing this down to Grails & Tomcat.
Usually in dev mode Grails mounts, as you pointed out, the context of appName, i.e. http://localhost:8080/BareBones/ here.
But the path it mounts on tomcat is not up to Grails itself, it is wholly dependent on the Tomcat configuration and primarily the name of the WAR file.
Even if you have BareBones as your appName and you deploy it as ROOT.war then it will mount the root "/" context. If you deploy it as BareBones.war then it should mount the same dir as in dev mode.
Because sites are usually mounted as ROOT.war "/", I can recommend setting
grails.app.context = "/"
in your Config.groovy file which will make it so that you will use the root context path also in dev, i.e. http://localhost:8080/
This makes it simpler since both dev and production will now have identical relative paths to everywhere and only the hostname:port will change.

how can i convert my grails application from http to https under Linux operating system?

how can i convert my grails application from http to https under Linux operating system
the configuration depends on what container you are running the application in your production environment. You should be deploying a war to your Production Server not doing a grails run-app -https
here is a stackoverflow question with a configuration for Tomcat.
I am certain you can google around and find proper configuration based on your application server
What do you mean by "convert"?
You can run the embedded tomcat with HTTPS enabled by adding -https to the run-app command:
grails run-app -https
If you want HTTPS enabled in production then you need to configure your app server to support it.

Resources