---
title: "简单又好用的Tomcat 页面权限控制"
author: "78425665"
date: "2013-08-08T09:02:01Z"
tags: ["Java", "Security", "Tomcat"]
categories: ["Web前端"]
slug: "tomcat"
draft: false
lastmod: "2013-08-08T09:02:01Z"
id: 1921808
url: "https://78425665.iteye.com/blog/1921808"
---

不废话,就2个配置文件，直接上代码。 web.xml \[code="xml"] web /index.jsp R\_CASE\_IT FORM /login.jsp /error.jsp 403 /403.jsp index.jsp \[/code] tomcat中的context.xml \[code="xml"] \[/code] 登录页面login.jsp \[code="html"]

Username  
Password

\[/code] 在配置中遇到的问题： 在登录的时候报这个：name jdbc is not bound in this context 解决办法，在realm配置中加上 localDataSource="true"，否则会在tomcat中定义的realm中找dataSource,即（针对tomcat-user.xml定义的UserDataSource），原因看下面的源代码。 在org.apache.catalina.realm.DataSourceRealm中打开数据库连接的源代码： \[code="java"] 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); \[/code] 如果要自定义用户验证方式，将realm中的DataSourceRealm换成自定义的类（继承RealmBase或实现Realm接口，实现其验证方法）即可