Skip to content

Commit 4f50b44

Browse files
author
Nirmala Sundarappa
committed
Removed the "jdbc-ucp" folder
1 parent 7df487f commit 4f50b44

File tree

3 files changed

+222
-1
lines changed

3 files changed

+222
-1
lines changed

java/jdbc-ucp

Submodule jdbc-ucp deleted from 5e5c927
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
Rem ProxySessionSample.sql
2+
Rem
3+
Rem Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
4+
Rem
5+
Rem NAME
6+
Rem ProxySessionSample.sql - Complimentory SQL file for ProxySessionSample.java
7+
Rem
8+
Rem DESCRIPTION
9+
Rem The sample shows connecting to the Oracle Database using Proxy authentication
10+
Rem or N-tier authentication.
11+
Rem
12+
Rem MODIFIED (MM/DD/YY)
13+
Rem nbsundar 04/10/15 - Created
14+
Rem
15+
16+
Rem This sample requires connecting as a system user that has the ability to
17+
Rem create user and grant necessary privileges
18+
connect system/manager
19+
20+
Rem Clean up before creating necesary schema
21+
drop role select_role;
22+
drop role insert_role;
23+
drop role delete_role;
24+
25+
drop user jeff cascade;
26+
drop user smith cascade;
27+
drop user proxy cascade;
28+
29+
Rem Create new database users "jeff", "smith" and "proxy"
30+
create user proxy identified by proxy;
31+
create user jeff identified by jeffpasswd;
32+
create user smith identified by smithpasswd;
33+
34+
Rem Grant necessary privileges to DB users "proxy", "jeff" and "smith"
35+
grant create session, connect, resource, unlimited tablespace to proxy;
36+
grant create session, connect, resource, unlimited tablespace to jeff;
37+
grant create session, connect, resource, unlimited tablespace to smith;
38+
39+
Rem Create roles and grant necessary roles to users "jeff" and "smith"
40+
create role select_role;
41+
create role insert_role;
42+
create role delete_role;
43+
44+
Rem Connect as a proxy user
45+
connect proxy/proxy
46+
47+
Rem Create the table which will be shared with the users "jeff" and "smith"
48+
create table proxy_account (purchase number);
49+
insert into proxy_account values(11);
50+
insert into proxy_account values(13);
51+
52+
Rem Grant the required privileges
53+
grant select on proxy_account to select_role;
54+
grant insert on proxy_account to insert_role;
55+
grant delete on proxy_account to delete_role;
56+
57+
Rem Connect as system user to grant necessary roles to the DB users "jeff" and "smith"
58+
connect system/manager
59+
grant select_role, insert_role, delete_role to jeff;
60+
grant select_role, insert_role, delete_role to smith;
61+
62+
Rem grant the users "jeff" and "smith" to connect through proxy with specified roles
63+
alter user jeff grant connect through proxy with role select_role, insert_role;
64+
alter user smith grant connect through proxy with role select_role, insert_role;
65+
66+
commit;
67+
exit
68+

0 commit comments

Comments
 (0)