| <#-- FreeMarker template (see http://freemarker.org/) |
| |
| Licensed to the Apache Software Foundation (ASF) under one |
| or more contributor license agreements. See the NOTICE file |
| distributed with this work for additional information |
| regarding copyright ownership. The ASF licenses this file |
| to you under the Apache License, Version 2.0 (the |
| "License"); you may not use this file except in compliance |
| with the License. You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, |
| software distributed under the License is distributed on an |
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| KIND, either express or implied. See the License for the |
| specific language governing permissions and limitations |
| under the License. |
| |
| --> |
| <#assign licenseFirst = "/*"> |
| <#assign licensePrefix = " * "> |
| <#assign licenseLast = " */"> |
| <#include "${project.licensePath}"> |
| |
| <#if package?? && package != ""> |
| package ${package}; |
| |
| </#if> |
| import java.awt.Dimension; |
| import java.awt.Toolkit; |
| import java.awt.event.ActionEvent; |
| import java.awt.event.ActionListener; |
| import java.io.IOException; |
| import java.net.Authenticator; |
| import java.net.PasswordAuthentication; |
| import java.util.Properties; |
| import java.util.logging.Level; |
| import java.util.logging.Logger; |
| import javax.swing.BorderFactory; |
| import javax.swing.JButton; |
| import javax.swing.JFrame; |
| import javax.swing.JLabel; |
| import javax.swing.JPanel; |
| import javax.swing.JPasswordField; |
| import javax.swing.JTextField; |
| |
| /** |
| * |
| * @author ${user} |
| */ |
| public class ${name} extends Authenticator { |
| |
| private static String username; |
| private static String password; |
| private static final String PROP_FILE = ${name}.class.getSimpleName().toLowerCase() + ".properties"; |
| |
| static { |
| try { |
| Properties props = new Properties(); |
| props.load(${name}.class.getResourceAsStream(PROP_FILE)); |
| username = props.getProperty("username"); |
| password = props.getProperty("password"); |
| } catch (IOException ex) { |
| Logger.getLogger(${name}.class.getName()).log(Level.SEVERE, null, ex); |
| } |
| } |
| |
| private static ${name} singleton = new ${name}(); |
| |
| public static void login() throws IOException { |
| if (!isValidUsernamePassword()) { |
| (new AuthenticationPanel()).show(); |
| if (!isValidUsernamePassword()) { |
| throw new IOException("Invalid username and password"); |
| } |
| } |
| |
| Authenticator.setDefault(singleton); |
| } |
| |
| private static boolean isValidUsernamePassword() { |
| return (username != null && username.length() > 0 && |
| password != null && password.length() > 0); |
| } |
| |
| private ${name}() { |
| } |
| |
| protected PasswordAuthentication getPasswordAuthentication() { |
| return new PasswordAuthentication(username, password.toCharArray()); |
| } |
| |
| static class AuthenticationPanel implements ActionListener { |
| private JFrame frame; |
| private JPanel panel; |
| private JTextField userNameTF = new JTextField(15); |
| private JPasswordField passwordTF = new JPasswordField(15); |
| |
| public void makePanel() { |
| JButton submitButton = new JButton("Submit"); |
| JButton cancelButton = new JButton("Cancel"); |
| JLabel unLabel = new JLabel("User Name :"); |
| JLabel pwdLabel = new JLabel("Password :"); |
| |
| userNameTF.setText(""); |
| passwordTF.setText(""); |
| |
| //adding elements to panel |
| panel = new JPanel(); |
| panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 10)); |
| panel.setLayout(null); |
| unLabel.setBounds(40, 40, 100, 20); |
| userNameTF.setBounds(150, 40, 150, 20); |
| pwdLabel.setBounds(40, 80, 100, 20); |
| passwordTF.setBounds(150, 80, 150, 20); |
| submitButton.setBounds(60, 140, 100, 30); |
| cancelButton.setBounds(180, 140, 100, 30); |
| |
| cancelButton.addActionListener(this); |
| submitButton.addActionListener(this); |
| submitButton.setActionCommand("Submit"); |
| cancelButton.setActionCommand("Cancel"); |
| |
| panel.add(unLabel); |
| panel.add(userNameTF); |
| panel.add(pwdLabel); |
| panel.add(passwordTF); |
| panel.add(submitButton); |
| panel.add(cancelButton); |
| |
| userNameTF.addActionListener(new ActionListener() { |
| |
| public void actionPerformed(ActionEvent e) { |
| passwordTF.grabFocus(); |
| } |
| |
| }); |
| |
| passwordTF.addActionListener(new ActionListener() { |
| |
| public void actionPerformed(ActionEvent e) { |
| AuthenticationPanel.this.actionPerformed(new ActionEvent(passwordTF, 0, "Password")); |
| } |
| |
| }); |
| userNameTF.getInputMap().put(javax.swing.KeyStroke.getKeyStroke("ESCAPE"), "Cancel"); |
| passwordTF.getInputMap().put(javax.swing.KeyStroke.getKeyStroke("ESCAPE"), "Cancel"); |
| userNameTF.getActionMap().put("Cancel", new javax.swing.AbstractAction() { |
| public void actionPerformed(ActionEvent e) { |
| AuthenticationPanel.this.actionPerformed(new ActionEvent(userNameTF, 0, "Cancel")); |
| } |
| }); |
| passwordTF.getActionMap().put("Cancel", new javax.swing.AbstractAction() { |
| public void actionPerformed(ActionEvent e) { |
| AuthenticationPanel.this.actionPerformed(new ActionEvent(passwordTF, 1, "Cancel")); |
| } |
| }); |
| } |
| |
| //method to call to show authentication...can be replaced by main() if required |
| public void show() { |
| frame = new JFrame("User Authentication"); |
| makePanel(); |
| //setting up the frame |
| frame.getContentPane().add(panel); |
| frame.setSize(350, 250); |
| frame.setVisible(true); |
| |
| //code to center the frame |
| Toolkit tk = Toolkit.getDefaultToolkit(); |
| Dimension scrnSize = tk.getScreenSize(); |
| frame.setLocation((int) (scrnSize.getWidth() - frame.getWidth()) / 2, (int) (scrnSize.getHeight() - frame.getHeight()) / 2); |
| |
| synchronized (this) { |
| try { |
| this.wait(); |
| } catch (InterruptedException ex) { |
| } |
| } |
| } |
| |
| //code to do actions on button press |
| public void actionPerformed(ActionEvent evt) { |
| String actionSource = evt.getActionCommand(); |
| if (actionSource.equals("Cancel")) { |
| frame.dispose(); |
| } else if (actionSource.equals("Submit") || actionSource.equals("Password")) { |
| username = userNameTF.getText(); |
| password = new String(passwordTF.getPassword()); |
| frame.dispose(); |
| //validate using connection to database |
| //if validation is right move to next page |
| } |
| |
| synchronized (this) { |
| this.notify(); |
| } |
| } |
| } |
| } |