tomcat的j_security_check验证机制 是利用tomcat自身配置的角色和用户名来登录应用系统的
配置如下:
tomcat-users.xml
<role rolename="tomcat"/> <role rolename="tian"/> <role rolename="manager"/> <user name="admin" password="admin" roles="tomcat,tian,manager" /> <user name="tian" password="tian" roles="tian,manager" />
web.xml
<!-- 首页 --> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> <security-constraint> <web-resource-collection> <web-resource-name>testLogin</web-resource-name> <url-pattern>/index.jsp</url-pattern> <http-method>GET</http-method> <http-method>POST</http-method> <http-method>PUT</http-method> </web-resource-collection> <auth-constraint> <role-name>tian</role-name> </auth-constraint> </security-constraint> <login-config> <auth-method>FORM</auth-method> <form-login-config> <form-login-page>/login.jsp</form-login-page> <form-error-page>/login.jsp?error=1</form-error-page> </form-login-config> </login-config> <security-role> <role-name>tian</role-name> </security-role>
其中,<url-pattern>中指定受限的url,可以使用通配符*,通常对整个目录进行访问权限控制。
<auth-constraint>中指定哪些角色可以访问<url-pattern>指定的url,在<role-name>中可以设置一个或多个角色名。
<form-login-page>指定登录页面url,<form-error-page>指定登录失败时的提示页面url。
login.jsp
<form method="get" action="<%=request.getContextPath() %>/j_security_check"> <fieldset> <legend>Login to Pluto</legend> <div> <label for="j_username">User Name</label> <input type="text" name="j_username" id="j_username"/> </div> <div> <label for="j_password">Password</label> <input type="password" name="j_password" id="j_password"/> </div> <div> <label for="j_login"></label> <input type="submit" value="Login" name="login" id="j_login"/> </div> </fieldset> </form>