通常对于一般的web应用程序,都是自己写验证,输入用户名和密码,然后到数据库去验证,然后返回。但是对于安全性要求较高的应用,自己写的安全验证则会出现许多没考虑到的地方,这时应该使用安全框架。
我使用的是struts框架,服务器是weblogic8.14,配置了基于FORM的验证方式,具体配置如下:
1、目录结构:根目录文件:index.jsp,login.html,error.jsp
admin目录:存放系统模块业务页面的路径
pages目录:存放公用页面的路径
2、login.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[url]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd[/url]">
<html xmlns="[url]http://www.w3.org/1999/xhtml[/url]">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
body{
 margin:0;
 font-size:12px;
 height:100%;
}
#content {
 position:absolute;
 left:58%;
 top:42%;
 width:190px;
 height:104px;
 z-index:1;
}
.int{
 width:120px;
 height:13px;
 border:#c0c0c0 1px solid;
}
.btn{padding-top:2px;}
</style>
</head>
<body>
<div id="content">
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <form  method="post" action="j_security_check">
 <tr>
   <td height="25" colspan="2" align="right" valign="bottom">&nbsp;</td>
   </tr>
 <tr>
      <td width="60" height="35" align="right" valign="bottom">用户id:</td>
      <td width="120" valign="bottom"><input type="text" name="j_username"  id="j_username" class="int"/></td>
    </tr>
    <tr>
      <td width="60" height="25" align="right" valign="bottom">密码:</td>
      <td valign="bottom"><input type="password" name="j_password" id="j_password" class="int"/></td>
    </tr>
    <tr>
      <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
      <td height="30" colspan="2" align="right">
        <input type="submit"  id="btnok" value="登录" class="btn"/>
       <input id="reset" type="reset" value="取消" class="btn"/></td>
    </tr>
    </form>
  </table>
</div>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td><table width="601" height="364" border="0" align="center" cellpadding="0" cellspacing="0" background="p_w_picpaths/login.jpg">
      <tr>
        <td>&nbsp;</td>
      </tr>
    </table></td>
  </tr>
