Java web的安全约束--Basic验证

要进行basic验证是用户名/口令机制,当浏览器要访问受保护的资源时,服务器会要求一个用户名和口令,只有输入了合法的用户名和口令。服务器才发送资源。用户名和口令可以存储在安全域中。安全域是标识一个Web应用程序的合法用户名和口令的“数据库”,其中还包含了与用户相关的角色。

例子:使用basic和MemoryRealm登录

    1、在tomcat下的/conf/tomcat-users.xml定义了角色和用户,还有一些角色

<tomcat-users xmlns="http://tomcat.apache.org/xml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
version="1.0">
<user username="li" password="123456" roles="admin-gui,manager-gui" />
<role rolename="manager"/>
<user username="admin" password="123456" roles="manager"/>

</tomcat-user>

    2、在web.xml中定义如下:

 

<security-constraint>
<web-resource-collection>
<web-resource-name>adminResource</web-resource-name>
<url-pattern>/AccountServlet</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<description>allManager</description>
<role-name>manager</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>

<login-config>
<auth-method>BASIC</auth-method>
<realm-name>test</realm-name>
</login-config>



<security-role>
<role-name>manager</role-name>
</security-role>

其中<security-constraint>标签设置受保护的资源和可以访问的用户角色;

<login-config>是验证机制,访问受保护的资源,弹出的对话框不同,它的<realm-name>test</realm-name>;是弹出的对话框中显示的安全域的名

<security-role>是用来定义,使用的角色

这样就可对资源进行保护,在访问资源时,在弹出的对话框中输入之前定义的用户,密码就可以访问了

转载于:https://www.cnblogs.com/l1019/p/6921265.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值