Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.34 KB | None | 0 0
  1. public class LoginScreen extends JFrame implements Observer {
  2. private JLabel lblTitle, lblUsername, lblPassword;
  3. private JTextField txtVanReg;
  4. private JPasswordField txtPassword;
  5. private JPanel pnlCenter, pnlNorth;
  6. private JButton btnLogin, btnCancel;
  7. private final Font fntOther = new Font("Verdana", Font.PLAIN, 16);
  8. private final Font fntTitle = new Font("Verdana", Font.PLAIN, 20);
  9. private LoginController controller;
  10.  
  11. public LoginScreen() {
  12. this.controller = new LoginController(this);
  13. this.setTitle("Login");
  14. this.setLayout(new BorderLayout());
  15.  
  16. setUpComponents();
  17. }
  18.  
  19. private void setUpComponents() {
  20.  
  21. lblTitle = new JLabel("Burrito Business");
  22. lblUsername = new JLabel("Van reg: ");
  23. lblPassword = new JLabel("Password: ");
  24.  
  25. txtVanReg = new JTextField();
  26. txtPassword = new JPasswordField();
  27.  
  28. btnLogin = new JButton("Login");
  29. btnCancel = new JButton("Cancel");
  30.  
  31. btnLogin.addActionListener(new ActionListener() {
  32.  
  33. public void actionPerformed(ActionEvent e) {
  34. controller.loginRequested(txtVanReg.getText(), txtPassword.getPassword());
  35. }
  36. });
  37.  
  38. btnCancel.addActionListener(new ActionListener() {
  39.  
  40. public void actionPerformed(ActionEvent e) {
  41. controller.loginCancelled();
  42.  
  43. }
  44. });
  45.  
  46.  
  47. pnlCenter = new JPanel(new GridLayout(3, 3));
  48. pnlNorth = new JPanel(new BorderLayout());
  49.  
  50. pnlCenter.add(lblUsername);
  51. pnlCenter.add(txtVanReg);
  52. pnlCenter.add(lblPassword);
  53. pnlCenter.add(txtPassword);
  54. pnlCenter.add(btnLogin);
  55. pnlCenter.add(btnCancel);
  56.  
  57. pnlNorth.add(lblTitle);
  58. setFonts();
  59.  
  60. this.add(pnlNorth, BorderLayout.NORTH);
  61. this.add(pnlCenter, BorderLayout.CENTER);
  62.  
  63. this.setMinimumSize(new Dimension(400, 150));
  64. this.setResizable(false);
  65.  
  66. }
  67.  
  68. private void setFonts() {
  69.  
  70. lblTitle.setFont(fntTitle);
  71.  
  72. lblUsername.setFont(fntOther);
  73. lblPassword.setFont(fntOther);
  74. txtVanReg.setFont(fntOther);
  75. txtPassword.setFont(fntOther);
  76.  
  77. btnLogin.setFont(fntOther);
  78. btnCancel.setFont(fntOther);
  79.  
  80. }
  81.  
  82. /*
  83. * (non-Javadoc)
  84. *
  85. * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
  86. */
  87. public void update(Observable o, Object arg) {
  88. if (o instanceof DBUserAuth && ((Boolean) arg).booleanValue() == true) {
  89. JOptionPane.showMessageDialog(new JFrame(), "Login successful");
  90.  
  91. } else {
  92. JOptionPane.showMessageDialog(new JFrame(), "Login failed");
  93. }
  94.  
  95. }
  96.  
  97. public char[] getPassword() {
  98. return this.txtPassword.getPassword();
  99. }
  100.  
  101. public String getVanReg() {
  102. return this.txtVanReg.getText();
  103. }
  104.  
  105. public void close() {
  106. this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
  107. }
  108.  
  109. public class LoginController {
  110. private DBUserAuth dbConnection;
  111. private LoginScreen view;
  112.  
  113. public LoginController(LoginScreen view) {
  114. dbConnection = new DBUserAuth();
  115. this.view = view;
  116. dbConnection.addObserver(view);
  117. }
  118.  
  119. public void loginRequested(String van_reg, char[] password) {
  120. String pass = "";
  121.  
  122. for (char a : password) {
  123. pass += a;
  124. }
  125.  
  126. if (dbConnection.checkUser(van_reg, pass)) {
  127. StaffMenu aMenu = new StaffMenu(van_reg);
  128. aMenu.setVisible(true);
  129. view.close();
  130. }
  131.  
  132.  
  133. }
  134.  
  135. public void loginCancelled() {
  136. view.close();
  137. }
  138.  
  139. public class DBUserAuth extends Observable {
  140.  
  141. private Properties props;
  142. private Connection con;
  143. private String url, password, username;
  144. private PreparedStatement loginAuthStatement, addTruckStatement;
  145. private ResultSet results;
  146.  
  147. /**
  148. * Loads the properties file and gets the url, username and the password
  149. * from the file. Then goes on to open a connection to the database.
  150. */
  151. public DBUserAuth() {
  152.  
  153. // 1. Load properties in
  154. props = new Properties();
  155. ClassLoader loader = Thread.currentThread().getContextClassLoader();
  156. InputStream stream = loader.getResourceAsStream("prop.properties");
  157.  
  158. try {
  159. props.load(stream);
  160. url = props.getProperty("db.url");
  161. username = props.getProperty("db.username");
  162. password = props.getProperty("db.password");
  163. } catch (IOException e) {
  164. e.printStackTrace();
  165. System.out.println("Failed to load properties");
  166. }
  167.  
  168. }
  169.  
  170. /**
  171. * Used to connect to the DB
  172. *
  173. * @return True if success, false otherwise
  174. */
  175. private boolean openConnection() {
  176. try {
  177. con = DriverManager.getConnection(url, username, password);
  178. // connect to the db
  179. System.out.println("Connected to the DB");
  180. return true;
  181. } catch (SQLException e) {
  182. e.printStackTrace();
  183. System.out.println("Failed to connect to the DB");
  184. return false;
  185. }
  186. }
  187.  
  188. /**
  189. * Used to close connection to the DB
  190. *
  191. * @return True if success, false otherwise
  192. */
  193. private boolean closeConnection() {
  194. try {
  195. con.close();
  196. return true;
  197. } catch (SQLException e) {
  198. e.printStackTrace();
  199. return false;
  200. }
  201. }
  202.  
  203. /**
  204. *
  205. * @param password
  206. * the proposed password i.e. the password to check
  207. * @return
  208. */
  209. public boolean checkUser(String username, String password) {
  210. long time13 = System.currentTimeMillis();
  211. long time14;
  212. try {
  213. openConnection();
  214. loginAuthStatement = con.prepareStatement(
  215. "SELECT Van_reg_no, Hashed_Password FROM Food_Truck WHERE Van_reg_no LIKE ? ORDER BY Van_reg_no ASC");
  216. loginAuthStatement.setString(1, "%" + username + "%");
  217.  
  218. results = loginAuthStatement.executeQuery();
  219.  
  220. results.beforeFirst();
  221.  
  222. while (results.next()) {
  223. String reg = results.getString(1);
  224. String hashPassword = results.getString(2);
  225.  
  226. if (reg.equals(username) && BCrypt.checkpw(password, hashPassword)) {
  227. System.out.println("Login passed");
  228.  
  229. time14 = System.currentTimeMillis();
  230. System.out.println("Total login time: " + (time14 - time13) + " miliseconds.");
  231. setChanged();
  232. notifyObservers(new Boolean(true));
  233. return true;
  234. }
  235. }
  236.  
  237. } catch (SQLException e) {
  238. System.out.println("SQL for user auth. failed");
  239. e.printStackTrace();
  240. } finally {
  241. closeConnection();
  242. }
  243. setChanged();
  244. notifyObservers(new Boolean(false));
  245.  
  246. time14 = System.currentTimeMillis();
  247.  
  248. System.out.println("Total login time: " + (time14 - time13) + " miliseconds.");
  249. return false;
  250. }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement