免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2962 | 回复: 0

[WebLogic] weblogic--创建安全的web应用程序 [复制链接]

论坛徽章:
0
发表于 2006-09-25 15:21 |显示全部楼层
为了保证web应用程序的安全,应该对登录的用户进行身份验证。在WebLogic Server中进行web应用程序的身份验证有两种方式:
1.基本验证方式(Basic Authentication)
2.表单验证方式(Form Authentication)
基本验证方式比较简单,而表单验证方式可以提供自定义的登录页面和出错处理页面。
1.基本验证方式(Basic Authentication)
        采用这种验证方式的web应用程序,用户访问时首先会弹出一个登录界面要求用户输入用户名和密码,然后查看此用户是否在web应用中定义的安全角色中。
        开发一个以基本验证方式进行身份验证的web应用程序的基本步骤如下。
        (1)在web应用程序的描述符web.xml中做如下声明:
   <security-role>
  <role-name>webuser</role-name>
 </security-role>
指定验证方式为基本验证方式:用<login-config>定义。例如:
<login-config>
  <auth-method>BASIC</auth-method>
  <realm-name>default</realm-name>
 </login-config>
定义被保护的资源,例如下面这段声明指明了只有角色webuser才能访问被保护的资源:
<security-constraint>
 <web-resource-collection>
  <web-resource-name>Success</web-resource-name>
  <url-pattern>/welcome.jsp</url-pattern>
   <http-method>GET</http-method>
   <http-method>POST</http-method>
 </web-resource-collection>
 <auth-constraint>
  <role-name>webuser</role-name>
 </auth-constraint>
 </security-constraint>
(2)在weblogic.xml文件中定义安全角色到weblogic server中用户或用户组的映射。即指定weblogic server中的哪些实体属于安全角色。例如下面这段声明将weblogic server中的实体myGroup映射到安全角色webUser:
<weblogic-web-app>
 <security-role-assignment>
  <role-name>webuser</role-name>
  <principal-name>myGroup</principal-name>
 </security-role-assignment>
</weblogic-web-app>
        下面时完整的相关页面:
        web.xml文件
       
        <web-app>
        <welcome-file-list>
                <welcome-file>welcome.jsp</welcome>
        </welcome-file-list>
 <security-constraint>
 <web-resource-collection>
  <web-resource-name>Success</web-resource-name>
  <url-pattern>/welcome.jsp</url-pattern>
   <http-method>GET</http-method>
   <http-method>POST</http-method>
 </web-resource-collection>
 <auth-constraint>
  <role-name>webuser</role-name>
 </auth-constraint>
 </security-constraint>
 <login-config>
  <auth-method>BASIC</auth-method>
  <realm-name>default</realm-name>
 </login-config>
 <security-role>
  <role-name>webuser</role-name>
 </security-role>
</web-app>

weblogic.xml文件
<weblogic-web-app>
 <security-role-assignment>
  <role-name>webuser</role-name>
  <principal-name>myGroup</principal-name>
 </security-role-assignment>
</weblogic-web-app>

welcome.jsp文件
<html>
        <head>
                <title>Browser Based Authentication Example Welcome Page</title>
        </head>
        <body>
        <h1>Browser Based Authentication Example Welcome Page</h1>
        <p>Welcome<%=request.getRemoteUser()%>
        </body>
</html>

2.表单验证方式
使用表单验证方式进行web应用程序的身份验证,需要开发者定义一个登录页面和登录失败的错误处理页面。登录页面可以时html、jsp或servlet。使用这种验证方式的好处时可以对程序有更进一步的控制。登录页面应该让用户输入用户名和密码,错误处理页面应该将验证失败的信息反馈给用户。开发使用表单验证方式的web应用程序的基本步骤如下。
(1)编写登录页面和错误处理页面,在web应用程序的欢迎页面中加入到登录页面的超级链接,提示用户首先进行登录。登录页面举例:
...
<form method="POST" action="j_security_check">
   <input type="text" name="j_username">
   <input type="text" name="j_password">
   <input type="submit" value="Log in">
</form>
...
(2)配置web.xml
<web-app>
<welcome-file-list>
                <welcome-file>welcome.jsp</welcome>
        </welcome-file-list>
<login-config>
   <auth-method>FORM</auth-method>
   <realm-name>Web Demo</realm-name>
   <form-login-config>
      <form-login-page>/admin/login.jsp</form-login-page>
      <form-error-page>/admin/error.jsp</form-error-page>
   </form-login-config>
</login-config>
<security-role>
  <role-name>webuser</role-name>
</security-role>
<security-constraint>
 <web-resource-collection>
  <web-resource-name>Success</web-resource-name>
  <url-pattern>/welcome.jsp</url-pattern>
   <http-method>GET</http-method>
   <http-method>POST</http-method>
 </web-resource-collection>
 <auth-constraint>
  <role-name>webuser</role-name>
 </auth-constraint>
</security-constraint>
<security-constraint>
 <web-resource-collection>
  <web-resource-name>login</web-resource-name>
  <url-pattern>/login2.jsp</url-pattern>
   <http-method>GET</http-method>
   <http-method>POST</http-method>
 </web-resource-collection>
 <auth-constraint>
  <role-name>webuser</role-name>
 </auth-constraint>
</security-constraint>
</web-app>

(3)在weblogic.xml文件中定义安全角色到weblogic server中实体(用户或用户组)的映射。例如:
<weblogic-web-app>
 <security-role-assignment>
  <role-name>webuser</role-name>
  <principal-name>myGroup</principal-name>
 </security-role-assignment>
</weblogic-web-app>

(引自《J2EE应用开发(webligic+JBuilder)》)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP