在GlassFish中为Web应用程序设置用户访问权限

在实际应用中GlassFish,我们可能会把用户、权限、角色之类的写在数据库中,用程序对用户访问权限加以控制,也可能会用到Spring AOP的Interceptor来拦截非法访问,但是可能会存在一个问题,如果将超级管理员的用户名和密码写在数据库,万一不小心把超级管理员的用户或角色删掉,怎么办?因此让应用服务器来托管管理员(或其他特殊角色),不但可以避免这个问题,而且也简化了应用程序的用户权限控制(你不必因为害怕普通用户非法访问管理员的访问域而费尽心思使用代码或AOP来控制),而且GlassFish应用服务器在安全性方面很有保证,大大提高了应用程序的安全性。

实验环境:
1、Netbeans 6.1中文版,JDK1.6以上
2、GlassFish V2或Sun Application Server

实验步骤:
1、创建Web应用程序,命名为WebApplicationSecurity,在创建过程中保持默认选项即可,不需要选择其他框架。

2、在WEB页 目录下创建两个文件夹(新建-其他-其他-文件夹),分别命名为secureAdmin和secureUser,一个是管理员的访问域,一个是普通用户的访问域。接下来将严格控制管理员和普通用户的访问权限,普通用户只能访问secureUser目录下的页面,而管理员则两个都可以访问。

