[转载]tomcat中配置DataSourceRealm

tomcat中配置DataSourceRealm
昨天和Tomcat奋战了整整8个钟头,23:30才搞定它。
目的:
使用Servlet2.3规定的Realm功能实现WEB程序的登录,将role控制在web.xml中申明。
方法:
1、在mysql中创建用户数据库
2、按照tomcat4.1.30的《JNDI DataSource HOW-TO》说明配置DBCP
3、按照tomcat4.1.30的《Realm HOW-TO》说明配置Realm
4、在web.xml中申明security-constraint
项目Context文件如下:
<!--
Context configuration file for the TEST Web App
--&gt


docBase="D:/projects/work/test/workspace"
debug="0"
privileged="true">

auth="Container"
type="javax.sql.DataSource" />



factory
org.apache.commons.dbcp.BasicDataSourceFactory

<!-- Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
--&gt

maxActive
100

<!-- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
--&gt

maxIdle
30

<!-- Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
--&gt

maxWait
10000


driverClassName
com.mysql.jdbc.Driver


url
jdbc:mysql://localhost/tps?autoReconnect=true


username
test


password
test



debug="99"
dataSourceName="java:/comp/env/jdbc/myds"
userTable="realms"
userNameCol="username"
userCredCol="password"
userRoleTable="realms"
roleNameCol="role" />


web.xml如下:
<?xml version="1.0" encoding="ISO-8859-1"?>

br /> "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<!--
- Web app deployment descriptor that loads both a root application context
- and an "test" DispatcherServlet with a specific context.
-
- Any number of additional DispatcherServlets can be added.
-
- Depends on the following libraries in WEB-INF/lib:
- * spring.jar
- * commons-logging.jar
- * log4j-1.2.x.jar
--&gt



<!--
- Key of the system property that should specify the root directory of this
- web app. Applied by WebAppRootListener or Log4jConfigListener.
--&gt

webAppRootKey
test.webapp.root


<!--
- Location of the Log4J config file, for initialization and refresh checks.
- Applied by Log4jConfigListener.
--&gt

log4jConfigLocation
/WEB-INF/classes/log4j.properties


<!--
- Location of the XML file that defines the root application context
- Applied by ContextLoaderListener.
--&gt

contextConfigLocation
/WEB-INF/applicationContext-hibernate.xml


<!--
- Configures Log4J for this web app.
- As this context specifies a context-param "log4jConfigLocation", its file path
- is used to load the Log4J configuration, including periodic refresh checks.
-
- Would fall back to default Log4J initialization (non-refreshing) if no special
- context-params are given.
-
- Exports a "web app root key", i.e. a system property that specifies the root
- directory of this web app, for usage in log file paths.
- This web app specifies "test.webapp.root" (see log4j.properties file).
--&gt

org.springframework.web.util.Log4jConfigListener


<!--
- Loads the root application context of this web app at startup,
- by default from "/WEB-INF/applicationContext.xml".
- Use WebApplicationContextUtils.getWebApplicationContext(servletContext)
- to access it anywhere in the web application, outside of the framework.
-
- The root context is the parent of all servlet-specific contexts.
- This means that its beans are automatically available in these child contexts,
- both for getBean(name) calls and (external) bean references.
--&gt

org.springframework.web.context.ContextLoaderListener


<!--
- Servlet that dispatches request to registered handlers (Controller implementations).
- Has its own application context, by default defined in "{servlet-name}-servlet.xml",
- i.e. "example-servlet.xml".
-
- A web app can contain any number of such servlets.
- Note that this web app has a shared root application context, serving as parent
- of all DispatcherServlet contexts.
--&gt

test
org.springframework.web.servlet.DispatcherServlet
1


<!--
- Maps the example dispatcher to /example/*. All handler mappings in
- example-servlet.xml will by default be applied to this subpath.
- If a mapping isn't a /* subpath, the handler mappings are considered
- relative to the web app root.
-
- A single dispatcher could get mapped to multiple paths, like any servlet.
--&gt

test
/test/*


test
*.do




Operator Area
*.jsp
*.do
*.html


operator



<!-- Login configuration uses form-based authentication --&gt

FORM
Tomcat Server Configuration Form-Based Authentication Area

/auth/login.do
/auth/error.do



<!-- Security roles referenced by this web application --&gt

admin


operator



然后将test.xml拷贝到tomcat4.1.30_home/webapps下,启动tomcat4.1.30开始测试

问题:
登录总是失败,检查tomcat4.1.30_home/logs发现发生异常javax.naming.NameNotFoundException: Name java: is not bound in this Context

解决方法:
经过漫长的各种尝试,和在google上的搜索,发现tomcat5.0的《Realm HOW-TO》在DataSourceRealn中增加了一个参数 localDataSource,说置成true才可以支持在context中申明的DataSource。经过试验后成功。看来tomcat4.1.30无法支持在context元素中申明的Datasource。
附上在tomcat5.0.25上配置成功的context文件,web.xml没有变化。context文件不在拷贝到webapps中而是拷贝到jakarta-tomcat-5.0.25confCatalinalocalhost上。Realm中申明的dataSourceName必须是相对于java:/comp/env的
<!--
Context configuration file for the TPS Web App
--&gt


docBase="D:/projects/work/wapgw/tps/workspace/tps2"
debug="0"
privileged="true">

auth="Container"
type="javax.sql.DataSource" />



factory
org.apache.commons.dbcp.BasicDataSourceFactory

<!-- Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
--&gt

maxActive
100

<!-- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
--&gt

maxIdle
30

<!-- Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
--&gt

maxWait
10000


driverClassName
com.mysql.jdbc.Driver


url
jdbc:mysql://localhost/tps?autoReconnect=true


username
tps


password
tps



debug="99"
dataSourceName="jdbc/myds"
localDataSource="true"
userTable="realms"
userNameCol="username"
userCredCol="password"
userRoleTable="realms"
roleNameCol="role" />

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/374079/viewspace-131938/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/374079/viewspace-131938/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值