Jetty对某个web应用启用安全认证

1、Jetty的版本为:【jetty-7.6.7.v20120910】

2、使用了spring bean格式的Jetty配置文件【jetty.xml】,需要修改如下配置段
//////////begin/////////
<!-- modified by can_do on web app security login not for [/*]=begin= -->
<bean id="securityConstraint" class="org.eclipse.jetty.util.security.Constraint">
<property name="name" value="BASIC" />
<property name="roles" value="admin" />
<property name="authenticate" value="true" />
</bean>
<bean id="securityConstraintMapping" class="org.eclipse.jetty.security.ConstraintMapping">
<property name="constraint" ref="securityConstraint" />
<property name="pathSpec" value="/console/*" />
</bean>
<!-- modified by can_do on web app security login =end= -->
//////////end///////////
其中要改两处:
(1)、参数【authenticate】开启,即改为true;
(2)、参数【pathSpec】调整映射的context路径,默认是所有war应用【/*】,此处根据需要可调整为针对当前应用的,
       即需要改为【/console/*】

注意:配置多个角色的方式时,角色间以comma隔开,如下:

<property name="roles" value="admin,read-only" />

3、调整认证域中指定的配置信息,即用户名、密码、角色等,如下:
//////////begin/////////
<bean id="securityLoginService" class="org.eclipse.jetty.security.HashLoginService">
<property name="name" value="FMQRealm" />
<property name="config" value="${fmq.conf}/jetty-realm.properties" />
</bean>
//////////end///////////
此处指定了配置文件为【${fmq.conf}/jetty-realm.properties】
//////begin////
# username: password [,rolename ...]
admin: console, admin
//////end//////
注意:配置格式为[用户名]: [密码], [角色]

4、通过Jetty自动工具类,可以对密码进行安全保护,有三种格式:混淆、MD5、校验和
用法如下:
////////////begin///////
Usage - java org.eclipse.jetty.security.Password [<user>] <password>
C:\fabric\fmq\lib\web>java -cp .;./jetty-all-server-7.6.7.v20120910.jar org.eclipse.jetty.util.security.Password admin console
console
OBF:1sot1wg21wu61ym71wu81wfw1sox
MD5:bfafd813d7ea65ee4db1f09d7c8ffbf4
CRYPT:adL4jzDus01r6
///////////end/////////

5、将认证域配置文件中的明文转发为密文存储起来,如下:
/////////begin//////////
# username: password [,rolename ...]
admin: MD5:bfafd813d7ea65ee4db1f09d7c8ffbf4, admin
/////////end////////////

注意:此处的前缀很重要,【OBF:】、【MD5:】、【CRYPT:】其告诉Jetty认证服务验证时,采用哪种方式进行验证。

猜你喜欢

转载自can-do.iteye.com/blog/2246946