package jfxapp; import javafx.application.Platform; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.TabPane; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.GridPane; import org.apache.shiro.authc.credential.DefaultPasswordService; import org.apache.shiro.authc.credential.PasswordService; import org.h2.jdbcx.JdbcConnectionPool; import org.jooq.DSLContext; import org.jooq.Record; import org.jooq.SQLDialect; import org.jooq.impl.DSL; import org.postgresql.ds.PGPoolingDataSource; import java.net.URL; import java.sql.Connection; import java.sql.SQLException; import java.util.Arrays; import java.util.ResourceBundle; import static jfxapp.Tables.UDAJE; import static jfxapp.public_.Tables.PRIHLASENI; public class samexam8 { private PasswordService passwordService = new DefaultPasswordService(); private Connection H2CONN = null; private Connection PGCONN = null; private String host = "jdbc:postgresql://127.0.0.1:5432/fxguidedb"; private static final String h2file = "jdbc:h2:file:./Data/fxguidedb;CIPHER=AES"; private static final String jmenoh2 = "fxguide"; private static final String hesloh2 = "fxguide fxguide"; @FXML private ResourceBundle resources; @FXML private URL location; @FXML private AnchorPane contentPane; @FXML private TabPane tabPane; @FXML private GridPane grid1; @FXML private TextField plainpws, encpwd, matchpwd, matching; @FXML private TextArea data; @FXML private Button btn_MatchPwd, btn_EncPwd, btn_Data; @FXML void btn_Enc_Click(ActionEvent event) { encPwd(); } @FXML void btn_Match_Click(ActionEvent event) { matchPwd(); } @FXML void btn_Data_Click(ActionEvent event) throws SQLException { loginDB(); } @FXML void initialize() { } private void dialogMessage(final String text, final String title, final char type) { DialogFX dfx = new DialogFX(); dfx.setTitleText(title); dfx.setMessage(text); switch (type) { case 'A': dfx.setType(DialogFX.Type.ACCEPT); break; case 'E': dfx.setType(DialogFX.Type.ERROR); break; case 'I': dfx.setType(DialogFX.Type.INFO); break; } dfx.showDialog(); } private void setFocus(final TextField tf) { Platform.runLater(new Runnable() { @Override public void run() { tf.requestFocus(); } }); } private void encPwd() { encpwd.setText(passwordService.encryptPassword(plainpws.getText())); data.setPrefRowCount(2); data.setText("Počet znaků šifrovaného hesla: "+encpwd.getLength()+"\n"+encpwd.getText());} private void matchPwd() { Boolean match = passwordService.passwordsMatch(matchpwd.getText(),encpwd.getText()); matching.setText(match.toString()); } private static JdbcConnectionPool getH2Pool() throws SQLException { try { Class.forName("org.h2.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } JdbcConnectionPool h2cp = JdbcConnectionPool.create(h2file, jmenoh2, hesloh2); return h2cp; } private Connection connPGP(final String jmeno, final String heslo) { PGPoolingDataSource source = new PGPoolingDataSource(); source.setServerName("127.0.0.1:5432"); source.setDatabaseName("fxguidedb"); source.setUser(jmeno); source.setPassword(heslo); source.setMaxConnections(100); try { Class.forName("org.postgresql.Driver"); } catch (ClassNotFoundException ex) { ex.getMessage(); } try { PGCONN = source.getConnection(jmeno, heslo); return PGCONN; } catch (SQLException ex) { ex.getMessage(); } return PGCONN; } private static PGPoolingDataSource getPgPool (final String jmeno, final String heslo) throws SQLException { PGPoolingDataSource source = new PGPoolingDataSource(); source.setServerName("127.0.0.1:5432"); source.setDatabaseName("fxguidedb"); source.setUser(jmeno); source.setPassword(heslo); source.setMaxConnections(100); try { Class.forName("org.postgresql.Driver"); } catch (ClassNotFoundException ex) { ex.getMessage(); } return source; } private void Users() throws SQLException { JdbcConnectionPool h2pool = getH2Pool(); H2CONN = h2pool.getConnection(); DSLContext create = DSL.using(H2CONN, SQLDialect.H2); String[] users = create.select().from(PRIHLASENI).fetchArray(PRIHLASENI.UZIVATEL); Boolean infield = Arrays.asList(users).contains("safeuser"); data.setPrefRowCount(3); data.setText(Arrays.asList(users).toString()+"\n"+users[1]+"\n"+infield); H2CONN.close(); h2pool.dispose(); } private void loginDB() throws SQLException { JdbcConnectionPool h2pool = getH2Pool(); H2CONN = h2pool.getConnection(); DSLContext create = DSL.using(H2CONN, SQLDialect.H2); String[] users = create.select().from(PRIHLASENI).fetchArray(PRIHLASENI.UZIVATEL); String logName = plainpws.getText(); String logPassw = encpwd.getText(); Boolean inUsers = Arrays.asList(users).contains(logName); setFocus(plainpws); if (!inUsers) { dialogMessage("Zadaný uživatel neexistuje!","Přihlášení uživatele",'E'); plainpws.setText(""); encpwd.setText(""); setFocus(plainpws); return; } else { plainpws.setText(""); encpwd.setText(""); setFocus(plainpws); String passwd = (String) create .select() .from(PRIHLASENI) .where(PRIHLASENI.UZIVATEL.eq(logName)) .fetchOne(PRIHLASENI.HESLO); Boolean match = passwordService.passwordsMatch(logPassw, passwd); if (!match) { dialogMessage("Zadané heslo uživatele není správné!", "Přihlášení uživatele", 'E'); plainpws.setText(""); encpwd.setText(""); setFocus(plainpws); return; } else { dialogMessage("Uživatel byl úspěšně přihlášen","Přihlášení uživatele",'I'); PGPoolingDataSource pgpool = getPgPool("fxguide", "fxguide"); PGCONN = pgpool.getConnection(); DSLContext pgcreate = DSL.using(PGCONN, SQLDialect.POSTGRES); ObservableList pgdata = javafx.collections.FXCollections.observableArrayList(pgcreate .select() .from(UDAJE) .orderBy(UDAJE.ID) .fetch()); data.setText(pgdata.toString()); create.close(); H2CONN.close(); h2pool.dispose(); pgcreate.close(); PGCONN.close(); } } } }