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

news/2025/2/26 7:22:35
如果在 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 !

 



http://www.niftyadmin.cn/n/3653795.html

相关文章

MySQL 行列转换变化各种方法实现总结(行变列报表统计 列变行数据记录统计等)

前言&#xff1a;mysql行列变化&#xff0c;最难的就是将多个列变成多行&#xff0c;使用的比较多的是统计学中行变列&#xff0c;列变行&#xff0c;没有找到现成的函数或者语句&#xff0c;所以自己写了存储过程&#xff0c;使用动态sql来实现&#xff0c;应用业务场景&#…

JavaTWO 2004 in taiwan 紀事

舊金山有 JavaONE, 台灣有 JavaTWO雖然沒有 JavaONE 來自各地先驅名流聚集, 但是台灣的 Java 界同好也都有到場互動第一天的早上, 是由台灣昇陽主持, 相關的一些未來發展與介紹, 包括 Motorala, Mac, Nokia 等等的國際大廠一起為 Java 大力推廣與前幾屆不同的地方, 這次加入了 …

Servlet Security 的缺憾 -- request.getUserSubject()

此篇詳細討論在 http://www.javaworld.com.tw/jute/post/view?bid5&id63394&tpg1&ppg1&sty1&age1#63394smallufo 對於我在 javatwo 演講的 j2ee security 很不滿意因為我只有簡略地帶過 JAAS.所以提出了相關的問題與討論在 JavaWorld Taiwan 之中很高興和…

zabbix3 0 监控mysql服务免用户名密码登录的问题故障处理详细过程

1&#xff0c;My.cnf中用户名密码无效在azure云上面&#xff0c;使用Zabbix监控mysql中&#xff0c;发现在/usr/local/mysql/my.cnf里面设置的默认用户名密码无效&#xff0c;出不来数据&#xff0c;而且在zabbix服务器上&#xff0c;使用zabbix_get也报错failed&#xff0c;如…

Git学习 -- 个人常用命令add commit以及push

Git命令行配置1 安装Github2 安装msysgit3 要配置用户名和油箱git config --global user.name <用户名>我的命令就是&#xff1a;git config --global user.name mchdbaghgit config --global user.email <油箱>我的命令就是&#xff1a;git config --global mchdb…

4th Asia Opensource Symposium in Taipei Howard

活動的首頁是 http://www.nii.org.tw/cnt/ecnews/activity/aoss/ 9/1 晚上 ..我到場的時候 已經是晚上 6:30 了因為整天都在客戶那兒處理一些有的沒的資料進入了福華之後, 在中庭的地方,就是 4th AOSS 的晚宴的所在進場的時候 和 中研院的專案經理 寒喧了一下帶我到 資訊研究所…

commons-beanutils 1.7 新增的 LazyDynaBean

commons-beanutils 除了增加一些 Exception 處理外, 還包含了一個新的東西, 那就是 LazyDynaBean.. 所謂 Lazy, 就是懶人寫 DynaBean 的方法, 簡化了 DynaBean 要先寫 DynaProperty 的設定值, 我們可以完全不用去宣告他原本的預設值, 只要直接把值放進去就可以了. 在 commons-b…

tomcat9 0 最新版初体验

1&#xff0c;rpm方式安装jdk8软件准备&#xff1a;先下载jdk8 下载地址&#xff1a;http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html。下载linux64位版本的rpm安装包&#xff0c;wget http://download.oracle.com/otn-pub/java/jdk/8u6…