简单又好用的Tomcat 页面权限控制
不废话,就2个配置文件,直接上代码。
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID" version="3.0"><!-- <resource-ref><res-ref-name>jdbc/mt</res-ref-name><res-type>javax.sql.DataSource</res-type><res-auth>Container</res-auth><res-sharing-scope>Shareable</res-sharing-scope></resource-ref><security-role><role-name>R_CASE_IT</role-name></security-role> --><security-constraint><display-name>web</display-name><web-resource-collection><url-pattern>/index.jsp</url-pattern></web-resource-collection><auth-constraint><role-name>R_CASE_IT</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>/error.jsp</form-error-page></form-login-config></login-config><error-page><error-code>403</error-code><location>/403.jsp</location></error-page><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
<?xml version="1.0" encoding="UTF-8"?><Context path="/web"><Resource auth="Container" driverClassName="oracle.jdbc.OracleDriver"maxActive="10" maxIdle="4" maxWait="5000" name="jdbc/mt" password=""type="javax.sql.DataSource"url=""username="" /><!-- --><Realm className="org.apache.catalina.realm.DataSourceRealm"localDataSource="true"dataSourceName="jdbc/mt"roleNameCol="role_id" userCredCol="password" userNameCol="user_id"userRoleTable="rsms_user_in_roles" userTable="rsms_users" /></Context>
<form action="j_security_check" method="post"> Username<input type="text" name="j_username" /><br /> Password<input type="password" name="j_password" /><br /> <input type="submit" /> </form>
Context context = null; if (localDataSource) { context = ContextBindings.getClassLoader(); context = (Context) context.lookup("comp/env"); } else { context = ((StandardServer)getServer()).getGlobalNamingContext(); } DataSource dataSource = (DataSource)context.lookup(dataSourceName);