web安全 - - 授权的配置以及实现基于表单的认证

首先在 tomcat-users.xml中配置

<tomat-users>
    <role rolename="Admin"/>
    <role rolename="Member"/>
    <role rolename="Guest"/>
    <user username="Stupid Wolf" password="admin" roles="Admin, Member, Guest"/>
 </tomcat-users>

接着在 web.xml中配置

<security-role>
    <role-name>Admin</role-name>
</security-role>
<!--授权时,容器将把开发商特定的role信息映射到这里的找到的<role-name>中 -->

定义资源/方法约束

<security-constraint>

  <web-resource-collection>
    <web-resource-name>Resources</web-resource-name>
    <url-pattern>/url of files</url-pattern>
    <http-method>POST</http-method>
    <http-method>GET</http-method>
  </web-resource-collection>
  
  <auth-constraint>
    <role-name>Admin</role-name>
  </auth-constraint>
  
</security-constraint>

在web.xml添加

<login-config>
  <auth-method>FORM</auth-method>
  <form-login-config>
  	<form-login-page>/login.html</form-login-page>
  	<form-error-page>/error.html</form-error-page>
  </form-login-config>

其中login.html error.html的代码分别为

<!DOCTYPE html>
<html>
  <head>
    <title>login.html</title>
	
    <meta name="keywords" content="keyword1,keyword2,keyword3">
    <meta name="description" content="this is my page">
    <meta name="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
  
  <body>
    <form name="f1" id="f1" action="j_security_check" method="post">
      <table>
        <tr>
          <td>Login:</td>
          <td><input type="text" name="j_username" id="login"></td>
        </tr>
        <tr>
          <td>Password:</td>
          <td><input type="password" name="j_password" id="password"></td>
        </tr> 
        <tr>
          <td colspan="2"><input type="submit"></td>
        </tr>
      </table>
    </form>
  </body>
</html>
<!DOCTYPE html>
<html>
  <head>
    <title>error.html</title>
	
    <meta name="keywords" content="keyword1,keyword2,keyword3">
    <meta name="description" content="this is my page">
    <meta name="content-type" content="text/html; charset=UTF-8">
    <meta charset="UTF-8">
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
  
  <body>
  error!
     身份认证错误 <br>
  </body>
</html>

最后,我们也就完成了未经认证的客户请求一个没有传输保证的受限资源


附上结果截图:

其中 Security/test.jsp为一个受限的资源

170838_k9SA_2301987.png

当未经授权的用户访问test.jsp时,会弹出一个身份认证的表单

170749_YlXS_2301987.png

输入正确的身份之后,访问成功,否则,访问失败

170749_xtHz_2301987.png









转载于:https://my.oschina.net/u/2301987/blog/546205

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值