在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.csdahua.cn/qtweb/news36/417086.html">在GlassFish中为Web应用程序设置用户访问权限</a> <br> 本文网址:<a href="http://www.csdahua.cn/qtweb/news36/417086.html">http://www.csdahua.cn/qtweb/news36/417086.html</a> </p> <p> 网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等 </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.csdahua.cn/" 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/yidongjianshe/">移动网站建设知识</a></p> <ul> <li> <a class="text_overflow" href="/qtweb/news6/505256.html">80%的攻击中检测到三个恶意软件加载程序</a> </li><li> <a class="text_overflow" href="/qtweb/news41/365841.html">Css入门:vector-effect(向量效果)</a> </li><li> <a class="text_overflow" href="/qtweb/news29/252829.html">为什么要选择武汉网站企业,武汉网站企业的优势与实力</a> </li><li> <a class="text_overflow" href="/qtweb/news17/450317.html">境外服务器停机:原因、影响以及如何预防</a> </li><li> <a class="text_overflow" href="/qtweb/news6/547306.html">诱人却不一定适合 数据中心虚拟化的不利因素及对策</a> </li><li> <a class="text_overflow" href="/qtweb/news8/519008.html">如何查看服务器日志中的内存溢出问题(查看服务器日志内存溢出)</a> </li><li> <a class="text_overflow" href="/qtweb/news10/326460.html">网络Linux网络扫描:为你的网络安全提供保障(linux扫描)</a> </li><li> <a class="text_overflow" href="/qtweb/news35/410935.html">怎么把文件中的数据写入二维数组中c语言</a> </li><li> <a class="text_overflow" href="/qtweb/news22/526522.html">国内虚拟主机备案限制:可备案几个域名?(国内虚拟主机能备案几个域名)</a> </li><li> <a class="text_overflow" href="/qtweb/news23/135573.html">海外服务器租用有什么好处?(封海外的服务器好吗)</a> </li><li> <a class="text_overflow" href="/qtweb/news18/340618.html">信息安全发展之企业信息安全精简版</a> </li><li> <a class="text_overflow" href="/qtweb/news14/273964.html">网络可以用,为什么电视看不了了,电视首页可以加载出来,但是打开就?(网站无法打开看状态都正常)</a> </li><li> <a class="text_overflow" href="/qtweb/news14/191364.html">主机商是什么?主机服务商是什么</a> </li><li> <a class="text_overflow" href="/qtweb/news34/141434.html">开启新世界:Linux学院助你掌握操作系统(linux学院)</a> </li><li> <a class="text_overflow" href="/qtweb/news29/273229.html">Linux下.gz文件的解压方法(linux解压.gz文件)</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/zdfhw/" 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/hwxxy/" 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/qzgqb/" 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/jbgc/" 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/hntjbz/" 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/zbljbc/" 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/chalousj/" 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/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/hntjbc/" 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/bxgds/" 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/pvc/" target="_blank">PVC花箱</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/sclsb/" 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/hntjbj/" 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/ggzz/" 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/fengyangtai/" target="_blank">封阳台</a> </li> </ul> </div> </div> </div> <div class="carousel-inner linkbg" style="background: #fff"> <div class="container"> <a href="http://seo.cdkjz.cn/tuiguang/" target="_blank">成都网站推广</a>    <a href="http://www.kswcd.com/solution/" target="_blank">网站解决方案</a>    <a href="http://chengdu.cdcxhl.com/dingzhi/" target="_blank">定制网站建设</a>    <a href="https://www.cdcxhl.cn/ " target="_blank">腾讯云免备案主机</a>    <a href="http://www.tyjike.cn/" target="_blank">成都富士康招工</a>    <a href="http://www.cdkjz.cn/wangzhan/advmedia/" target="_blank">广告媒介投放</a>    <a href="https://www.cdxwcx.com/wangzhan/dingzhi.html" target="_blank">定制网站建设</a>    <a href="http://m.cdcxhl.cn/applets/" target="_blank">微信小程序</a>    <a href="http://www.hdybtv.com/" target="_blank">鼎尚理发店</a>    <a href="http://www.teliergzn.com/" target="_blank">特丽尔硅藻泥</a>    <a href="http://www.wjzwz.com/" target="_blank">温江网站制作</a>    <a href="http://www.csruizhi.cn/" target="_blank">csruizhi.cn</a>    <a href="http://m.cdcxhl.com/" target="_blank">成都网站建设</a>    <a href="https://www.cdcxhl.com/xiangyingshi.html" target="_blank">成都响应式网站建设公司</a>    <a href="http://www.4006tel.net/yingxiao/" target="_blank">seo优化</a>    <a href="http://www.cxhljz.cn/solution/" target="_blank">教育建设方案</a>    <a href="https://www.cdxwcx.com/jifang/xibuxinxi.html" target="_blank">西部信息机房</a>    <a href="http://m.cdcxhl.cn/applets/ " target="_blank">小程序开发</a>    <a href="https://www.cdcxhl.com/service/400.html" target="_blank">400电话申请</a>    <a href="http://www.cdhuace.com/zhangui.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> 节约开发成本 <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'"> 立即开始2800定制网站建设</button> <button type="button" class="btn btn-default btn-xs" onClick="window.location.href='tencent://message/?uin=631063699&Site=&Menu=yes'"> 2800定制网站建设</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.csdahua.cn/" target="_blank">网站优化</a> <a href="http://www.csdahua.cn/" target="_blank">网络推广</a> <a href="http://www.csdahua.cn/" 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>