XForum 里用 Filter 编程实现安全访问控制

J2EE/EJB 论坛 / XForum 里用 Filter 编程实现安全访问控制
cinc 2003.03.11, 15个回复, 1745次浏览
在 J2ee 里,实现安全有两种:
用声明实现安全,就是在 web.xml 里实现安全限制。
用编程实现安全,自己写代码

在 XForum 里,我们没有让 container 来管理安全检查,我们使用的是自己编程实现的:

用户登陆后,把用户的信息存放在 session 里,通过检查 session 中的用户信息,就可以
知道用户是否登陆成功了。


XForum 以前的版本里,这个检查过程是在分散在每个被保护的页面里做的,比如:
在 ViewMyThreadAction 里:
        final HttpSession session = request.getSession();
        // If the user is null which means the user has not loged in,
        // forward it to logon screen
        if( userId == null || userId.length() == 0 ){
             final String url = "/viewMyThreads.go";
             request.setAttribute( ForumConstants.DEST_URL, url );
             return (mapping.findForward("logon"));
             // Else display the user's threads
        }else{
        。。。
如果用户没有登陆,把当前页面存放在 session 中,转到 logon 页面让用户登陆。


在 XForum 新版本里,我们用了 Filter 技术做集中的 acl 控制: AclFilter
首先,把需要包含的页面存放在一个 acl-config.xml 里:

<protected-resource> <uri>post.go</uri> <desc>Post Thread Form</desc> </protected-resource> <protected-resource> <uri>viewMyThreads.go</uri> <desc>View My Thread</desc> </protected-resource> </acl-config> 在 web.xml ,对每个 web resource 都应用 这个 Filter: <filter> <filter-name>AclFilter</filter-name> <filter-class>org.redsoft.forum.filters.AclFilter</filter-class> </filter> <filter-mapping> <filter-name>AclFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 在 AclFilter 的 doFilter 方法里检查:如果请求的 uri 是 protected resource,而且 session 中没有用户信息,就转向到 logon 页面 这样,如果以后要添加保护页面,只需要在 acl-config.xml 里加上一个 protected-resource 就可以了,而且也可以保护静态页面(.html 的页面)动态页面里的代码由于少了检查用户登陆的代码,也清晰了很多。 :)




目前还实现不了,现在只有两个角色:
  guest 用户:不能访问 protected resource
  登陆  用户:可以访问所有的资源

不过在现有的 acl-config.xml 的基础上加以修改应该可以实现,可以定义几个 role
比如 guest, user, manager,然后修改 acl-config.xml 如下:

    <protected-resource>
        <uri>post.go</uri>
        <desc>Post Thread Form</desc>
        <role>manager,user</role>
    </protected-resource>

实现复杂的 acl 控制


使用filter是个好办法,tomcat曾经使用安全拦截部件Security Interuptor
(org.apache.tomcat.request.SecurityCheck)来实现web.xml中申明的资源授权。

我们完全可以参考这个类来实现自己的安全过滤器(使用JNDI或JDBC资源)。
而读取的是web.xml中的资源配置信息。这样就非常灵活了。

web.xml的配置并不需要在java中写上角色名的问题,改动角色名不需要修改代码。这
是servlet规范中的一段原文:<SRV12.3>
For example, to map the security role reference "FOO" to the security role
with role-name "manager" the syntax would be:
 <security-role-ref>
   <role-name>FOO</role-name>
   <role-link>manager</manager>
 </security-role-ref>
In this case if the servlet called by a user belonging to the "manager'
securiyt role made the API call isUserInRole("FOO") would be true.

那么,在程序中只要写明FOO,使用role-link可以将其他角色也视为同义词。


用 application 还是 container 管理安全确实是 J2EE 程序设计时需要好好考虑的问题
XForum 的 Filter 其实是参照 Sun Pet Store 的 SignonFilter 简化改写的

在 Pet Store 的文档里有很大一段篇幅是来讲 SignonFilter 的,有兴趣的可以看看:

All users in the pet store are logged in as the same system user, so they all
have the same system permissions, such as whether or not they can execute a
certain method. Application permissions, such as who can create an order 
(registered users only), are modelled and controlled at the application level.

Casual users such as the shoppers in the pet store do not have to authenticate
their real-life identity in any way. In such cases, it's usually preferable 
to manage users in the application layer instead of at the system layer. Users
with special permissions, such as system or application administrators, are 
usually best represented by system users, using the J2EE login mechanisms 
mentioned above.

ftp://210.52.88.133/pub/doc/java/j2ee/petstore/sample-app1.3.1.pdf
回应这个帖子



转载于:https://www.cnblogs.com/sunsonbaby/archive/2004/10/23/55902.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值