2

As now web.xml will be generated dynamically, I'd need to add a simple filter class at the top of of it without writing or using a plugin. What code would I need and where should I put it?

Or could I somehow get a template of what grails would generate for a web.xml and I could modify and override it?

I saw in the documentation

grails.war.resources = { stagingDir, args ->
    copy(file: "grails-app/conf/custom-web.xml",
         tofile: "${stagingDir}/WEB-INF/web.xml")
}

but first: would this function in application.yml?

and second: I'd still need an appropriate web.xml template to change...

Thanks!

rawi
  • 401
  • 3
  • 13

3 Answers3

4

From cfrick's suggestion, I tried this, (applies to grails 3)

Create a filter e.g. [grails-project]/src/main/java/hello/SimpleCORSFilter.java (Create the 'java' directory, or create a SimpleCORSFilter.groovy in the [grails-project/src/main/groovy directory)

You can use the example in one of the spring guides.

package hello;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;

@Component
public class SimpleCORSFilter implements Filter {

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
//        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        // I used this instead
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        chain.doFilter(req, res);
}

    public void init(FilterConfig filterConfig) {}

    public void destroy() {}

}

Then in [grails-project]/grails-app/conf/spring/resources.groovy, add the filter, e.g.

import hello.SimpleCORSFilter

// Place your Spring DSL code here
beans = {
    myFilter(SimpleCORSFilter)
}

Also, in the grails docs, to order your filter:

import hello.SimpleCORSFilter
import org.springframework.boot.context.embedded.FilterRegistrationBean
import org.springframework.core.Ordered

myFilter(FilterRegistrationBean) {
    filter = bean(SimpleCORSFilter)
    urlPatterns = ['/*']
    order = Ordered.HIGHEST_PRECEDENCE
}
  • Thank you. I had a Filter, but I wasn't able to wire it. It was easier for me to edit web.xml. Zyro pointed me to the same FilterRegistration here: http://stackoverflow.com/questions/28475431/grails-3-0-0-m1-create-plugin-could-not-find-method-bintray – rawi Mar 26 '15 at 10:33
0

i know it's not the anwers to the actual question, but adding filters "springboot" style is just providing a @Bean for it. E.g. you can just put it in grails-app/conf/spring/resources.groovy. More details can be found at: How to add a filter class in Spring Boot?

Grails 3 embraces Springboot and with that came the good bye to XML based config (not saying it is forbidden, but its discouraged)

Community
  • 1
  • 1
cfrick
  • 29,743
  • 4
  • 46
  • 59
-3

You can use install-templates command, which will copy templates to src/templates directory, there you will find web.xml template inside war directory, you can customize it

See http://www.grails.org/doc/latest/ref/Command%20Line/install-templates.html

Sudhir N
  • 3,890
  • 1
  • 20
  • 31