gpt4 book ai didi

grails - Grails 3-操作web.xml

转载 作者:行者123 更新时间:2023-12-02 14:07:12 25 4
gpt4 key购买 nike

由于现在将动态生成web.xml,因此我需要在其顶部添加一个简单的过滤器类,而无需编写或使用插件。
我需要什么代码,应该放在哪里?

还是我可以某种方式获取将为web.xml生成的grails的模板,然后我可以对其进行修改和覆盖?

我在文档中看到了

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

但首先:可以在application.yml中使用此功能吗?

第二:我仍然需要适当的web.xml模板进行更改...

谢谢!

最佳答案

根据cfrick的建议,我尝试了这一点(适用于grails 3)

创建一个过滤器,例如[grails-project] /src/main/java/hello/SimpleCORSFilter.java
(在“grails-project / src / main / groovy”目录中创建“java”目录,或创建一个SimpleCORSFilter.groovy)

您可以在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() {}

}

然后在[grails-project] /grails-app/conf/spring/resources.groovy中,添加过滤器,例如
import hello.SimpleCORSFilter

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

另外,在grails文档中,订购过滤器:
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
}

关于grails - Grails 3-操作web.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28430675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com