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

回应这个帖子

XForum 里用 Filter 编程实现安全访问控制相关推荐

  1. MATLAB里面的filter和filtfilt的C语言源代码

    MATLAB里面的filter和filtfilt的C语言源代码 嗯,算法非常简单,就是网上搜不到C代码实现.filter是个很万能的数字滤波器函数,只要有滤波器的差分方程系数,IIR呀FIR呀都能通过 ...

  2. 用通俗易懂的大白话搞明白Java里的函数式编程和Lambda表达式

    今天,用通俗易懂的大白话来彻底搞明白Java里的函数式编程和Lambda表达式 为什么引入函数式编程,lambda表达式? 大家都知道,JDK1.8引入了函数式编程,lambda表达式. 那有没有想过 ...

  3. python加上子类的特性_Python里的元编程:控制产生的实例对象的特性以及实例

    很多人不理解"元编程"是个什么东西,关于它也没有一个十分准确的定义.这篇文章要说的是Python里的元编程,实际上也不一定就真的符合"元编程"的定义.只不过我无 ...

  4. angular里的filter过滤数据传参

    在ng-repeat中使用过滤器filter ,则在filter闭包内第一个参数,就是ng-repeat中的原数组,其余参数可以在-repeat表达式中,使用filterName :param1 :p ...

  5. linux5关闭apic服务,阐述Linux内核里面的APIC编程

    微软操作系统的火热,你是在应用Linux操作系统么?如果你是Linux操作系统的老用户. 这里为你讲解的问题会对Linux内核里面的APIC编程有所帮助.Linux内核的名字也是"Linux ...

  6. c语言算法a i-j x y,MATLAB里面的filter和filtfilt的C语言源代码

    嗯,算法非常简单,就是网上搜不到C代码实现.filter是个很万能的数字滤波器函数,只要有滤波器的差分方程系数,IIR呀FIR呀都能通过它实现.在MATLAB里面,filter最常用的格式是这两个: ...

  7. Swift 4 和 Objective-C 在同一个工程里的混搭编程的方法

    快速起步 你可以在 xcode 里同时使用 Swift 和 Objective-C(以下简称OC)来写代码,混搭编程的好处很多,比如允许大量代码的复用,在性能和开发效率之间找到平衡等. 在 Swift ...

  8. 如何理解柯里化|函数式编程

    这里我们来谈论下函数式编程中另一个重要的概念,柯里化 首先,我们先通过下面的方式将上节代码中不纯的函数变成纯函数.就是将mini拿到函数内部去. function checkAge (age) {le ...

  9. SAP S/4HANA Analytics Path Framework 里过滤器(filter)的使用方法介绍

    In APF Configuration Modeler, there is a filter with type Smart Filter Bar automatically generated. ...

最新文章

  1. 【新手必看】17个常见的Python运行时错误
  2. Java NIO 选择器(Selector)的内部实现(poll epoll)
  3. 每周分享之cookie详解
  4. 蒙特卡洛方法学习(二)
  5. 20 SD配置-主数据-定义客户账户组
  6. havc是什么意思_avc是什么意?eduis里h.264/a – 手机爱问
  7. Java-图片加水印-与图片缩放
  8. PAT (Basic Level) Practice1020 月饼
  9. jQuery实现点击行(tr)选中某列中CheckBox
  10. 在dos下或web下Fatal Error: undefined function mysql_connect()
  11. gg修改器ios版下载
  12. Wunderlist – 免费同步 Todo List
  13. 系统分析设计案例分析
  14. STC8I2CGY-302(BH1750光照度强度模块)
  15. 点击关闭Element-UI中dialog右上角的关闭图标按钮
  16. 贷后评分模型的三种细分应用
  17. colorkey唇釉是否安全_colorkey唇釉安全吗-colorkey唇釉真假辨别
  18. SparkShell创建表
  19. Gateway+Nacos+Sleuth+Zipkin网关链路追踪(测试及源码),Gateway+FeignClient+Nacos通过网关远程调用微服务(一)
  20. python链表合并去重_合并两个链表并去重(php实现)

热门文章

  1. makefile编译问题记录
  2. Gym - 101981I-MagicPotion-最大流
  3. Linux C++ 实现线程池
  4. 从零开始学C++之STL(八):函数对象、 函数对象与容器、函数对象与算法
  5. 【Java】异常处理的目的
  6. php函数网,php函数
  7. 关于gitgithub的操作
  8. centos 7新机使用前操作
  9. Use Selenium webdriver in Javascript
  10. 解决解决鼠标右键被锁定