简体   繁体   English

Tomcat jaas.conf / log4j.properties位置

[英]Tomcat jaas.conf/log4j.properties location

I'm developing a GWT web application and while deploying to Tomcat, I face a problem. 我正在开发一个GWT Web应用程序,在部署到Tomcat时,我遇到了一个问题。

The problem is quite simple but I don't know how to fix it without telling Tomcat where to look for these files. 问题很简单,但我不知道如何修复它而不告诉Tomcat在哪里查找这些文件。

When I deploy my app right out on to Tomcat, it looks for the files in the /bin directory so if I place the log4j.properties and my jaas.conf in there it works like a charm. 当我将我的应用程序直接部署到Tomcat时,它会在/ bin目录中查找文件,因此如果我将log4j.properties和我的jaas.conf放在那里,它就像一个魅力。

The thing is I would like to be able to keep those files within my webapp. 问题是我希望能够将这些文件保存在我的webapp中。

How can I do it? 我该怎么做? Is there anything I can add to the web.xml? 我可以添加到web.xml吗?

I tried to put both files into the /WEB-INF/classes directory but it didn't work out. 我试图将两个文件放入/WEB-INF/classes目录,但它没有用完。

When I run my projects in Eclipse my jaas.config has to be in the /war folder while my log4j.properties stays in the /src folder. 当我在Eclipse中运行我的项目时,我的jaas.config必须位于/ war文件夹中,而我的log4j.properties保留在/ src文件夹中。

Edit : I read this and and tried it even if I don't use log4j for Tomcat internal logging but it didn't work either. 编辑:我读了这个并尝试了它,即使我不使用log4j进行Tomcat内部日志记录,但它也没有用。

I use Tomcat 7.0 我使用Tomcat 7.0

Regarding jaas.config: Implement a ServletContextListener and within the contextInitialized method do the following (this is if jaas.config is in the root of your war, otherwise, just change the path): 关于jaas.config:实现ServletContextListener并在contextInitialized方法中执行以下操作(如果jaas.config位于war的根目录中,否则,只需更改路径):

String jaasConfigPath = event.getServletContext().getRealPath("jaas.config");
System.setProperty("java.security.auth.login.config", jaasConfigPath);

This answer will fit my particular problem but anyone can surely adapt it to make it works in his project. 这个答案将适合我的特殊问题,但任何人都可以适应它,使其适用于他的项目。

My project had two servlet depending on external files. 我的项目有两个servlet,具体取决于外部文件。 To centralize these external files into my webapp folder, I used a third sevlet to set the location of the files. 为了将这些外部文件集中到我的webapp文件夹中,我使用了第三个sevlet来设置文件的位置。 In my GWT web.xml, I add the StartUpServlet and I set it to be loaded first by my Servlet Container (Tomcat). 在我的GWT web.xml中,我添加了StartUpServlet ,并将其设置为首先由我的Servlet容器(Tomcat)加载。 Here is my web.xml : 这是我的web.xml:

        <?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
    <servlet>
        <servlet-name>StartUpServlet</servlet-name>
        <servlet-class>com.banctecmtl.ca.vlp.view.webview.server.StartUpServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet>
        <servlet-name>VlpControllerService</servlet-name>
        <servlet-class>com.banctecmtl.ca.vlp.view.webview.server.VlpControllerServiceImpl</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>UserAccessService</servlet-name>
        <servlet-class>com.banctecmtl.ca.vlp.view.webview.server.UserAccessServiceImpl</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>

    <servlet>
        <servlet-name>EventService</servlet-name>
        <servlet-class>de.novanic.eventservice.service.EventServiceImpl</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>


    <servlet-mapping>
        <servlet-name>EventService</servlet-name>
        <url-pattern>/VirtualLabPortal/gwteventservice</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>VlpControllerService</servlet-name>
        <url-pattern>/VirtualLabPortal/VlpController</url-pattern>

    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>UserAccessService</servlet-name>
        <url-pattern>/VirtualLabPortal/UserAccess</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>StartUpServlet</servlet-name>
        <url-pattern>/VirtualLabPortal/StartUpServlet</url-pattern>
    </servlet-mapping>


    <!-- Default page to serve -->
    <welcome-file-list>
        <welcome-file>VirtualLabPortal.html</welcome-file>
    </welcome-file-list>
</web-app>

In this StartUpServlet , I set the files location like so : (There might be a better way to do it, but it worked) : 在这个StartUpServlet ,我像这样设置文件位置:(可能有更好的方法,但它工作):

public class StartUpServlet extends RemoteServiceServlet {
    /**
     * This Servlet is used to set configuration file locations.
     */
    private static final long serialVersionUID = 6459940076859400546L;
    private final String CONFIG_FOLDER = "config";
    private final String LOG_FOLDER = "logs";

    public void init() {
        // Check the current of to have the good file separator for file
        // browsing
        String os = System.getProperty("os.name").toLowerCase();
        // windows
        String fileSeparator;
        if (os.indexOf("win") >= 0) {
            fileSeparator = "\\";
        } else {
            fileSeparator = "/";
        }

        String jaasConfigPath = super.getServletContext().getRealPath(
                CONFIG_FOLDER + fileSeparator + "JaasConfig.conf");
        String jaasConfigName = "JaasConfig";
        String configFile = super.getServletContext().getRealPath(
                CONFIG_FOLDER + fileSeparator + "config.properties");

        String log4j = getServletContext().getRealPath(
                CONFIG_FOLDER + fileSeparator + "log4j.properties");
        String logFile = getServletContext().getRealPath(
                LOG_FOLDER + fileSeparator + "vlplog.log");

        // Order is important here as the log4j properties file use the system
        // property : "logFile"
        System.setProperty("logFile", logFile);
        PropertyConfigurator.configure(log4j);
        System.setProperty("jaasConfigName", jaasConfigName);
        System.setProperty("jaasConfigPath", jaasConfigPath);
        System.setProperty("configFile", configFile);
    }
}

Basically, I get the ServletContext real path so it gives me, on Tomcat, the following path : ${CATALINA_HOME}/webapps/<myapps> 基本上,我得到了ServletContext的真实路径,所以它在Tomcat上给了我以下路径: ${CATALINA_HOME}/webapps/<myapps>

Then I use this to set the file locations : config/config.properties, config/log4j.properties and my config/JaasConfig. 然后我用它来设置文件位置:config / config.properties,config / log4j.properties和我的config / JaasConfig。

Once they are set I can use them in my other servlets like this System.getProperty(KEY); 一旦设置好,我就可以在我的其他servlet中使用它们,例如System.getProperty(KEY);

Thanks to @Aviram for helping me to set the location of the JaasConfig. 感谢@Aviram帮助我设置JaasConfig的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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