· 4 years ago · Dec 06, 2020, 12:58 PM
1//LOGIN
2String a = jTextField1.getText().toLowerCase();
3 String b = hash.hash(jPasswordField1.getText());
4 boolean isComplete = false;
5 String strCurrentLine;
6 BufferedReader br = null;
7
8 try {
9 br = new BufferedReader(new FileReader("loginData.txt"));
10
11 while ((strCurrentLine = br.readLine()) != null) {
12
13 String[] tempstringarray = strCurrentLine.split(",");
14
15 if (a.equals(tempstringarray[0]) && b.equals(tempstringarray[1]) && "admin".equals(tempstringarray[2])) {
16 new adminDashboard().setVisible(true);
17 this.setVisible(false);
18 isComplete = true;
19 } else if (a.equals(tempstringarray[0]) && b.equals(tempstringarray[1]) && "general".equals(tempstringarray[2])) {
20 new generalDashboard().setVisible(true);
21 this.setVisible(false);
22 isComplete = true;
23 }
24 }
25
26 } catch (FileNotFoundException ex) {
27 Logger.getLogger(mainPanel.class.getName()).log(Level.SEVERE, null, ex);
28 } catch (IOException ex) {
29 Logger.getLogger(mainPanel.class.getName()).log(Level.SEVERE, null, ex);
30 }
31
32//Move to new JFrame
33this.setVisible(false);
34registerPanel.setVisible(true);
35
36//Send Email
37
38package computerscienceia;
39
40
41import java.util.Properties;
42import javax.mail.Authenticator;
43import javax.mail.Message;
44import javax.mail.MessagingException;
45import javax.mail.PasswordAuthentication;
46import javax.mail.Session;
47import javax.mail.Transport;
48import javax.mail.internet.AddressException;
49import javax.mail.internet.InternetAddress;
50import javax.mail.internet.MimeMessage;
51
52public class JavaMailUtil {
53 public static void sendMail(String recepient, String subject, String contentEmail) throws MessagingException{
54 System.out.println("Preparing");
55 Properties properties = new Properties();
56
57 properties.put("mail.smtp.auth", true); //Starts Authorisation
58 properties.put("mail.smtp.starttls.enable", true); //Starts TLS protocol
59 properties.put("mail.smtp.host", "smtp.gmail.com"); //Connects to Gmail Host
60 properties.put("mail.smtp.port", "587"); // SMTP protocol hosted on port 587
61
62 String myAccountEmail = "@gmail.com"; //Login Information
63 String myAccountPassword = ""; //Login Password
64
65 Session session = Session.getInstance(properties, new Authenticator(){ //Authentication Function
66 @Override
67 protected PasswordAuthentication getPasswordAuthentication()
68 {
69 return new PasswordAuthentication(myAccountEmail, myAccountPassword);
70 }
71 });
72
73 Message message = prepareMessage(session, myAccountEmail, recepient, subject, contentEmail);
74
75 Transport.send(message);
76 System.out.println("sent successfully");
77 }
78
79 public static Message prepareMessage(Session session, String myAccountEmail, String recepient, String subject, String contentEmail) throws AddressException, MessagingException {
80 try{
81 Message message = new MimeMessage(session);
82 message.setFrom(new InternetAddress(myAccountEmail));
83
84 message.setRecipient(Message.RecipientType.TO, new InternetAddress(recepient));
85 message.setSubject(subject);
86 message.setText(contentEmail);
87 return message;
88 }
89 catch (Exception e)
90 {
91
92 }
93
94 return null;
95 }
96
97
98
99}
100
101//Adding an icon on MACOSX
102//CREATE A NEW CLASS CALLED icon
103
104import javax.swing.Icon;
105import javax.swing.ImageIcon;
106public class icon {
107 public static javax.swing.Icon icon()
108 {
109 Icon icon = new ImageIcon("src/computerscienceia/logo.png");
110 return icon;
111 }
112}
113
114//REFERENCE FOR CODE
115
116jLabel4.setIcon(icon.icon());
117
118// Writing to a file
119BufferedWriter bw = new BufferedWriter(new FileWriter("file.txt",true)) //flag as false if you want to delete all the previous content
120bw.write("Whatever"+"\n");
121bw.close();
122
123//Reading a File
124
125String str;
126BufferedReader br = new BufferedReader(new FileReader("file.txt"));
127
128str = br.readLine(); //ignores the first line of the File
129
130while ((str = br.readLine())!=null){
131 String[] tempstringarray = str.split(","); //creates an array, seperating each of the elements with a comma
132 if (tempstringarray[0].equals("whatever"))
133 {
134 //do work
135 }
136}
137
138//FORGOT Password
139
140/*
141generate a random number
142use the email system above to send them an email of the number
143check if the number generated is the same as the textfield
144redirect them to change password page
145*/
146
147//HASHING A Password
148
149import java.security.MessageDigest;
150import java.security.NoSuchAlgorithmException;
151
152public class hash {
153 public static String hash (String password) {
154
155 try{
156 MessageDigest messageDigest = MessageDigest.getInstance("MD5");
157 messageDigest.update(password.getBytes());
158
159 byte[] resultByteArray = messageDigest.digest();
160
161 StringBuilder sb = new StringBuilder();
162
163 for (byte b : resultByteArray) {
164 sb.append(String.format("%02x",b));
165 }
166
167 return sb.toString();
168 }
169
170 catch (NoSuchAlgorithmException e) {
171 e.printStackTrace();
172 }
173
174 return "";
175 }
176
177}
178
179//Reading jTextField
180
181jTextField.getText();
182
183//Setting jLabel
184
185jLabel.setText("text");
186
187//Getting item from combo box
188
189String string = (String) jComboBox.getSelectedItem();
190
191//Checking if jCheckBox or jRadioButton is selected
192
193boolean bool = jCheckBox.isSelected();