WAR 中的安全配置文件放在哪里?

发布于 2024-10-07 02:35:20 字数 652 浏览 0 评论 0原文

我正在尝试在我的 WAR 中使用 JAAS 进行身份验证。我了解我的配置文件 (另一个链接)应该放置在某个地方(如此处)。不幸的是,如果我们谈论战争,我不明白到底在哪里?以及如何命名该文件?

// JAAS has to find the file and retrieve "foo" from it
LoginContext ctx = new LoginContext("foo", this);

I'm trying to use JAAS for authentication in my WAR. I understand that my configuration file (another link) should be placed somewhere (as explained here). Unfortunately, I can't understand where exactly, if we're talking about WAR? And how to name the file?

// JAAS has to find the file and retrieve "foo" from it
LoginContext ctx = new LoginContext("foo", this);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨巷深深 2024-10-14 02:35:20

我遇到了同样的问题,我想看看是否无法根据当前的类路径(位于战争本身内部)动态设置此属性。

public class SecurityListener implements ServletContextListener {
    public SecurityListener() {
    }

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        if(System.getProperty("java.security.auth.login.config") == null) {
            String jaasConfigFile = null;
            URL jaasConfigURL = this.getClass().getClassLoader().getResource("login.conf");
            if(jaasConfigURL != null) {
                jaasConfigFile = jaasConfigURL.getFile();
            }
            System.setProperty("java.security.auth.login.config", jaasConfigFile);
        }
    }
}

显然,您需要将侦听器添加到 web.xml 中:

<listener>
    <listener-class>example.SecurityListener</listener-class>
</listener>

这样做的作用是在 Web 应用程序实例化时设置属性 java.security.auth.login.config(如果尚未定义) 。这意味着您可以将其放入源文件夹中并自动加载(如果没有在其他地方重新定义)。我已经对此进行了测试,它可以在 Tomcat 6 上运行。

因此,例如,如果您的 tomcat 安装在“C:\program files\tomcat6\”中,而您的 war 部署在“C:\program files\tomcat6\webapps\mywar”中,它找到的路径是“C:\program files\tomcat6\webapp\mywar\WEB-INF\classes”,它总是准确的。不确定这个解决方案是否也适用于其他 Web 应用程序,但我想是这样,因为 login.conf 将位于类路径根所在的位置。

希望有帮助!

I had the same problem and I wanted to see if I couldn't dynamically set this property based on the current classpath (which would be located inside the war itself).

public class SecurityListener implements ServletContextListener {
    public SecurityListener() {
    }

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        if(System.getProperty("java.security.auth.login.config") == null) {
            String jaasConfigFile = null;
            URL jaasConfigURL = this.getClass().getClassLoader().getResource("login.conf");
            if(jaasConfigURL != null) {
                jaasConfigFile = jaasConfigURL.getFile();
            }
            System.setProperty("java.security.auth.login.config", jaasConfigFile);
        }
    }
}

Obviously, you need to add the listener to your web.xml:

<listener>
    <listener-class>example.SecurityListener</listener-class>
</listener>

What this does is set the property java.security.auth.login.config upon instantiation of the web application if it has not yet been defined. This means you could throw it in your source folder and load it automatically if not otherwise redefined elsewhere. I have tested this and it works on Tomcat 6.

So, for example if your tomcat installation was in "C:\program files\tomcat6\" with your war deployed in "C:\program files\tomcat6\webapps\mywar", the path it would find would be "C:\program files\tomcat6\webapp\mywar\WEB-INF\classes" which is always accurate. Not sure if this solution also works with other web applications, but I would imagine so since login.conf is going to be where the classpath root is.

Hope that helps!

岛徒 2024-10-14 02:35:20

可以将client_jaas.conf封装在jar中,使用代码动态指定配置

System.setProperty("java.security.auth.login.config", XXX.class.getClassLoader().getResource("client_jaas.conf").toString());

You can encapsulate the client_jaas.conf in the jar, and use the code to specify the configuration dynamically

System.setProperty("java.security.auth.login.config", XXX.class.getClassLoader().getResource("client_jaas.conf").toString());
玩套路吗 2024-10-14 02:35:20

不幸的是,我能够让它工作的唯一方法是创建一个文件 jass.conf 并使用以下任一方法指定它:

  • 在 Tomcat java 参数中:

    -Djava.security.auth.login.config==c:\\path\\To\\file.conf
    
  • 或在 Java 代码中:

    System.setProperty("java.security.auth.login.config","c:\\path\\To\\file.conf");
    

我还想知道指定配置的更好方法。我想将该配置打包到我的 WAR 中。

Unfortunately the only way I was able to get it to work was to create a file jass.conf and specify it using either:

  • In the Tomcat java parameters:

    -Djava.security.auth.login.config==c:\\path\\To\\file.conf
    
  • or from within the Java code:

    System.setProperty("java.security.auth.login.config","c:\\path\\To\\file.conf");
    

I'd also like to know a better way to specify the configuration. I want to package that configuration in my WAR.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文