XForum 里用 Filter 编程实现安全访问控制
生活随笔
收集整理的這篇文章主要介紹了
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 | |||
| 回應這個帖子 | |||
總結
以上是生活随笔為你收集整理的XForum 里用 Filter 编程实现安全访问控制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我喜欢的一首歌--《幸福的瞬间》
- 下一篇: chedandekaoyan