Advertisement
Guest User

Untitled

a guest
Jan 29th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  2. <property name="driverClassName" value="${database.driver}"/>
  3. <property name="url" value="${database.url}"/>
  4. <property name="username" value="${database.user}"/>
  5. <property name="password" value="${database.password}"/>
  6. </bean>
  7.  
  8. database.driver=com.mysql.jdbc.Driver
  9. database.url=jdbc:mysql://192.168.1.2:3306/db
  10. database.user=root
  11. database.password=password
  12.  
  13. <dependency>
  14. <groupId>com.jcraft</groupId>
  15. <artifactId>jsch</artifactId>
  16. <version>0.1.53</version>
  17. </dependency>
  18.  
  19. import com.jcraft.jsch.JSch;
  20. import com.jcraft.jsch.Session;
  21.  
  22. public class SSHConnection {
  23.  
  24. private final static String S_PATH_FILE_PRIVATE_KEY = "C:\Users\Val\.ssh\privatekeyputy.ppk"; \windows absolut path of our ssh private key locally saved
  25. private final static String S_PATH_FILE_KNOWN_HOSTS = "C:\Users\Val\.ssh\known_hosts";
  26. private final static String S_PASS_PHRASE = "mypassphrase";
  27. private final static int LOCAl_PORT = 3307;
  28. private final static int REMOTE_PORT = 3306;
  29. private final static int SSH_REMOTE_PORT = 22;
  30. private final static String SSH_USER = "87a34c7f89f5cf407100093c";
  31. private final static String SSH_REMOTE_SERVER = "myapp-mydomain.rhcloud.com";
  32. private final static String MYSQL_REMOTE_SERVER = "127.6.159.102";
  33.  
  34. private Session sesion; //represents each ssh session
  35.  
  36. public void closeSSH ()
  37. {
  38. sesion.disconnect();
  39. }
  40.  
  41. public SSHConnection () throws Throwable
  42. {
  43.  
  44. JSch jsch = null;
  45.  
  46. jsch = new JSch();
  47. jsch.setKnownHosts(S_PATH_FILE_KNOWN_HOSTS);
  48. jsch.addIdentity(S_PATH_FILE_PRIVATE_KEY, S_PASS_PHRASE.getBytes());
  49.  
  50. sesion = jsch.getSession(SSH_USER, SSH_REMOTE_SERVER, SSH_REMOTE_PORT);
  51. sesion.connect(); //ssh connection established!
  52.  
  53. //by security policy, you must connect through a fowarded port
  54. sesion.setPortForwardingL(LOCAl_PORT, MYSQL_REMOTE_SERVER, REMOTE_PORT);
  55.  
  56. }
  57. }
  58.  
  59. @WebListener
  60. public class MyContextListener implements ServletContextListener {
  61.  
  62. private SSHConnection conexionssh;
  63.  
  64.  
  65. public MyContextListener()
  66. {
  67. super();
  68. }
  69.  
  70. /**
  71. * @see ServletContextListener#contextInitialized(ServletContextEvent)
  72. */
  73. public void contextInitialized(ServletContextEvent arg0)
  74. {
  75. System.out.println("Context initialized ... !");
  76. try
  77. {
  78. conexionssh = new SSHConnection();
  79. }
  80. catch (Throwable e)
  81. {
  82. e.printStackTrace(); // error connecting SSH server
  83. }
  84. }
  85.  
  86. /**
  87. * @see ServletContextListener#contextDestroyed(ServletContextEvent)
  88. */
  89. public void contextDestroyed(ServletContextEvent arg0)
  90. {
  91. System.out.println("Context destroyed ... !");
  92. conexionssh.closeSSH(); // disconnect
  93. }
  94.  
  95. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  96. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  97. <property name="url" value="jdbc:mysql://localhost:3307/yourSchema" />
  98. <property name="username" value="admin4ajCbcWM" />
  99. <property name="password" value="dxvfwEfbyaPL-z" />
  100. </bean>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement