亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

postgresql(set role user)命令在SSM項目中如何使用?

postgresql(set role user)命令在SSM項目中如何使用?

Helenr 2023-07-13 16:55:38
現在項目使用的是 springmvc+ spring + mybatis + druid + postgresql 項目中的用戶與數據庫中的用戶一一對應,所以每次運行SQL時都要通過(set role user)命令切換用戶,然后進行增刪改查數據庫的操作。我的問題:因為連接池中有很多連接,所以第一步就是獲取數據庫的連接,然后切換用戶,然后對數據庫進行業務SQL的操作。但我不知道這個邏輯應該由項目的哪一部分來處理,因為連接池的連接和SQL的執行都是由底層代碼實現的。你有什么好的計劃嗎?能否給我一個完整的demo,比如如下操作:步驟1,從spring security(或shiro)獲取用戶名。步驟2、從連接池中獲取當前使用數據庫的連接。步驟3、執行SQL(設置角色用戶)進行角色切換。步驟4、進行增刪改查操作。第五步、重置數據庫連接(重置角色)
查看完整描述

2 回答

?
蕭十郎

TA貢獻1815條經驗 獲得超13個贊

這是在mybatis-spring的幫助下完成您需要的操作的簡單方法。

除非您已經使用 mybatis-spring,否則第一步是更改項目的配置,以便您使用SqlSessionFactorymybatis?org.mybatis.spring.SqlSessionFactoryBean-spring 提供的功能。

下一步是設置/重置連接的用戶角色。在mybatis中,連接生命周期由實現接口的類控制org.apache.ibatis.transaction.Transaction。查詢執行器使用此類的實例來獲取連接。

簡而言之,您需要創建自己的此類實現并配置 mybatis 來使用它。

您的實現可以基于SpringManagedTransactionmybatis-spring ,并且看起來像:

import org.springframework.security.core.Authentication;


class UserRoleAwareSpringManagedTransaction extends SpringManagedTransaction {


? public UserRoleAwareSpringManagedTransaction(DataSource dataSource) {

? ? super(dataSource);

? }


? @Override

? public Connection getConnection() throws SQLException {

? ? Connection connection = getCurrentConnection();

? ? setUserRole(connection);

? ? return connection;

? }


? private Connection getCurrentConnection() {

? ? return super.getConnection();

? }


? @Override

? public void close() throws SQLException {

? ? resetUserRole(getCurrentConnection());

? ? super.close();

? }??


? private void setUserRole(Connection connection) {

? ? Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

? ? String username = authentication.getName();

? ? Statement statement = connection.createStatement();

? ? try {

? ? ? // note that this direct usage of usernmae is a subject for SQL injection

? ? ? // so you need to use the suggestion from

? ? ? // https://stackoverflow.com/questions/2998597/switch-role-after-connecting-to-database

? ? ? // about encoding of the username

? ? ? statement.execute("set role '" + username + "'");

? ? } finally {

? ? ? statement.close();

? ? }

? }


? private void resetUserRole(Connection connection) {

? ? Statement statement = connection.createStatement();

? ? try {

? ? ? statement.execute("reset role");

? ? } finally {

? ? ? statement.close();

? ? }

? }


}

現在您需要配置 mybatis 來使用您的Transaction實現。為此,您需要實現TransactionFactory類似于mybatis-springorg.mybatis.spring.transaction.SpringManagedTransactionFactory提供的功能:


public class UserRoleAwareSpringManagedTransactionFactory implements TransactionFactory {

? @Override

? public Transaction newTransaction(DataSource dataSource, TransactionIsolationLevel level, boolean autoCommit) {

? ? return new UserRoleAwareSpringManagedTransaction(dataSource);

? }

? @Override

? public Transaction newTransaction(Connection conn) {

? ? throw new UnsupportedOperationException("New Spring transactions require a DataSource");

? }

? @Override

? public void setProperties(Properties props) {

? }

}

然后UserRoleAwareSpringManagedTransactionFactory在 spring 上下文中定義一個類型的 bean 并將其注入到spring 上下文中transactionFactory的屬性中。SqlSessionFactoryBeen


現在mybatis每次獲取到Connection執行Transaction都會設置當前的spring security用戶來設置角色。


查看完整回答
反對 回復 2023-07-13
?
皈依舞

TA貢獻1851條經驗 獲得超3個贊

最佳實踐是數據庫用戶是應用程序。應用程序用戶對特定數據/資源的訪問應在應用程序中進行控制。應用程序不應依賴數據庫來限制數據/資源訪問。因此,應用程序用戶不應該在數據庫中具有不同的角色。應用程序應僅使用單個數據庫用戶帳戶。

Spring是最佳實踐的體現。因此Spring并沒有實現這個功能。如果你想要這樣的功能,你需要破解。您最好的選擇是:

@Autowired JdbcTemplate jdbcTemplate;?


// ...


public runPerUserSql() {


? ? jdbcTemplate.execute("set role user 'user_1';");

? ? jdbcTemplate.execute("SELECT 1;");


}

對此我還是沒有太大的信心。除非您正在為多個用戶編寫 pgAdmin Web 應用程序,否則您應該重新考慮您的方法和設計。


查看完整回答
反對 回復 2023-07-13
  • 2 回答
  • 0 關注
  • 152 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號