如何从Spring在DataSource中设置角色 [英] How to set a role in DataSource from Spring

查看:95
本文介绍了如何从Spring在DataSource中设置角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用默认角色,以便在定义并连接数据源之后,该角色将允许我使用select语句.我知道它有点奇怪,但是出于安全考虑,我必须这样做.这是Spring的applicationContext.xml中的代码:

I have to use a default Role so that after the datasource has been defined and connected that role will allow me to use select statements. I know its little weird but I have to do it due to security concerns. So here is the code in my Spring's applicationContext.xml:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
    <property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver" />
    <property name="url"
            value="jdbc:db2://host_name:Port/DB_NAME:INFORMIXSERVER=SERVER_NAME;DELIMIDENT=y;" />
    <property name="username" value="user" />
    <property name="password" value="password" />
    <property name="minIdle" value="2" />
</bean>

一切都很好,我能够连接到数据源,但是我还需要告诉数据库所有查询都将通过该角色进行,因此我确实具有选择权限和内容: 对于命令提示符,我正在执行以下操作:

Everything is fine I am able to connect to the datasource but I also need to tell the database that all the queries will be through this ROLE so that I do have select rights and stuff: For command prompt I am doing like this:

set role ROLE_ACCESS_SELECT;

在执行sql命令之前,但不知道如何在此处进行设置.

before executing sql commands but don't know how to set it up here.

请咨询.

推荐答案

DBCP的 BasicDataSource 允许您传递要在连接初始化期间执行的SQL语句.像这样:

DBCP's BasicDataSource allows you to pass SQL statements to be executed during initialization of the connection. Something like this:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
    destroy-method="close">
    <property name = "connectionInitSqls">
        <list><value>set role ROLE_ACCESS_SELECT</value></list>
    </property>
    ...
</bean>

这篇关于如何从Spring在DataSource中设置角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