docker - jaas基本认证 docker 运行者

标签 docker jaas jetty-9

我正在尝试让jaas基本身份验证与通过jettyRunner运行的webapp一起工作,但是即使使用最基本的webapp,我也没有运气。

我正在按照here的说明进行操作

首先,我使用生成一个全新的webapp项目

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp

生成的web.xml如下
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
</web-app>

我运行mvn软件包,然后使用jetty-runner 9.2.5.v20141112在 war 中执行jetty-runner
 java -jar jetty-runner.jar testwebapp.war

使用chrome浏览器导航到localhost:8080,我看到“Hello World!”信息

我停止jetty-runner并编辑web.xml以添加登录配置详细信息,以使其看起来像
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

<login-config>
  <auth-method>BASIC</auth-method>
  <realm-name>Test JAAS Realm</realm-name>
</login-config>

</web-app>

我运行MVN包

然后,我创建一个名为 myjass.xml 的 jetty 配置文件,如下所示
<Configure id="Server" class="org.eclipse.jetty.server.Server">

  <Call name="addBean">
    <Arg>
      <New class="org.eclipse.jetty.jaas.JAASLoginService">
        <Set name="name">Test JAAS Realm</Set>
        <Set name="LoginModuleName">mysecurity</Set>
      </New>
    </Arg>
  </Call>

</Configure>

我为安全性创建了一个配置文件 mysecurity.conf ,如下所示
mysecurity {
        org.eclipse.jetty.jaas.spi.PropertyFileLoginModule required
        debug="true"
        file="security.props";
      };

最后,我创建了 security.props 文件,其中仅包含
test: Password_123

所有这些文件都与jetty-runner.jar位于同一目录中,然后运行
java -Djava.security.auth.login.config=mysecurity.conf -jar jetty-runner.jar --config myjass.xml testwebapp.war

没有异常(exception)。当我导航到loclhost:8080时,我完全希望输入我的凭据,但它只是像以前一样显示HelloWorld消息。关于 jetty 的Jass认证,我是否误解了一些基本知识?

更新2017-04-27。

根据Walkerwatch的评论,我仅使用标准发行版再次尝试。我从以下Dockerfile创建了一个名为jetty-test的docker镜像,该镜像使用jetty 9.4.4.v20170414构建
FROM jetty

RUN java -jar $JETTY_HOME/start.jar --add-to-start=jaas

RUN echo "jetty.jaas.login.conf=$JETTY_BASE/etc/login.conf" >> $JETTY_BASE/start.d/jaas.ini

COPY mysecurity.conf $JETTY_BASE/etc/login.conf 
COPY security.props $JETTY_BASE/etc/security.props
COPY context.xml $JETTY_BASE/webapps/context.xml
COPY jetty-test.war $JETTY_BASE/jetty-test.war

文件如下

mysecurity.conf
mysecurity {
        org.eclipse.jetty.jaas.spi.PropertyFileLoginModule required
        debug="true"
        file="/var/lib/jetty/etc/security.props";
      };

security.props
test: Password_123

context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">

  <Set name="contextPath">/doesthiswork</Set>
  <Set name="war">/var/lib/jetty/jetty-test.war</Set>

  <Set name="securityHandler">
    <New class="org.eclipse.jetty.security.ConstraintSecurityHandler">
     <Set name="loginService">
       <New class="org.eclipse.jetty.jaas.JAASLoginService">
         <Set name="name">Test JAAS Realm</Set>
         <Set name="loginModuleName">mysecurity</Set>
       </New>
     </Set>
    </New>
  </Set>

</Configure>

jety-test.war的构建如前所述,但是

web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

  <display-name>Archetype Created Web Application</display-name>

  <login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>Test JAAS Realm</realm-name>
  </login-config>

</web-app>

当我启动docker容器时
docker run  -d -p8080:8080 --name=jetty-test -t jetty-test

我可以导航到localhost:8080 / doesthiswork并且页面加载而无需任何凭据。如果我从打开调试开始,我会在日志中看到
2017-04-27 15:49:24.565:DBUG:oejx.XmlParser:main: parse: file:/usr/local/jetty/etc/jetty-jaas.xml

2017-04-27 15:49:24.566:DBUG:oejx.XmlParser:main: parsing: sid=file:/usr/local/jetty/etc/jetty-jaas.xml,pid=null


2017-04-27 15:49:24.669:DBUG:oejx.XmlConfiguration:main: XML new org.eclipse.jetty.jaas.JAASLoginService

2017-04-27 15:49:24.670:DBUG:oejx.XmlConfiguration:main: using normal mapping

2017-04-27 15:49:24.670:DBUG:oejx.XmlConfiguration:main: XML org.eclipse.jetty.jaas.JAASLoginService@578486a3.setName(Test JAAS Realm)

2017-04-27 15:49:24.671:DBUG:oejx.XmlConfiguration:main: XML org.eclipse.jetty.jaas.JAASLoginService@578486a3.setLoginModuleName(mysecurity)

2017-04-27 15:49:24.671:DBUG:oejx.XmlConfiguration:main: XML org.eclipse.jetty.security.ConstraintSecurityHandler@5bc79255.setLoginService(org.eclipse.jetty.jaas.JAASLoginService@578486a3)

2017-04-27 15:49:24.673:DBUG:oejuc.ContainerLifeCycle:main: org.eclipse.jetty.security.ConstraintSecurityHandler@5bc79255 added {org.eclipse.jetty.jaas.JAASLoginService@578486a3,AUTO}

2017-04-27 15:49:24.673:DBUG:oejx.XmlConfiguration:main: XML o.e.j.w.WebAppContext@4e9ba398{/doesthiswork,null,UNAVAILABLE}{/var/lib/jetty/jetty-test.war}.setSecurityHandle


2017-04-27 15:49:25.091:DBUG:oejuc.AbstractLifeCycle:main: starting org.eclipse.jetty.jaas.JAASLoginService@578486a3

2017-04-27 15:49:25.091:DBUG:oejuc.AbstractLifeCycle:main: STARTED @1201ms org.eclipse.jetty.jaas.JAASLoginService@578486a3

因此,似乎jaas服务已启动,但从未要求我提供任何凭据。我想念/误解了什么?

最佳答案

我自己找到了答案。我当时只是个愚蠢的人,没有在我的Web应用程序中包含任何安全限制。因此,没有标记为需要身份验证的资源。将以下内容添加到web.xml中可解决此问题

<security-constraint>
  <web-resource-collection>
    <web-resource-name>Everything</web-resource-name>
    <url-pattern>/*</url-pattern>
  </web-resource-collection>
  <auth-constraint>
    <role-name>**</role-name>
  </auth-constraint>
</security-constraint>

关于docker - jaas基本认证 docker 运行者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43543578/

相关文章:

docker - 为Docker父镜像build设置http_proxy

tomcat - 基于 Java EE 的 Web 应用程序服务器中的 JAAS 安全性是否依赖?

java - 使用 JMX 和 Jaas 进行 jconsole 身份验证

Jetty 和最大内容大小

java - war 文件的 jetty 9.x 基本目录的最小文件集是什么?

java - 如何摆脱 http ://localhost:8080/test/in Jetty? 中的多余/

docker - 如何在一个容器中运行多个JMX文件?

ruby-on-rails - 使用 Rails 进行本地编码时需要 docker 吗?

看板和 Redis 的 Docker-Compose 错误

authentication - Windows 域的 JAAS 身份验证