</table>
</body>
</html>
需要注意的是这里的用户名,密码和提交页面必须固定为j_username,j_password,j_security_check见文件的反显部分。
3、error.jsp
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
error
</title>
</head>
<body bgcolor="#ffffff">
<h1>
Login error!
</h1>
</body>
</html>
4、在web.xml里配置要保护的资源
<security-constraint>
    <web-resource-collection>
      <web-resource-name>admin</web-resource-name>
      <url-pattern>/admin/*</url-pattern>
      <http-method>PUT</http-method>
      <http-method>GET</http-method>
    </web-resource-collection>
    <web-resource-collection>
      <web-resource-name>pages</web-resource-name>
      <url-pattern>/pages/*</url-pattern>
      <http-method>PUT</http-method>
      <http-method>GET</http-method>
    </web-resource-collection>
    <web-resource-collection>
      <web-resource-name>indexservlet</web-resource-name>
      <url-pattern>/indexservlet</url-pattern>
      <http-method>PUT</http-method>
      <http-method>GET</http-method>
    </web-resource-collection>
    <web-resource-collection>
      <web-resource-name>index</web-resource-name>
      <url-pattern>/index.jsp</url-pattern>
      <http-method>PUT</http-method>
      <http-method>GET</http-method>
    </web-resource-collection>
    <auth-constraint>
      <role-name>mgr</role-name>
    </auth-constraint>
    <user-data-constraint>
      <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
  </security-constraint>
这里我配置了对admin目录、index.jsp、indexservlet的保护,只允许mgr角色可以访问,注意不要对error.jsp也保护了,否则会导致登陆失败时达不到错误页面。
5、配置角色:
mgr是这样配置的:
在web.xml里加入:
<security-role>
    <role-name>mgr</role-name>
  </security-role>
在weblogic.xml里加入角色映射:
<security-role-assignment>
    <role-name>mgr</role-name>
    <principal-name>admin</principal-name>
      </security-role-assignment>
该段表示将weblogic服务器里配置的admin用户映射为这里的mgr角色。
6、配置验证方式:
在web.xml里加入
<login-config>
    <auth-method>FORM</auth-method>
    <form-login-config>
      <form-login-page>/login.html</form-login-page>
      <form-error-page>/error.jsp</form-error-page>
    </form-login-config>
  </login-config>
现在,打包发布你的程序,输入任何保护的资源,在未验证的情况下就会跳转到登陆页面了,从而完成对资源的保护。

转载于:https://blog.51cto.com/caopeng/46794

关于web应用程序的安全验证相关推荐

  1. Web应用程序的身份验证机制

    身份验证是大多数网站的基本要求. 但是,有许多机制可以实现身份验证,并且它们之间不是很互换. 根据业务需求,开发人员需要为其应用程序选择最合适的身份验证方法. 除非人们很好地了解机制之间的差异,否则这 ...

  2. 好程序员web前端教程分享JavaScript验证API

    好程序员web前端教程分享JavaScript验证API,小编每天会分享一下干货给大家.那么今天说道的就是web前端培训课程中的章节. JavaScript验证API 约束验证DOM方法 Proper ...

  3. 如何使用json开发web_如何通过使用JSON Web令牌简化应用程序的身份验证

    如何使用json开发web by Sudheesh Shetty 由Sudheesh Shetty 如何通过使用JSON Web令牌简化应用程序的身份验证 (How to simplify your ...

  4. 如何使用pFuzz以多种方法验证Web应用程序防火墙的安全性

    关于pFuzz pFuzz是一款功能强大的Web应用程序防火墙安全检测/绕过工具,可以帮助广大研究人员同时通过多种方式绕过目标Web应用程序防火墙,以测试WAF的安全性. pFuzz本质上是一款高级红 ...

  5. framer x使用教程_如何使用Framer Motion将交互式动画和页面过渡添加到Next.js Web应用程序

    framer x使用教程 The web is vast and it's full of static websites and apps. But just because those apps ...

  6. web应用程序和web网站_Web应用程序和移动应用程序的基本启动清单

    web应用程序和web网站 by Ben Cheng 通过本诚 Web应用程序和移动应用程序的基本启动清单 (The Essential Launch Checklist for Web Apps a ...

  7. ASP .NET Core Web MVC系列教程:使用ASP .NET Core创建MVC Web应用程序

    本系列教程翻译自微软官方教程,官方教程地址:Get started with ASP.NET Core MVC | Microsoft Docs 本系列教程介绍了构建MVC Web应用程序的基础知识. ...

  8. ASP.NET Core Web Razor Pages系列教程:使用ASP.NET Core创建Razor Pages Web应用程序

    ASP .Net Core Razor Pages MySQL Tutorial 本系列教程翻译自微软官方教程,官方教程地址:Tutorial: Create a Razor Pages web ap ...

  9. java web里实现 mvc_MVC模式在Java Web应用程序中的实现

    一.MVC简介: MVC架构是一个复杂的架构,其实现也显得非常复杂..Views可以看作一棵树,可以用Composite Pattern来实现.Views和Models之间的关系可以Observer  ...

最新文章

  1. 如何构建金字塔结构性思维
  2. 8. An Introduction to MCMC for Machine Learning (3)
  3. Python---modules(模块)
  4. asp.net url传值,弹窗
  5. asp空间和php空间_免费ASP空间与免费PHP空间
  6. PyTorch载入图片后ToTensor解读(含PIL和OpenCV读取图片对比)
  7. java线程抢占式执行,Java并发基础(一)-线程基础
  8. transferwise怎么提现_收款工具transfer wise介绍(多币种、可收CJ联盟)
  9. hudson--插件管理
  10. 睡眠阶段分期——SVM和ELM分别与粒子群算法结合(function)
  11. 学用计算机 关机,电脑关机命令是什么?快速关机必备(你学会了吗)
  12. 数据结构、算法、程序的关系
  13. Mac的聚焦搜索Spotlight搜索不准确问题
  14. sketch常用快捷键键盘对应
  15. PMP证书好考吗?难度如何?
  16. 电源与地之间接电容的原因分析
  17. STM32F103 中断优先级理解
  18. Instruments使用技巧
  19. 共享创业新契机--共享陪护床
  20. 区块链+数据隐私安全打破数据时代“环形监狱”的利器

热门文章

  1. iir matlab 系数,手把手教你用matlab生成STM32官方IIR滤波器的系数
  2. Ubuntu查看文件夹下文件的个数
  3. Spark基础学习笔记08:Scala简介与安装
  4. Python案例:格式化输出斐波拉契数列
  5. 安卓案例:在同一Activity里切换Fragment
  6. Scala案例:评定成绩等级
  7. 在Eclipse里搭建Go开发的环境
  8. 鼠标侧键能改为ctrl吗_200元档次又一高竞争力外设 雷柏V30鼠标评测
  9. bzoj1876 [SDOI2009]SuperGCD 辗转相减+高精
  10. 【英语学习】【English L06】U05 Appointments L2 I'd like to make an airport shuttle service reservation