|
| 1 | +/* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.*/ |
| 2 | + |
| 3 | +/* |
| 4 | + DESCRIPTION |
| 5 | + The code sample demonstrates how to connect to the Oracle Database using |
| 6 | + Proxy authentication or N-tier authentication. Proxy authentication is the |
| 7 | + process of using a middle tier for user authentication. Proxy connections |
| 8 | + can be created using any one of the following options. |
| 9 | + (a) USER NAME: Done by supplying the user name or the password or both. |
| 10 | + (b) DISTINGUISHED NAME: This is a global name in lieu of the password of |
| 11 | + the user being proxied for. |
| 12 | + (c) CERTIFICATE:More encrypted way of passing the credentials of the user, |
| 13 | + who is to be proxied, to the database. |
| 14 | + |
| 15 | + Step 1: Connect to SQLPLUS using the database USER/PASSWORD. |
| 16 | + Make sure to have ProxySessionSample.sql accessible to |
| 17 | + execute from sqlplus. Update ProxySessionSample.sql with correct |
| 18 | + SYSTEM username and password. |
| 19 | + Step 2: Run the SQL file after connecting to DB "@ProxySessionSample.sql" |
| 20 | + Step 3: Enter the Database details in this file. DB_URL is required. |
| 21 | + Step 4: Run the sample with "ant ProxySessionSample" |
| 22 | +
|
| 23 | + NOTES |
| 24 | + Use JDK 1.7 and above |
| 25 | +
|
| 26 | + MODIFIED (MM/DD/YY) |
| 27 | + nbsundar 04/10/15 - creation |
| 28 | + */ |
| 29 | + |
| 30 | +import java.sql.Connection; |
| 31 | +import java.sql.ResultSet; |
| 32 | +import java.sql.SQLException; |
| 33 | +import java.sql.Statement; |
| 34 | +import java.util.Properties; |
| 35 | + |
| 36 | +import oracle.jdbc.OracleConnection; |
| 37 | +import oracle.jdbc.pool.OracleDataSource; |
| 38 | + |
| 39 | +class ProxySessionSample { |
| 40 | + final static String DB_URL= "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(HOST=myhost)(PORT=1521)(PROTOCOL=tcp))(CONNECT_DATA=(SERVICE_NAME=myorcldbservicename)))"; |
| 41 | + |
| 42 | + /* |
| 43 | + * The code sample shows how to connect to an Oracle Database |
| 44 | + * using Proxy Session. The sample has the following: |
| 45 | + * (a) A shared table, PROXY_ACCOUNT owned by user PROXY. |
| 46 | + * (b) Users JEFF and SMITH have necessary roles for performing a SELECT, |
| 47 | + * INSERT and DELETE on table PROXY_ACCOUNT owned by PROXY user, |
| 48 | + * through the roles select_role, insert_role and delete_role. |
| 49 | + * Note that select_role has SELECT, insert_role has INSERT and delete_role |
| 50 | + * has DELETE privileges granted. |
| 51 | + * |
| 52 | + * The control flow in the sample is as follows: |
| 53 | + * (1) Obtain a database connection of user PROXY. |
| 54 | + * (2) Provide required privileges to users JEFF and SMITH to connect to the |
| 55 | + * database through user PROXY. |
| 56 | + * (3) Open a proxy session for users JEFF and SMITH. This does not open a |
| 57 | + * new connection to the database instead uses the pre-existing connection |
| 58 | + *(as user PROXY). The proxy session is established with the roles specified |
| 59 | + * while opening the connection. |
| 60 | + */ |
| 61 | + public static void main(String args[]) throws SQLException { |
| 62 | + OracleDataSource ods = new OracleDataSource(); |
| 63 | + |
| 64 | + // retrieve a database connection of user "proxy" |
| 65 | + OracleConnection proxyConn = getConnection("proxy", "proxy", DB_URL, ods); |
| 66 | + |
| 67 | + // isProxySession is false before opening a proxy session |
| 68 | + System.out.println("Before a proxy session is open, isProxySession: " |
| 69 | + + proxyConn.isProxySession()); |
| 70 | + // check if the user is "proxy" |
| 71 | + checkUser(proxyConn); |
| 72 | + |
| 73 | + // open a proxy session for the user "jeff". |
| 74 | + // This session reuses existing proxy session to connect as user, "jeff". |
| 75 | + // There is no need to authenticate the user "jeff". |
| 76 | + demoProxySession(proxyConn, "jeff"); |
| 77 | + |
| 78 | + // open a proxy session for the user "smith". |
| 79 | + // This session reuses existing proxy session to connect as user "smith" |
| 80 | + // There is no need to authenticate the user "smith". |
| 81 | + demoProxySession(proxyConn, "smith"); |
| 82 | + |
| 83 | + // Close the proxy connection |
| 84 | + proxyConn.close(); |
| 85 | + } |
| 86 | + /* |
| 87 | + * Demonstrates the following: |
| 88 | + * (1) Start a Proxy Session: Starts the proxy Session with corresponding |
| 89 | + * roles and authenticates the users "jeff" or "smith". |
| 90 | + * (2) Access Proxy user's table: The users "jeff" or "smith" can access |
| 91 | + * the "proxy" user table, 'proxy_account' through the proxy session. |
| 92 | + * (3) Close the Proxy Session: Close the proxy session for the user "jeff" |
| 93 | + * or "smith". |
| 94 | + */ |
| 95 | + private static void demoProxySession(OracleConnection conn, String proxyUser) |
| 96 | + throws SQLException { |
| 97 | + Properties prop = new Properties(); |
| 98 | + prop.put(OracleConnection.PROXY_USER_NAME, proxyUser); |
| 99 | + // corresponds to the alter sql statement (select, insert roles) |
| 100 | + String[] roles = { "select_role", "insert_role" }; |
| 101 | + prop.put(OracleConnection.PROXY_ROLES, roles); |
| 102 | + conn.openProxySession(OracleConnection.PROXYTYPE_USER_NAME, prop); |
| 103 | + System.out.println("======= demoProxySession BEGIN ======="); |
| 104 | + System.out.println("After the proxy session is open, isProxySession: " |
| 105 | + + conn.isProxySession()); |
| 106 | + // proxy session can act as users "jeff" & "smith" to access the |
| 107 | + // user "proxy" tables |
| 108 | + try (Statement stmt = conn.createStatement()) { |
| 109 | + // Check who is the database user |
| 110 | + checkUser(conn); |
| 111 | + // play insert_role into proxy.proxy_account, go through |
| 112 | + stmt.execute("insert into proxy.proxy_account values (1)"); |
| 113 | + System.out.println("insert into proxy.proxy_account, allowed"); |
| 114 | + // play select_role from proxy.proxy_account, go through |
| 115 | + try (ResultSet rset = stmt.executeQuery("select * from " |
| 116 | + + " proxy.proxy_account")) { |
| 117 | + while (rset.next()) { |
| 118 | + // display the execution results of a select query. |
| 119 | + System.out.println(rset.getString(1)); |
| 120 | + } |
| 121 | + System.out.println("select * from proxy.proxy_account, allowed"); |
| 122 | + // play delete_role from proxy.proxy_account, SQLException |
| 123 | + stmt.execute("delete from proxy.proxy_account where purchase=1"); |
| 124 | + } catch(Exception e) { |
| 125 | + System.out.println("delete from proxy.proxy_account, not allowed"); |
| 126 | + } |
| 127 | + System.out.println("======= demoProxySession END ======="); |
| 128 | + // Close the proxy session of user "jeff" |
| 129 | + conn.close(OracleConnection.PROXY_SESSION); |
| 130 | + } |
| 131 | + } |
| 132 | + /* |
| 133 | + * Gets a database connection using a proxy user. |
| 134 | + */ |
| 135 | + private static OracleConnection getConnection(String user, String password, |
| 136 | + String url, OracleDataSource ods) throws SQLException { |
| 137 | + ods.setUser(user); |
| 138 | + ods.setPassword(password); |
| 139 | + ods.setURL(url); |
| 140 | + return ((OracleConnection) ods.getConnection()); |
| 141 | + } |
| 142 | + /* |
| 143 | + * Checks the database user. Note that the user will be proxy. |
| 144 | + */ |
| 145 | + private static void checkUser(Connection conn) throws SQLException { |
| 146 | + try (Statement stmt = conn.createStatement()) { |
| 147 | + try (ResultSet rset = stmt.executeQuery("select user from dual")) { |
| 148 | + while (rset.next()) { |
| 149 | + System.out.println("User is: " + rset.getString(1)); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments