jaas基本authenticationjetty-runner
我正在尝试使用jetty runner运行webapp的jaas基本身份validation,但即使使用最基本的webapp,我也没有运气。
我正在按照这里的说明
首先我使用一个全新的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我执行jetty runner与战争
java -jar jetty-runner.jar testwebapp.war
使用Chrome浏览器导航到localhost:8080我看到“Hello World!” 信息
我停止jetty-runner并编辑web.xml以添加loginconfiguration详细信息,使其看起来像
<!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包
然后我创build一个名为myjass.xml的dockerconfiguration文件,如下所示
<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>
我为安全性创build了一个名为mysecurity.conf的configuration文件,如下所示
mysecurity { org.eclipse.jetty.jaas.spi.PropertyFileLoginModule required debug="true" file="security.props"; };
最后,我创build了简单包含的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
没有例外。 当我导航到loclhost:8080时,我完全期待被要求提供我的凭证,但是它只是像以前一样显示HelloWorld消息。 我是否误解了关于dockerjassauthentication的东西?
更新2017-04-27。
Walkerwatch的评论之后,我再次尝试使用标准分发。 我从下面的Dockerfile创build了一个名为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是按照前面所述build造的,但是
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
我可以导航到本地主机:8080 / doesthiswork和页面加载而不要求任何凭据。 如果我开始打开debugging,我看到在日志中
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 oejwWebAppContext@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服务似乎开始好,但我从来没有要求任何凭据。 什么是我错过/误解?
我自己find了答案。 我只是愚蠢的,并没有包括任何安全约束我的networking应用程序。 所以没有被标记为要求authentication的资源。 将以下内容添加到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>