您的位置:首页 > 其它

web.xml 中的 security-role 的運作剖析

2008-03-11 15:29 330 查看
如果在 tomcat 之上執行程式
你撰寫的 security-role 到底有沒有用呢 ?

當呼叫 isUserInRole 其實是去呼叫 RealmBase 中的 hasRole 透過 GenericPrincipal 去檢查
GenericPrincipal gp = (GenericPrincipal) principal;
boolean result = gp.hasRole(role);
return result;


而 Principals 是在你 authentication 的時候就給了,
這是 取出 tomcat-users.xml 的 roles 去處理放在你的 Principal 中
void addUser(String username, String password, String roles) {

 // Accumulate the list of roles for this user
 ArrayList list = new ArrayList();
 roles += ",";
 while (true) {
 int comma = roles.indexOf(',');
 if (comma < 0)
 break;
 String role = roles.substring(0, comma).trim();
 list.add(role);
 roles = roles.substring(comma + 1);
 }

 // Construct and cache the Principal for this user
 GenericPrincipal principal =
 new GenericPrincipal(this, username, password, list);
 principals.put(username, principal);

 }


不過還有一個情況, 有 servlet 採用 security-role-ref link security-role 的時候
如果是採用

FOO
manager

在 isUserInRole("FOO") 會使用 Wrapper 去找相關的 roles,
if (wrapper != null) {
 String realRole = wrapper.findSecurityReference(role);
 if ((realRole != null) &&
 realm.hasRole(userPrincipal, realRole))
 return (true);
 }


在 tomcat starup 的同時 ContextConfig 會建立
與 的關係
Container wrappers[] = context.findChildren();
for (int i = 0; i < wrappers.length; i++) {
Wrapper wrapper = (Wrapper) wrappers[i];
String runAs = wrapper.getRunAs();
if ((runAs != null) && !context.findSecurityRole(runAs)) {
log(sm.getString("contextConfig.role.runas", runAs));
context.addSecurityRole(runAs);
}
String names[] = wrapper.findSecurityReferences();
for (int j = 0; j < names.length; j++) {
String link = wrapper.findSecurityReference(names[j]);
if ((link != null) && !context.findSecurityRole(link)) {
log(sm.getString("contextConfig.role.link", link));
context.addSecurityRole(link);
}
}
}

因此 , 當你使用 servlet 時, 裡面可能會寫
isUserInRole("FOO"), 但是對應到的應該是整個系統的 manager,
如果在 security-role 之中沒有定義相關的 manager role,

則會新增一個 manager role 在 context 之中, 相當於建立了
你就會在 servlet 中撰寫相關的"FOO" role ref link 到 manager role,
這樣, 就可以使用 FOO 成為 manager 的角色了

因此 security-constraint 裡面的 role 與 security-role 是沒有關係的 ^^~

所以呢, 如果你在 tomcat 上面執行, security-role 有沒有設定的結果應該都是一樣的.

至於其他商業用的 commercial application server, 通常會在專用部署檔中參考 web.xml 中的 security-role 去做 role-mapping !

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: