2009年3月6日星期五

WLS_013:WebLogic Server基本管理之九:配置Web应用安全

本文最后一次修改时间:2011-12-07。
运行环境:WebLogic Server 10.0.2。

WebLogic Server验证用户的流程是这样的:
客户端浏览器发送请求后,由Web Container接收请求,并将账户信息交给Realm验证。
验证不通过,发送错误响应;验证通过后,处理请求并发送响应到客户端浏览器。
其中错误响应分两种:
(1)如果用户没有权限,但是账户/口令输入正确,那么页面会显示错误:“Error 403--Forbidden”。
(2)如果用户有权限,但是口令三次输入不正确,那么页面会显示错误:“Error 401--Unauthorized”。


timeoff应用介绍:

普通员工可以generate absence reports 和request time off,只有经理才可以close office。

1. 在Console中建立用户和组
[domain_name]->Security Realms
(1)john/welcome1 Administrators
(2)joe/welcome1 employees, managers
(3)ted/welcome1 employees, managers
(4)mary/welcome1 employees
(5)albert/welcome1 employees

2. 在web.xml中配置安全信息
只有角色director允许访问URL Pattern:/officeclosing/*和/managers/*。

<?xml version='1.0' encoding='UTF-8'?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<servlet>
<servlet-name>AbsenceReport</servlet-name>
<servlet-class>com.servlets.absenceReport</servlet-class>
</servlet>
<servlet>
<servlet-name>OfficeClosing</servlet-name>
<servlet-class>com.servlets.officeClosing</servlet-class>
</servlet>
<servlet>
<servlet-name>TimeOffRequest</servlet-name>
<servlet-class>com.servlets.timeOffRequest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AbsenceReport</servlet-name>
<url-pattern>/absencereport</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>TimeOffRequest</servlet-name>
<url-pattern>/timeoff</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>OfficeClosing</servlet-name>
<url-pattern>/officeclosing</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>welcome.html</welcome-file>
</welcome-file-list>
<security-constraint>
<display-name>Constraint-0</display-name>
<web-resource-collection>
<web-resource-name>Constraint-0</web-resource-name>
<url-pattern>/officeclosing/*</url-pattern>
<url-pattern>/managers/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>director</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>default</realm-name>
</login-config>
<security-role>
<role-name>director</role-name>
</security-role>
</web-app>

3. 在weblogic.xml中映射安全角色
组managers隶属于角色director。即隶属于组managers的用户都具有角色director。

<security-role-assignment>
<role-name>director</role-name>
<principal-name>managers</principal-name>
</security-role-assignment>

4. 发布应用timeoff.war

4.1 选择DD Only: Use only roles and policies that are defined in the deployment descriptors。
此选项表明角色和安全策略都来自于DD,即web.xml(负责安全策略)和weblogic.xml(负责角色映射)。
(1)点击timeoff应用-->Security-->URL Pattern-->Roles,会看到定义了一个角色:director。
这个定义是从web.xml中读取的。

(2)点击director,会看到定义了一个映射关系:director和managers。
这个定义是从weblogic.xml中读取的。

(3)点击timeoff应用-->Security-->URL Pattern-->Policies,会看到定义了两个策略:/officeclosing/*和/managers/*。
这个定义是从web.xml中读取的。

(4)点击/managers/*策略,可以看到其中定义了角色director,也就是说允许拥有director角色的用户访问/managers/*。

(5)访问 http://localhost:7001/timeoff/,点击Close An Office,弹出认证窗口,只有managers组的成员才可以进入。
手工增加一个用户如maping/welcome1到managers组,点击Close An Office,同样可以认证通过。
从这里可以看出,可以在发布应用后增加用户到某个组,从而具有这个组所拥有的角色。

4.2 选择Custom Roles: Use roles that are defined in the Administration Console; use policies that are defined in the deployment descriptor。
此选项表明角色来自于Console中的定义,忽略weblogic.xml;而安全策略来自于DD,即web.xml。
(1)由于忽略了weblogic.xml,点击timeoff应用-->Security-->URL Pattern-->Roles,会看到角色定义是空的:

(2)为此,我们需要手工增加一个新的角色映射,规则如下:
URL Pattern:/managers/*
Name:director(因为web.xml中已经定义了这个角色,所以必须保持一致)
Provider Name:XACMLRoleMapper。

(3)点击director,增加Add Conditions,选择Choose Group,输入你想要映射的组,这里不一定和weblogic.xml中一致,比如你也可以加入employees组。

(4)访问 http://localhost:7001/timeoff/,点击Close An Office,弹出认证窗口,只有managers的成员才可以进入。
在Console中删除director与managers组的映射,增加director与employees组的映射,点击Close An Office,弹出认证窗口,只有employees的成员才可以进入。
从这里可以看出,选择Custom Roles,可以在发布应用后修改角色映射关系,这是这个选项的好处。

(5)对于URL Pattern的访问权限,除了可以按照角色映射访问外,还可以要求只能在指定日期访问。
增加一个新的条件:Access occurs on the specified day of the month。

注意,我国是东八区,因此GMT offset:GMT+8:00。

与其它条件是And的关系,即必须全部满足才可以访问。


4.3 Custom Roles and Policies: Use only roles and policies that are defined in the Administration Console.
此选项表明角色与安全策略全部来自于Console中的定义,忽略web.mxl和weblogic.xml中有关的定义。

Project下载:timeoff.war

没有评论: