weidagang2046的专栏

物格而后知致
随笔 - 8, 文章 - 409, 评论 - 101, 引用 - 0
数据加载中……

为tomcat页面设置访问权限(j_security_check)

web应用中,对页面的访问控制通常通过程序来控制,流程为:
登录 -> 设置session -> 访问受限页面时检查session是否存在,如果不存在,禁止访问

对于较小型的web应用,可以通过tomcat内置的访问控制机制来实现权限控制。采用这种机制的好处是,程序中无需进行权限控制,完全通过对tomcat的配置即可完成访问控制。

为了在tomcat页面设置访问权限控制,在项目的WEB-INFO/web.xml文件中,进行如下设置:

<web-app>

<!--servlet等其他配置-->

<security-constraint>
  <web-resource-collection>
      <display-name>Example Security Constraint</display-name>
      <web-resource-name>My Test</web-resource-name>
  
      <url-pattern>/ddly/admin/*</url-pattern>
  </web-resource-collection>
 
<auth-constraint>
    <role-name>role1</role-name>
    <role-name>tomcat</role-name>
</auth-constraint>

</security-constraint>

<login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>My Test</realm-name>
</login-config>

</web-app>

其中,<url-pattern>中指定受限的url,可以使用通配符*,通常对整个目录进行访问权限控制。
<auth-constraint>
中指定哪些角色可以访问<url-pattern>指定的url,在<role-name>中可以设置一个或多个角色名。

使用的角色名来自tomcat的配置文件${CATALINA_HOME}/conf/tomcat-users.xml

<login-config>中设置登录方式,<auth-method>的取值为BASICFORM。如果为BASIC,浏览器在需要登录时弹出一个登录窗口。如果为FORM方式,需要指定登录页面和登录失败时的提示信息显示页面。

使用FORM方式的配置样例如下:

<login-config>
   <auth-method>FORM</auth-method>
   <realm-name>Example Form-Based Authentication Area</realm-name>
   <form-login-config>
      <form-login-page>/login.jsp</form-login-page>
      <form-error-page>/error.jsp</form-error-page>
   </form-login-config>
</login-config>

其中的<form-login-page>指定登录页面url<form-error-page>指定登录失败时的提示页面url
登录页面中,formaction,以及其中的用户名和密码两个参数的名称,都应取固定的值。登录的后台处理程序为j_security_check;用户名和密码的参数名称分别为:j_usernamej_password
如下是登录页面(如:login.jsp)的一段示例代码:

<form method="POST" action='<%= response.encodeURL("j_security_check") %>' >
  <table border="0" cellspacing="5">
    <tr>
      <th align="right">Username:</th>
      <td align="left"><input type="text" name="j_username"></td>
    </tr>
    <tr>
      <th align="right">Password:</th>
      <td align="left"><input type="password" name="j_password"></td>
    </tr>
    <tr>
      <td align="right"><input type="submit" value="Log In"></td>
      <td align="left"><input type="reset"></td>
    </tr>
  </table>
</form>

posted on 2005-07-28 12:42 weidagang2046 阅读(4863) 评论(1)  编辑  收藏 所属分类: Java

评论

# re: 为tomcat页面设置访问权限(j_security_check)   回复  更多评论   

注意这里的url-pattern <url-pattern>/ddly/admin/*</url-pattern>其实是以本web app为根的相对路径。
2005-09-03 20:45 | weidagang2046

# re: 为tomcat页面设置访问权限(j_security_check)   回复  更多评论   

自己测试了一下,下面是我的web.xml配置文件:

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"">http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<security-constraint>
<web-resource-collection>
<display-name>Example Security Constraint</display-name>
<web-resource-name>welcome_info</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>role1</role-name>
<role-name>tomcat</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>welcome_info</realm-name>
</login-config>
</web-app>

2005-09-03 20:47 | weidagang2046

只有注册用户登录后才能发表评论。


网站导航: