일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- Java
- 리눅스
- spring integration
- spring data jpa
- python
- 자바네트워크
- 유닉스
- NIO
- android
- 웹 크롤링
- oracle
- jpa
- flet
- 시스템
- kotlin
- 파이썬
- 소프트웨어공학
- 역학
- 자바
- Database
- write by GPT-4
- chatGPT's answer
- 코틀린
- GPT-4's answer
- JVM
- 고전역학
- 자바암호
- write by chatGPT
- 데이터베이스
- 인프라
- Today
- Total
기억을 지배하는 기록
Java 네트워크 - 6 본문
9. URLConnection
① package acdpu.pki.applet; ② import javax.swing.JApplet; ③ import java.awt.Dimension; ④ import java.awt.Toolkit; ⑤ import java.awt.BorderLayout; ⑥ import javax.swing.JFrame; ⑦ import javax.swing.UIManager; ⑧ import javax.swing.JLabel; ⑨ import java.awt.Rectangle; ⑩ import javax.swing.JPanel; ⑪ import javax.swing.BorderFactory; ⑫ import javax.swing.JScrollPane; ⑬ import javax.swing.JTextPane; ⑭ import javax.swing.JTextField; ⑮ import javax.swing.JButton; 16 import javax.swing.JPasswordField; 17 import java.awt.event.ActionListener; 18 import java.awt.event.ActionEvent; 19 import java.net.*; 20 import java.io.*; 21 import java.security.*; 22 import java.applet.*; 23 24 public class createPKIApplet extends JApplet { 25 26 // 객체 생성 27 private JLabel topLabel = new JLabel(); // 상단 라벨 28 private JScrollPane messageScrollPane = new JScrollPane(); // 하단 메시지창 스크롤 29 private JTextPane messageTextPane = new JTextPane(); // 하단 메시지창 30 private JTextField userid = new JTextField(); // 사용자 아이디 입력 31 private JPasswordField passwd = new JPasswordField(); // 사용자 암호 입력 32 private JButton submit = new JButton(); // 전송 버튼 33 private JButton reset = new JButton(); // 리셋 34 private JButton create = new JButton(); 35 private JButton login = new JButton(); 36 37 private final String serverIp = "10.60.34.202"; 38 private final int serverPort = 8888; 39 40 public createPKIApplet() { 41 } 42 43 public void init() { 44 45 try { 46 jbInit(); 47 } catch(Exception e) { 48 e.printStackTrace(); 49 } 50 } 51 52 private void jbInit() throws Exception { 53 this.getContentPane().setLayout(null); 54 this.setSize(new Dimension(524, 397)); 55 56 // 상단 라벨 57 topLabel.setText("인증키생성"); 58 topLabel.setBounds(new Rectangle(0, 0, 525, 30)); 59 60 // 사용자 아이디 61 userid.setBounds(new Rectangle(15, 35, 280, 65)); 62 userid.setBorder(BorderFactory.createTitledBorder("UserId")); 63 64 // 사용자 암호 65 passwd.setBounds(new Rectangle(15, 115, 280, 60)); 66 passwd.setBorder(BorderFactory.createTitledBorder("PassWord")); 67 68 // 전송 버튼 69 submit.setText("전송"); 70 submit.setBounds(new Rectangle(85, 215, 125, 40)); 71 submit.addActionListener(new ActionListener() { 72 public void actionPerformed(ActionEvent e) { 73 submit_actionPerformed(e); 74 } 75 }); 76 77 // 리렛 버튼 78 reset.setText("재입력"); 79 reset.setBounds(new Rectangle(305, 215, 125, 40)); 80 reset.addActionListener(new createPKIApplet_reset_actionAdapter(this)); 81 create.setText("인증서생성"); 82 create.setBounds(new Rectangle(355, 40, 115, 40)); 83 create.setActionCommand("create"); 84 create.setEnabled(false); 85 login.setText("로그인"); 86 login.setBounds(new Rectangle(355, 95, 115, 40)); 87 login.setActionCommand("login"); 88 login.addActionListener(new ActionListener() { 89 public void actionPerformed(ActionEvent e) { 90 login_actionPerformed(e); 91 } 92 }); 93 94 // 스크롤 상자 95 messageScrollPane.setBounds(new Rectangle(10, 290, 505, 80)); 96 messageScrollPane.setBorder(BorderFactory.createTitledBorder("Message")); 97 messageScrollPane.setAutoscrolls(true); 98 99 // 메시지 창 100 messageTextPane.setText("인증 서버연결 준비중..."); 101 messageTextPane.setEditable(false); 102 messageScrollPane.getViewport().add(messageTextPane, null); 103 104 this.getContentPane().add(login, null); 105 this.getContentPane().add(create, null); 106 this.getContentPane().add(passwd, null); 107 this.getContentPane().add(reset, null); 108 this.getContentPane().add(submit, null); 109 this.getContentPane().add(userid, null); 110 this.getContentPane().add(messageScrollPane, null); 111 this.getContentPane().add(topLabel, null); 112 } 113 114 static { 115 try { 116 // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 117 // UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); 118 } catch(Exception e) { 119 } 120 } 121 122 void reset_actionPerformed(ActionEvent e) { 123 userid.setText(""); 124 passwd.setText(""); 125 userid.setEditable(true); 126 passwd.setEditable(true); 127 setMessage("입력 창이 초기화 되었습니다."); 128 } 129 130 private void setMessage(String message) { 131 messageTextPane.setText(messageTextPane.getText()+"\n"+message); 132 } 133 134 private void submit_actionPerformed(ActionEvent e) { 135 userid.setEditable(false); 136 passwd.setEditable(false); 137 String userEnterText = userid.getText(); 138 String passEnterText = new String(passwd.getPassword()); 139 setMessage("인증서버에 접속중입니다.... "); 140 141 try{ 142 URL url = new URL("http", 143 serverIp, 144 serverPort, 145 "/createpki?userid="+userEnterText+ 146 "&passwd="+passEnterText); 147 URLConnection urlcon = url.openConnection(); 148 String contentType = urlcon.getContentType(); 149 150 // 테스트 출력 151 //setMessage(contentType); 152 153 InputStream in = urlcon.getInputStream(); 154 155 if(contentType.equalsIgnoreCase("text/html;euc-kr")) { 156 // 인증서가 정상적으로 발급되지 않은 경우 실행 157 BufferedReader inre = new BufferedReader( 158 new InputStreamReader( 159 in)); 160 setMessage("ERROR) : 다음과 같은 에러가 발생하였습니다."); 161 String errorMessage = inre.readLine(); 162 163 if(errorMessage.equals("1")) 164 setMessage("인증성 생성에 실패 하였습니다."); 165 else if(errorMessage.equals("2")) 166 setMessage("사용자가 틀림니다. 아이디와 비밀번호를 확인해 주십시요."); 167 else if(errorMessage.equals("3")) 168 setMessage("사용기간이 지났거나 시작되지 않았습니다."); 169 else 170 setMessage("인증서버 접속에 실패하였습니다. 관리자에 문의하십시요."); 171 in.close(); 172 173 } else { 174 // 인증서 클라이언트 PC에 저장 175 String filePath = "C:/peace"; 176 File file = new File(filePath); 177 if(!file.exists()){ 178 file.mkdir(); 179 } 180 ObjectInputStream obin = new ObjectInputStream(in); 181 PrivateKey privatekey = (PrivateKey)obin.readObject(); 182 ObjectOutputStream out = new ObjectOutputStream( 183 new FileOutputStream( 184 new File( 185 file, 186 userEnterText+"PRI.ser"))); 187 out.writeObject(privatekey); 188 obin.close(); 189 in.close(); 190 out.close(); 191 setMessage("인증키 발급이 성공되었습니다. 다음과 위치에 인증키가 저장되었습니다."); 192 setMessage(filePath+"/"+userEnterText+"PRI.ser"); 193 } 194 } catch (MalformedURLException mfurle) { 195 setMessage(mfurle.getMessage()); 196 } catch (IOException ioe) { 197 setMessage(ioe.getMessage()); 198 } catch (ClassNotFoundException cnote) { 199 setMessage(cnote.getMessage()); 200 } 201 } 202 203 private void login_actionPerformed(ActionEvent e) { 204 try{ 205 AppletContext cont = getAppletContext(); 206 cont.showDocument( 207 new URL("http://"+serverIp+":"+serverPort+"/applet/loginApplet.jsp")); 208 } catch (MalformedURLException mfurle) { 209 setMessage(mfurle.getMessage()); 210 } 211 } 212 } 213 class createPKIApplet_reset_actionAdapter implements ActionListener { 214 createPKIApplet adaptee; 215 216 createPKIApplet_reset_actionAdapter(createPKIApplet adaptee) { 217 this.adaptee = adaptee; 218 } 219 220 public void actionPerformed(ActionEvent e) { 221 adaptee.reset_actionPerformed(e); 222 } 223 } |
① package acdpu.pki.applet; ② import javax.swing.JApplet; ③ import java.awt.Dimension; ④ import java.awt.Toolkit; ⑤ import java.awt.BorderLayout; ⑥ import javax.swing.JFrame; ⑦ import javax.swing.UIManager; ⑧ import javax.swing.JLabel; ⑨ import java.awt.Rectangle; ⑩ import javax.swing.JPanel; ⑪ import javax.swing.BorderFactory; ⑫ import javax.swing.JScrollPane; ⑬ import javax.swing.JTextPane; ⑭ import javax.swing.JTextField; ⑮ import javax.swing.JButton; 16 import javax.swing.JPasswordField; 17 import java.awt.event.ActionListener; 18 import java.awt.event.ActionEvent; 19 import java.net.*; 20 import java.io.*; 21 import java.security.*; 22 import kr.co.itnext.crypto.*; 23 import java.applet.*; 24 25 public class loginPKIApplet extends JApplet { 26 27 // 객체 생성 28 private JLabel topLabel = new JLabel(); // 상단 라벨 29 private JScrollPane messageScrollPane = new JScrollPane(); // 하단 메시지창 스크롤 30 private JTextPane messageTextPane = new JTextPane(); // 하단 메시지창 31 private JTextField userid = new JTextField(); // 사용자 아이디 입력 32 private JPasswordField passwd = new JPasswordField(); // 사용자 암호 입력 33 private JButton submit = new JButton(); // 전송 버튼 34 private JButton reset = new JButton(); // 리셋 35 private JButton create = new JButton(); 36 private JButton login = new JButton(); 37 38 private final String serverIp = "10.60.34.202"; 39 private final int serverPort = 8888; 40 41 public loginPKIApplet() { 42 } 43 44 public void init() { 45 46 try { 47 jbInit(); 48 } catch(Exception e) { 49 e.printStackTrace(); 50 } 51 } 52 53 private void jbInit() throws Exception { 54 this.getContentPane().setLayout(null); 55 this.setSize(new Dimension(524, 397)); 56 57 // 상단 라벨 58 topLabel.setText("PKI 로그인"); 59 topLabel.setBounds(new Rectangle(0, 0, 525, 30)); 60 61 // 사용자 아이디 62 userid.setBounds(new Rectangle(15, 35, 280, 65)); 63 userid.setBorder(BorderFactory.createTitledBorder("UserId")); 64 65 // 사용자 암호 66 passwd.setBounds(new Rectangle(15, 115, 280, 60)); 67 passwd.setBorder(BorderFactory.createTitledBorder("PassWord")); 68 69 // 전송 버튼 70 submit.setText("로그인"); 71 submit.setBounds(new Rectangle(85, 215, 125, 40)); 72 submit.addActionListener(new ActionListener() { 73 public void actionPerformed(ActionEvent e) { 74 submit_actionPerformed(e); 75 } 76 }); 77 78 // 리렛 버튼 79 reset.setText("재입력"); 80 reset.setBounds(new Rectangle(305, 215, 125, 40)); 81 reset.addActionListener(new loginPKIApplet_reset_actionAdapter(this)); 82 create.setText("인증서생성"); 83 create.setBounds(new Rectangle(355, 40, 115, 40)); 84 create.setActionCommand("create"); 85 create.addActionListener(new ActionListener() { 86 public void actionPerformed(ActionEvent e) { 87 create_actionPerformed(e); 88 } 89 }); 90 login.setText("로그인"); 91 login.setBounds(new Rectangle(355, 95, 115, 40)); 92 login.setActionCommand("login"); 93 login.setEnabled(false); 94 95 // 스크롤 상자 96 messageScrollPane.setBounds(new Rectangle(10, 290, 505, 80)); 97 messageScrollPane.setBorder(BorderFactory.createTitledBorder("Message")); 98 messageScrollPane.setAutoscrolls(true); 99 100 // 메시지 창 101 messageTextPane.setText("사용자 아이디와 비밀번호를 입력하여 주십시요..."); 102 messageTextPane.setEditable(false); 103 messageScrollPane.getViewport().add(messageTextPane, null); 104 this.getContentPane().add(login, null); 105 106 this.getContentPane().add(create, null); 107 this.getContentPane().add(passwd, null); 108 this.getContentPane().add(reset, null); 109 this.getContentPane().add(submit, null); 110 this.getContentPane().add(userid, null); 111 this.getContentPane().add(messageScrollPane, null); 112 this.getContentPane().add(topLabel, null); 113 } 114 115 static { 116 try { 117 // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 118 // UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); 119 } catch(Exception e) { 120 } 121 } 122 123 void reset_actionPerformed(ActionEvent e) { 124 userid.setText(""); 125 passwd.setText(""); 126 userid.setEditable(true); 127 passwd.setEditable(true); 128 setMessage("입력 창이 초기화 되었습니다."); 129 } 130 131 private void setMessage(String message) { 132 messageTextPane.setText(messageTextPane.getText()+"\n"+message); 133 } 134 135 private void submit_actionPerformed(ActionEvent e) { 136 userid.setEditable(false); 137 passwd.setEditable(false); 138 String userEnterText = userid.getText(); 139 String passEnterText = new String(passwd.getPassword()); 140 setMessage("인증서버에 접속중입니다.... "); 141 142 try{ 143 URL url = new URL("http",serverIp,serverPort,"/loginpki"); 144 URLConnection urlcon = url.openConnection(); 145 urlcon.setDoInput(true); 146 urlcon.setDoOutput(true); 147 urlcon.setUseCaches(false); 148 149 String privateKeyFile = "C:/peace/"+userEnterText+"PRI.ser"; 150 File file = new File(privateKeyFile); 151 byte[] signningrow = null; 152 if(!file.exists()){ 153 setMessage("인증서가 존재하지 않습니다."); 154 //System.exit(0); 155 } else { 156 setMessage("sign 데이터 생성중입니다. "); 157 try { 158 Signature signning = Signature.getInstance("DSA"); 159 ObjectInputStream keyin = new ObjectInputStream(new FileInputStream(file)); 160 PrivateKey privatekey = (PrivateKey)keyin.readObject(); 161 keyin.close(); 162 signning.initSign(privatekey); 163 signning.update((userEnterText+passEnterText).getBytes()); 164 signningrow = signning.sign(); 165 BASE64Encoder encoder = new BASE64Encoder(); 166 String base64Signning = encoder.encode(signningrow); 167 urlcon.setRequestProperty("userid",userEnterText); 168 urlcon.setRequestProperty("passwd",passEnterText); 169 urlcon.setRequestProperty("signningdata",base64Signning); 170 urlcon.connect(); 171 172 setMessage("sign 데이터를 전송중입니다.... "); 173 } catch(NoSuchAlgorithmException noalgoe) { 174 setMessage("signning 알고리즘이 없습니다."); 175 //System.exit(0); 176 } catch(ClassNotFoundException cnotfe) { 177 setMessage("PC에 저장된 개인키를 생성할수 없습니다."); 178 //System.exit(0); 179 } catch(InvalidKeyException inkeye) { 180 setMessage("부적합한 개인키를 소유하고 있습니다."); 181 //System.exit(0); 182 } catch(SignatureException signe) { 183 setMessage("개인키 서명중 에러가 발생하였습니다."); 184 //System.exit(0); 185 } 186 } 187 188 BufferedReader inre = 189 new BufferedReader(new InputStreamReader(urlcon.getInputStream())); 190 String receiveMessage = inre.readLine(); 191 192 193 if(receiveMessage.equals("0")) { 194 setMessage("로그인 페이지로 이동합니다."); 195 AppletContext cont = getAppletContext(); 196 cont.showDocument(new URL( 197 "http://"+serverIp+":"+serverPort+"/login.jsp?f_userid="+userEnterText+ 198 "&f_passwd="+passEnterText)); 199 200 } else if(receiveMessage.equals("1")) { 201 setMessage("인증서가 구형이거나 정상적인 인증서가 아님니다."); 202 } else if(receiveMessage.equals("2")) { 203 setMessage("사용자가 틀림니다. 아이디와 비밀번호를 확인해 주십시요."); 204 } else if(receiveMessage.equals("3")) { 205 setMessage("사용기간이 지났거나 시작되지 않았습니다."); 206 } else { 207 setMessage("인증서버 접속에 실패하였습니다. 관리자에 문의하십시요."); 208 } 209 inre.close(); 210 211 } catch (MalformedURLException mfurle) { 212 setMessage(mfurle.getMessage()); 213 } catch (IOException ioe) { 214 setMessage(ioe.getMessage()); 215 } 216 } 217 218 private void create_actionPerformed(ActionEvent e) { 219 try{ 220 AppletContext cont = getAppletContext(); 221 cont.showDocument(new URL( 222 "http://"+serverIp+":"+serverPort+ 223 "/applet/createApplet.jsp")); 224 } catch (MalformedURLException mfurle) { 225 setMessage(mfurle.getMessage()); 226 } 227 228 } 229 } 230 class loginPKIApplet_reset_actionAdapter implements ActionListener { 231 loginPKIApplet adaptee; 232 233 loginPKIApplet_reset_actionAdapter(loginPKIApplet adaptee) { 234 this.adaptee = adaptee; 235 } 236 237 public void actionPerformed(ActionEvent e) { 238 adaptee.reset_actionPerformed(e); 239 } 240 } |
'오래된글 > Java' 카테고리의 다른 글
Java performance tips-2 (0) | 2018.04.09 |
---|---|
Java performance tips-1 (0) | 2018.04.09 |
Java 네트워크 - 5 (0) | 2018.04.09 |
Java 네트워크 - 4 (0) | 2018.04.09 |
Java 네트워크 - 3 (0) | 2018.04.09 |