codice:
/*
Apcontrol.java Agosto 1.999 Copyright() N. Betancort
Applet que contiene un panel para control de acceso a p ginas web
Applet with a panel for password control of access to a Web page.
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import java.net.URL;
import java.io.*;
//clase principal -- main class
public class Apcontrol extends Applet {
static final Color CFONDO = new Color(200,100,120); //color fondo
private int x = 300;
private int y = 240;
int id = 0; //parametro de idioma (0-spanish / 1- english)
int contador = 0; // contador n£mero de intentos --counter
int ikey = 0; //llave encriptado crypto-key
boolean encriptado = false; // centinela encriptado
final static String mensa[][] ={
{ "Introduzca ID de usuario y password\n",
"Comprobando usuario y password\n", "Usuario localizado\n",
"password comprobado\n", "password err¢neo, acceso denegado \n",
"Usuario no localizado\n", "Error en lectura fichero de claves\n",
"Agotados los 3 intentos\n Applet deshabilitado",
"Error en apertura fichero de claves\n", "no accesible",
"Error de acceso a pagina WEB ",
"Los campos de texto no pueden estar vacios\n",
"Error en parametro de encriptado\n",
"13", "14" },
{ "Type your ID and password\n", "Checking user ID and password\n",
"User localized\n","password OK\n","password incorrect, access denied\n",
"User not found\n", "Error reading keys file \n",
"Over 3 trys Applet disabled","Error opening keys file \n",
"not accesible \n", "Error loading the web page \n",
"Unable to process empty fields\n","key Encryption Parameter Error\n",
"13","14" }
};
Panel marco;
Apform formu;
Cifrado cifra = new Cifrado(); //Instanciar clase para cifrado
TextArea ta = new TextArea(2,20);
private Panel ptitu = new Panel(); //Panel para cabecera
private Label ltitu;
private String sfile = "lista.txt"; //nombre por defecto-default name
private String stitu = "Access Control"; //titulo por defecto-default title
private String webpg = "welcome.html"; //pagina por defecto-default page
public Dimension getPreferredSize() { return new Dimension(x,y);}
public void init(){
setLayout(null);
setSize(getPreferredSize());
setBackground(CFONDO);
//Captura de par metros--getting parameters
String sx = getParameter("an"); //dimensiones
String sy = getParameter("al");
if (sx != null & sy != null){
x = (Integer.valueOf(sx)).intValue();
y = (Integer.valueOf(sy)).intValue();
}
String idiom = getParameter("idioma"); //Idioma ingl‚s-espa¤ol
if (idiom != null & idiom.equals("eng")) {id = 1 ;}
try {
String skey = getParameter("key");
ikey = (Integer.valueOf(skey)).intValue();
if(ikey != 0){
this.encriptado = true;
boolean retorno = cifra.gencode(ikey);
}
}
catch(Exception e){ ta.setText(mensa[id][12]); }
String stx = getParameter("title"); //Asignar t¡tulo
if ( stx != null) { stitu = stx; }
ltitu = new Label(stitu); //Etiqueta con t¡tulo
String sfx = getParameter("claves"); //Archivo de claves
if ( stx != null) { sfile = sfx; } //keys file
marco = new Tapiz(x,y, CFONDO);
marco.setBounds(0,0, x, y);
add(marco);
ptitu.setBounds(20,20,x-40,y/8);
ptitu.setBackground(CFONDO);
ltitu.setForeground(new Color(10,10,240));
ltitu.setFont(new Font("Serif",Font.BOLD, 16));
ptitu.add("Center",ltitu);
marco.add(ptitu);
formu = new Apform(this, CFONDO, id);
marco.add(formu);
formu.setBounds(20,30+y/8,x-40,3*y/8);
ta.setEditable(false);
ta.append(mensa[id][0]); //modif.
ta.setBackground(Color.white);
marco.add(ta);
ta.setBounds(20, 40+y/2,x-40, y/4);
}
//..................................................
public void procesar(String us, String pw){
String line;
String usread, pwread;
String enus = us;
String enpw = pw;
boolean acceso = false;
StringTokenizer st;
InputStreamReader ist;
if( encriptado == true ) { //encriptar usuario y password
enus = cifra.encript(us, 0);
enpw = cifra.encript(pw, 0);
}
try {
ta.append(mensa[id][1]); // Comprobando usuario y password
InputStream is = new URL(getCodeBase(), sfile).openStream();
ist = new InputStreamReader(is);
BufferedReader br = new BufferedReader(ist,8000);
try {
contador++;
scan:
while((line = br.readLine())!=null) {
st = new StringTokenizer(line);
if(st.countTokens()==3){
usread = st.nextToken();
pwread = st.nextToken();
webpg = st.nextToken();
if(usread.equals(enus)) { //comprobar usuario
ta.append(mensa[id][2]); //Usuario localizado
if(pwread.equals(enpw)){ //comprobar password
ta.append(mensa[id][3]); //password reconocido
acceso = true;
break scan;
}
else{
ta.append(mensa[id][4]); //pasword err¢neo acceso denegado
break scan;
}
} //fin bucle if comprobar usuario
} //fin if contar tokens
} //fin while fin de b£squeda
if (acceso==true) {
if( encriptado == true) { webpg = cifra.encript(webpg, 1); }
cargar();}
else{
if(contador >= 3) {
ta.setText(mensa[id][7]); //agotados 3 intentos
formu.user.setEditable(false);
formu.pasw.setEditable(false);
formu.reset.setEnabled(false);
}
else{
ta.append(mensa[id][5]); //usuario no localizado
formu.submit.setEnabled(true);
}
}
} //fin try interior
catch(Exception e){
ta.setText(mensa[id][6]); //error lectura fichero claves
}
br.close();
is.close();
}
catch(Exception e) {
ta.setText(mensa[id][8]); //error apertura fichero de claves
ta.append(sfile + " "+mensa[id][9]); //fichero no accesible
}
}
//.........................................
public void cargar() {
try{
URL pp = new URL(getCodeBase(), webpg );
getAppletContext().showDocument(pp,"f2");
}
catch (Exception e){ ta.setText(mensa[id][10]); }
}
//..................................................
public void start(){ repaint(); }
//..........................................
public static void main(String args[]) {
Apcontrol Nicontrol = new Apcontrol();
Nicontrol.init();
Nicontrol.start();
}
//.............................................
} //Fin de clase Apcontrol
//------------------------------------------------------------------------
class Apform extends Panel implements ActionListener{
Apcontrol padre;
int id = 0;
Button submit;
Button reset;
TextField user = new TextField();
TextField pasw = new TextField();
final static String sb[] = {"ENVIAR", "SUBMIT", "BORRAR", "RESET",
"ID Usuario", "User ID" };
public Apform(Apcontrol padre, Color FONDO, int id){ //Constructor
super();
this.padre = padre;
this.id = id;
Label L1 = new Label(sb[4+id]);
Label L2 = new Label("Password");
submit = new Button(sb[id]);
reset = new Button(sb[2+id]);
setLayout(new GridLayout(3,2,5,5));
setBackground(FONDO);
add(L1); //Etiqueta usuario
add(user); //casilla usuario
add(L2); //Etiqueta clave
pasw.setEchoChar('*'); //ocultar clave
add(pasw); //casilla clave
submit.addActionListener(this);
reset.addActionListener(this);
add(submit);
add(reset);
}
//.......................................................
public void actionPerformed(ActionEvent ev) {
String suser = null;
String spasw = null;
String accion = ev.getActionCommand();
if(accion.equals(sb[2+id])){
user.setText("");
pasw.setText("");
this.repaint();
}
else if( accion.equals(sb[id]) ){
suser = user.getText();
spasw = pasw.getText();
if(suser.length() != 0 & spasw.length() != 0){
submit.setEnabled(false);
padre.procesar(suser,spasw);
}
else{ padre.ta.setText(padre.mensa[id][11]); }
}
}
} //Fin Clase Apform
//-------------------------------------------------------------------------
class Tapiz extends Panel { //panel de fondo para el applet
int an = 260; int al =200;
static Color cuno = new Color(210,210,210); //borde uno
static Color cdos = new Color(100,100,100); //borde dos
static Color ctre = new Color(220,100,10); //fondo tapiz
public Tapiz(int ancho, int alto, Color fondo) { //Constructor
super();
this.an = ancho ;
this.al = alto ;
this.ctre = fondo;
setLayout(null);
setBackground(cuno); //cuadro grande
repaint();
}
public void paint(Graphics g){
g.setColor(ctre);
g.fillRect(3,3, an-5, al-5);
g.setColor(cdos);
for(int i=0; i<3; i++){ //rebordes de 5 pixels de ancho
g.drawLine(an-i, i, an-i, al-i); //vert. dch.
g.drawLine(i, al-i, an-i, al-i); //hor. baja
}
}
} //fin de clase
//------------------------------------------------------------------------
//Fin c¢digo applet
//.........................*************************.............................
grz in anticipo per l'aiuto raga