3、在secureAdmin目录下创建一个名为pageA的HTML文件,同样的,在secureUser下创建一个名为pageU的HTML文件内容如下:

 

      
      
      
      
  1.  
  2.     
  3.       </font></strong>管理员安全域<strong><font> title></font></strong> </li> <li>   <strong><font> head></font></strong> </li> <li>   <strong><font><body></font></strong> </li> <li>      <strong><font><h1></font></strong>管理员安全域<strong><font> h1></font></strong> </li> <li>   <strong><font> body></font></strong> </li> <li><strong><font> html></font></strong> </li> </ol></pre> </td></tr></tbody></table><p> </p><p>4、在WEB页目录下创建主页index.jsp,将标签中的内容覆盖为:</p></p><p>5、在GlassFish中添加用户权限。</p><p>     首先,展开服务-服务器,选择GlassFish V2,右键-启动。(如果没看到有GlassFish,请安装后点击“添加”)。</p><p>     然后,打开浏览器,输入网址<strong><font>http://localhost:4848</font></strong>访问GlassFish的管理员平台,默认用户是admin,密码是adminadmin。</p><p>     接下来,展开 配置-安全性-领域,选择file。在主窗口打开的页面中,点击“管理用户”按钮,进入用户管理页面,点击新建,创建一个管理员用户,用户名为admin,密码也是admin。同样地,创建一个用户user,密码是user。</p><p>至此,GlassFish下的用户权限已经配置完毕,接下来,我们需要在应用程序的配置文件中添加一些配置来使用这两个用户。</p><p>6、打开WEB-INF目录下的web.xml,点击“安全”选项卡,展开“登陆配置”,选择“基本”,在“域名称”中填入 file。展开“安全角色”,点击“添加”,“角色名称”中填入Admin,同样的方法,添加一个角色名称为User的角色。在“安全约束”选项下,点击“添加安全约束”,“显示名称”中填入AdminConstraint,在“Web资源集合”选项下点击“添加”,“资源名称”为Admin,URL模式为“/secureAdmin/*”,表示Admin这个资源集合映射到secureAdmin下的任何页面,如下图所示:</p><p>      选中“启用验证约束”,编辑“角色名称”,将Admin添加到右边的框中。同样,创建一个显示名为UserConstraint的安全约束,资源集合中的资源名称为User,URL模式为/secureUser/*。注意角色名称应该Admin和User都选择(管理员和普通用户均可进入)。</p><p> </p><p>7、最后,在sun-web.xml中添加WEB应用定义的安全资源与GlassFish上的用户的映射。打开WEB-INF下的sun-web.xml,点击“安全”选项卡,点击“添加安全角色映射”,在“安全角色名”中填入Admin,添加主要用户,主要用户名称为admin。同样,创建一个名为User的安全角色,并添加主要用户名为user的主要用户,如下图所示:</p><p>8、部署,运行项目,点击管理员页面,如果用admin来登陆,将跳转到pageA页面,如果用user来登陆,则遭到拦截。如果你不希望使用“基本认证”来接受用户输入,你也可以自己写一个表单来接受用户输入:</p><table cellspacing="0" cellpadding="2" width="400" border="1"> <tbody> <tr><td><pre><p> </p><pre> <ol> <li><strong><font><form.</font></strong> <font>action</font>=<font>"j_security_check"</font> <font>method</font>=<font>"POST"</font><strong><font>></font></strong> </li> <li>            Username:<strong><font><input</font></strong> <font>type</font>=<font>"text"</font> <font>name</font>=<font>"j_username"</font><strong><font>><br></font></strong> </li> <li>            Password:<strong><font><input</font></strong> <font>type</font>=<font>"password"</font> <font>name</font>=<font>"j_password"</font><strong><font>></font></strong> </li> <li>            <strong><font><input</font></strong> <font>type</font>=<font>"submit"</font> <font>value</font>=<font>"Login"</font><strong><font>></font></strong> </li> <li><strong><font> form></font></strong> </li> </ol></pre> </td></tr></tbody></table><p> </p><p>在web.xml的安全选项中的登陆配置里选中“窗体”,然后选择登陆页面和登陆错误的页面即可。</p> <p> 当前题目:<a href="http://www.kswsj.com/qtweb/news36/417086.html">在GlassFish中为Web应用程序设置用户访问权限</a> <br> 分享网址:<a href="http://www.kswsj.com/qtweb/news36/417086.html">http://www.kswsj.com/qtweb/news36/417086.html</a> </p> <p> 网站建设、网络推广公司-成都快上网,一家网站设计、网站制作公司;服务项目有等 </p> <p class="adpic"> <a href="https://www.cdcxhl.com/service/ad.html" target="_blank" class="ad">广告</a> <a href="" target="_blank" class="adimg"><img src=""></a> </p> <p class="copy"> 声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: <a href="http://www.kswsj.com/" target="_blank">成都快上网</a> </p> </div> <div class="newsmorelb"> <p>成都快上网科技猜您喜欢</p> <ul> <li> <a href="/qtweb/news35/417085.html">apache虚拟主机配置的三种方式(小结)</a> </li><li> <a href="/qtweb/news34/417084.html">怎么在阿里云购买自定义服务器</a> </li><li> <a href="/qtweb/news33/417083.html">Linux 读写硬盘扇区技术指南(linux读写扇区)</a> </li><li> <a href="/qtweb/news32/417082.html">一直在windows错误恢复?(一直windows错误恢复画面)</a> </li><li> <a href="/qtweb/news31/417081.html">创新互联impala教程:impala 偏移条款</a> </li><li> <a href="/qtweb/news30/417080.html">权限Linux网卡设备文件权限管理小结(linux网卡设备文件)</a> </li><li> <a href="/qtweb/news29/417079.html">哪个云服务器比较便宜?什么服务器比较便宜</a> </li><li> <a href="/qtweb/news28/417078.html">web服务器的架设?如何用服务器做网站</a> </li><li> <a href="/qtweb/news27/417077.html">ic卡贴怎么使用?(卡贴怎么用)</a> </li> </ul> </div> </div> <div class="col-lg-3 noneb"> <div class="bkright" style="margin-top: 0"> <p><a href="https://www.cdcxhl.com/news/gaiban/">网站改版知识</a></p> <ul> <li> <a class="text_overflow" href="/qtweb/news46/523196.html">创新互联Python教程:python如何获取重定向输入</a> </li><li> <a class="text_overflow" href="/qtweb/news29/369479.html">Linux驱动开发必备基础教程(linux驱动基础教程)</a> </li><li> <a class="text_overflow" href="/qtweb/news49/271599.html">抖音6馍是什么意思-抖音6馍啥意思</a> </li><li> <a class="text_overflow" href="/qtweb/news40/496940.html">10个好用的Web日志安全分析工具</a> </li><li> <a class="text_overflow" href="/qtweb/news40/408940.html">CentOS7.2下Hadoop2.7.2集群搭建</a> </li><li> <a class="text_overflow" href="/qtweb/news47/124097.html">手机里的云服务怎么用?如何使用云服务</a> </li><li> <a class="text_overflow" href="/qtweb/news37/403137.html">十分钟搞懂K8S的亲和与反亲和调度</a> </li><li> <a class="text_overflow" href="/qtweb/news34/174634.html">linux层级结构:探寻深层次架构(linuxlayer)</a> </li><li> <a class="text_overflow" href="/qtweb/news25/555525.html">Linux轻松创建日期文件夹,让文件整理更有序(linux创建日期文件夹)</a> </li><li> <a class="text_overflow" href="/qtweb/news0/304200.html">WatchGuard:怎样用“大数据”降低网络安全成本</a> </li><li> <a class="text_overflow" href="/qtweb/news40/502840.html">日本价格比较低vps有什么优势吗</a> </li><li> <a class="text_overflow" href="/qtweb/news43/507993.html">如何在Aurora中实现自动扩展存储功能</a> </li><li> <a class="text_overflow" href="/qtweb/news25/539525.html">cpuLinux查看线程CPU占用情况(linux查看线程占用)</a> </li><li> <a class="text_overflow" href="/qtweb/news45/181245.html">令人恼怒的十个安全悖论</a> </li><li> <a class="text_overflow" href="/qtweb/news47/258047.html">MSSQL中使用循环实现表行遍历(mssql 循环表行)</a> </li> </ul> </div> <div class="bkright tag"> <p><a href="https://www.cdcxhl.com/hangye/" target="_blank">分类信息网站</a></p> <ul> <li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/yupeng/" target="_blank">雨棚定制</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/zhanting/" target="_blank">展览展示</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/muwu/" target="_blank">木屋</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/sdgz/" target="_blank">水电改造</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/weilanhulan/" target="_blank">围栏护栏</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/zufadianji/" target="_blank">发电机租赁</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/hntjbg/" target="_blank">混凝土搅拌罐</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/blgds/" target="_blank">玻璃钢雕塑</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/tadiaozulin/" target="_blank">塔吊租赁</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/putaojia/" target="_blank">葡萄架</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/gsdb/" target="_blank">工商代办</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/ddcl/" target="_blank">电动窗帘</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/hldzj/" target="_blank">护栏打桩机</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/ldjbc/" target="_blank">履带搅拌车</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/sdjbc/" target="_blank">隧道混凝土搅拌车</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/zdfhw/" target="_blank">主动防护网</a> </li> </ul> </div> </div> </div> <div class="carousel-inner linkbg" style="background: #fff"> <div class="container"> <a href="http://www.kswsj.cn/" target="_blank">全网营销网站推广</a>    <a href="http://www.cdxwcx.cn/" target="_blank">做网站</a>    <a href="http://www.cdxwcx.cn/tuoguan/zigong.html" target="_blank">自贡托管服务器</a>    <a href="http://www.cxjianzhan.com/baojia/" target="_blank">网站建设策划方案</a>    <a href="http://www.scjbc.cn/" target="_blank">小谭建站</a>    <a href="http://www.zzjierui.cn/" target="_blank">广安网站建设</a>    <a href="http://www.pzhzwz.com/" target="_blank">攀枝花网站设计</a>    <a href="http://www.czyouth.cn/" target="_blank">水泥搅拌车</a>    <a href="http://www.lsbanjia.cn/" target="_blank">崇州搬家公司</a>    <a href="https://www.cdcxhl.cn/ " target="_blank">香港空间</a>    <a href="http://www.cxhljz.cn/app/" target="_blank">ios开发</a>    <a href="http://www.cdfuwuqi.com/jigui/" target="_blank">成都机柜租用</a>    <a href="http://m.cdcxhl.cn/shop/" target="_blank">成都商城网站建设公司</a>    <a href="http://www.scghjhjc.com/" target="_blank">广汉锦华建材</a>    <a href="http://www.njxishu.com/" target="_blank">鳄鱼养殖</a>    <a href="http://www.cdxtjz.com/" target="_blank">网站制作</a>    <a href="http://chengdu.cdxwcx.cn/" target="_blank">成都网站制作</a>    <a href="https://www.cdcxhl.com/app.html" target="_blank">app软件开发</a>    <a href="http://www.cdxwcx.cn/tuoguan/xiyun.html" target="_blank">成都移动托管</a>    <a href="https://www.cdcxhl.com/xiyun.html" target="_blank">移动服务器托管</a>     </div> </div> <footer> <div class="carousel-inner footjz"> <div class="container"> <i class="icon iconfont zbw"></i> 品质网站建设 <i class="icon iconfont"></i> 多平台展现 <i class="icon iconfont"></i> 600元建站 <i class="icon iconfont"></i> 高效快速 <i class="icon iconfont"></i> 专业用心服务 <button type="button" class="btn btn-default btn-lg" onClick="window.location.href='tencent://message/?uin=631063699&Site=&Menu=yes'"> 立即开始600元网站建设</button> <button type="button" class="btn btn-default btn-xs" onClick="window.location.href='tencent://message/?uin=631063699&Site=&Menu=yes'"> 600元企业网站建设</button> </div> </div> <div class="carousel-inner bqsy"> <div class="container"> <div class="lxfs"> <h4 class="yutelnone">028-86922220 13518219792</h4> <h4 class="yutelblock"><a href="tel:02886922220">028-86922220</a> <a href="tel:13518219792">13518219792</a></h4> <a class="btn btn-default" href="tencent://message/?uin=532337155&Site=&Menu=yes" role="button">网站建设<span>QQ</span>:532337155</a> <a class="btn btn-default" href="tencent://message/?uin=631063699&Site=&Menu=yes" role="button">营销推广<span>QQ</span>:631063699</a> <a class="btn btn1 btn-default" href="mqqwpa://im/chat?chat_type=wpa&uin=532337155&version=1&src_type=web&web_src=oicqzone.com" role="button">网站制作<span>QQ</span>:532337155</a> <a class="btn btn1 btn-default" href="mqqwpa://im/chat?chat_type=wpa&uin=631063699&version=1&src_type=web&web_src=oicqzone.com" role="button">营销推广<span>QQ</span>:631063699</a> <a class="btn btn-default nonea" href="tencent://message/?uin=1683211881&Site=&Menu=yes" role="button">售后QQ:1683211881</a> <div class="dz">成都快上网专注: <a href="http://www.kswsj.com/" target="_blank">成都网站制作</a> <a href="http://www.kswsj.com/" target="_blank">网站设计</a> <a href="http://www.kswsj.com/" target="_blank">成都网站建设</a> <address>地址:成都太升南路288号锦天国际A幢10楼</address> </div> </div> <div class="bzdh dz"><img src="https://www.cdcxhl.com/imges/bottom_logo.png" alt="创新互联"> <p><a href="https://www.cdcxhl.com/menu.html" target="_blank">成都创新互联科技有限公司</a><br> Tel:028-86922220(7x24h)</p></div> </div> </div> </footer> </body> </html> <script> $.getJSON ("../../qtwebpic.txt", function (data) { var jsonContent = { "featured":data } var random = jsonContent.featured[Math.floor(Math.random() * jsonContent.featured.length)]; $(".adpic .adimg").attr("href",random.link) $(".adpic img").attr("src",random.pic); }) </script>