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. LoadRunner Winsock 10053错误的真正原因
  2. SMILES | 简化分子线性输入规范
  3. SWT 和 SWING
  4. vuex分模块后,如何获取state的值
  5. 一款针对Outlook的红队安全研究工具
  6. Swift之深入解析类和结构体的本质
  7. JVM-Ubuntu18.04.1下编译OpenJDK8
  8. 120天的烧脑只为孩子设计一套教具~
  9. maven mysql的jdbctemplate_JDBC、JDBCTemplate、MyBatis、Hiberante 比较与分析
  10. Linux函数--inet_pton / inet_ntop
  11. 个人和结对项目 - 英语单词词频统计
  12. C#利用委托实现窗体间的值传递
  13. linux mysql安装及密码相关问题(2)和navicat整合连接
  14. Linux命令格式及帮助命令详解
  15. 行到水穷处,坐看云起时-我的2007!
  16. 用计算机录制声音让音质更好,电脑有什么好用的录音软件吗
  17. 六种复杂控制系统简述:串级、分程、比值、前馈、选择性和三冲量控制
  18. html 按钮 click事件无效,html中onClick HTML事件不能被触发
  19. java、web前端开发日常记录
  20. node.js基于vue的化妆品销售管理系统的设计与实现毕业设计源码151314

热门文章

  1. php折半查找面试题,php 面试题(一)
  2. 【Verilog】数据流建模传输问题:赋值传输有方向
  3. 宏定义对调试代码的作用
  4. 一条正确的Java职业生涯规划,帮你突破瓶颈
  5. 一文搞懂JVM架构:入职3个月的Java程序员面临转正
  6. Spring主要用到两种设计模式
  7. 【转载】儒林外史人物——荀玫
  8. 正则表达式(读书过程所记未整理)
  9. ubuntu-14.04.2-desktop使用方法
  10. Visual Studio 快捷键汇总