怎样买网站建设,织梦如何做淘宝客网站,个人怎样注册一家公司,wordpress用户的区别J2EE/EJB 论坛 / XForum 里用 Filter 编程实现安全访问控制cinc 2003.03.11#xff0c; 15个回复#xff0c; 1745次浏览在 J2ee 里#xff0c;实现安全有两种#xff1a; 用声明实现安全#xff0c;就是在 web.xml 里实现安全限制。 用编程实现安全#xff0c;自己写代码…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 users threads }else{ 。。。 如果用户没有登陆把当前页面存放在 session 中转到 logon 页面让用户登陆。 在 XForum 新版本里我们用了 Filter 技术做集中的 acl 控制 AclFilter 首先把需要包含的页面存放在一个 acl-config.xml 里 protected-resource uripost.go/uri descPost Thread Form/desc /protected-resource protected-resource uriviewMyThreads.go/uri descView My Thread/desc /protected-resource /acl-config 在 web.xml 对每个 web resource 都应用 这个 Filter filter filter-nameAclFilter/filter-name filter-classorg.redsoft.forum.filters.AclFilter/filter-class /filter filter-mapping filter-nameAclFilter/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 uripost.go/uri descPost Thread Form/desc rolemanager,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-nameFOO/role-name role-linkmanager/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, its 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 回应这个帖子