PKI證書籤發系統(web版)
阿新 • • 發佈:2019-01-13
這幾天沒事幹,學校安排小學期做一個pki證書籤發系統,班上的學霸美女一組,哈哈!雖然90%的活都是我做的,但是幫幫女生也是可以得嘛!扯遠了!看看效果吧!用的是ssh框架做的一個簽發證書網站,有普通使用者申請證書,然後管理員利用金鑰庫生成證書!提供使用者下載這個證書安裝!看看效果圖吧!不過對前臺不精通的我也只能做一個這樣的介面了,實驗室專業做前臺的給我改了改!看看效果吧!
效果差不多是這樣的。
看看程式碼怎麼實現的吧!首先是資料庫,利用mysql資料庫,
建立一個名字為pki的資料庫!如下圖是資料庫表的基本欄位
使用者表和證書基本資訊表
使用者表
證書表
下面就是證書實現的的重點方法了!由於程式碼較多,就跳幾個重要的貼上來吧!
首先是證書的action
package com.twj.action;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;import java.util.Map;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletResponse;import org.apache.struts2.ServletActionContext;import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.Result;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;import com.opensymphony.xwork2.ActionContext;import com.twj.Enum.CAState;import com.twj.base.BaseAction;import com.twj.entity.Cabook;import com.twj.entity.User;import com.twj.service.CABookService;import com.twj.service.UserService;@Scope("prototype")@Component@Action(value="CABookAction",results={ @Result(name="login", location="/login.jsp"), @Result(name="success" ,type="redirectAction" ,location="CABookAction!select.action"), @Result(name="select" ,location="/ptuserselectcabook.jsp"), @Result(name="adminselect" ,location="/adminselect.jsp"), @Result(name="cainfo" ,location="/cabookinfo.jsp"), @Result(name="getbookcar" ,type="redirectAction",location="CABookAction!adminQueryNoParams.action")})public class CABookAction extends BaseAction { /** * */ private static final long serialVersionUID = 1L; @Autowired private CABookService cABookService; @Autowired private UserService userService; private Cabook cabook; private String caCn; private String caOu; private String caO; private String caL; private String caSt; private String caC; private String caStorepass; private String caKeypass; private String caUrl; private Integer UId; private String caStart; private List<Cabook> list=new ArrayList<Cabook>(); private User user=new User(); //-------該寫證書的啦 //普通使用者申請證書 public String apply(){ user=(User) ActionContext.getContext().getSession().get("ptUser"); if (null==user) { System.out.println("--------------null---------"); return "login"; }else { cabook=new Cabook(); cabook.setCaC(caC); cabook.setCaCn(caCn); cabook.setUId(user.getUId()); cabook.setCaKeypass(caKeypass); cabook.setCaL(caL); cabook.setCaO(caO); cabook.setCaOu(caOu); cabook.setCaSt(caSt); cabook.setCaStart(CAState.NOPASS.getDiscribe()); cabook.setCaStorepass(caStorepass); Date d=new Date(); SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd"); String url=f.format(d); caUrl="d:/"+user.getUName()+url+".keystore"; cabook.setCaUrl(caUrl); cABookService.Save(cabook); genkey(); return "success"; } } //普通使用者預設查詢 public String selectdef(){ user=(User) ActionContext.getContext().getSession().get("ptUser"); list.clear(); list=cABookService.getBooKById(user.getUId()); return "select"; } private String caselecttype; //普通使用者按狀態查詢 public String select(){ user=(User) ActionContext.getContext().getSession().get("ptUser"); list.clear(); list=cABookService.getBookByUId(user.getUId(),caselecttype); return "select"; } private Integer downCaBookId; //---------------admin操作 private String adcaState; private List<Cabook>adcabooklist=new ArrayList<Cabook>(); //管理員條件查詢證書 public String adminquery(){ user=(User) ActionContext.getContext().getSession().get("admin"); if (null==user) { return "login"; }else { list.clear(); list= cABookService.getBookByStart(adcaState); return"adminselect"; } } //管理員預設查詢所有證書 public String adminQueryNoParams(){ user=(User) ActionContext.getContext().getSession().get("admin"); if (null==user) { return "login"; }else { list.clear(); list= cABookService.getBookByStart(); return"adminselect"; } } //管理員檢視證書詳細資訊 public String adminselectCaInfor(){ user=(User) ActionContext.getContext().getSession().get("admin"); if (null==user) { return "login"; }else { cabook= cABookService.getCaBookById(caBookId); return"cainfo"; } } private Integer caBookId; //管理員簽發證書 public String adminsetCAbook(){ user=(User) ActionContext.getContext().getSession().get("admin"); if (null==user) { return "login"; }else { Cabook cabook= cABookService.getCaBookById(caBookId); System.out.println("------------>>"+cabook.getCaCn()); export(cabook); return"getbookcar"; } } //管理員刪除證書 public String deleteca(){ Cabook cabook=cABookService.getCaBookById(caBookId); java.io.File file=new java.io.File(cabook.getCaUrl()); cABookService.delete(cabook); if (file.exists()) file.delete(); return "getbookcar"; } public String downloadFile(){ return SUCCESS; } //--------------證書下載 public String getDownloadFile() { Cabook car=cABookService.getCaBookById(downCaBookId); String inputPath=car.getCaUrl(); if(inputPath!=null&&!"".equals(inputPath)){ HttpServletResponse response = ServletActionContext.getResponse(); response.setHeader("content-disposition", "attachment;filename=certificate" +car.getCaC()+inputPath.substring(inputPath.indexOf("."),inputPath.length())); byte[] buf = new byte[1000]; FileInputStream fos = null; try { String file=car.getCaUrl(); fos = new FileInputStream(file); ServletOutputStream out = response.getOutputStream(); while (fos.read(buf) != -1) { out.write(buf); } response.flushBuffer(); out.close(); } catch (Exception e) { e.printStackTrace(); } finally { try { fos.close(); } catch (Exception f) { } } }else{ HttpServletResponse response = (HttpServletResponse) ActionContext .getContext().get( org.apache.struts2.StrutsStatics.HTTP_RESPONSE); try { String message="還沒有上傳檔案"; response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.write(message); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } //-------------------- /** * 生成金鑰 */ public void genkey() { String[] arstringCommand = new String[] { "cmd ", "/k", "start", // cmd Shell命令 "G:\\java\\bin\\keytool", "-genkey", // -genkey表示生成金鑰 "-validity", // -validity指定證書有效期(單位:天),這裡是36500天 "36500", "-keysize",// 指定金鑰長度 "1024", "-alias", // -alias指定別名,這裡是ss "ss", "-keyalg", // -keyalg 指定金鑰的演算法 (如 RSA DSA(如果不指定預設採用DSA)) "RSA", "-keystore", // -keystore指定儲存位置,這裡是d:/demo.keystore caUrl, "-dname",// CN=(名字與姓氏), OU=(組織單位名稱), O=(組織名稱), L=(城市或區域名稱), // ST=(州或省份名稱), C=(單位的兩字母國家程式碼)" "CN=("+caCn+"), OU=("+caOu+"), O=("+caO+"), L=("+caL+"),ST=("+caSt+"), C=("+caC+")", "-storepass", // 指定金鑰庫的密碼(獲取keystore資訊所需的密碼) "123456", "-keypass",// 指定別名條目的密碼(私鑰的密碼) caKeypass, "-v"// -v 顯示金鑰庫中的證書詳細資訊 }; execCommand(arstringCommand); } /** * 管理員 匯出證書檔案 */ public void export(Cabook cabook) { User user= userService.getUserById(cabook.getUId()); String url="d:/"+user.getUName()+cabook.getCaId()+".cer"; String[] arstringCommand = new String[] { "cmd ", "/k", "start", // cmd Shell命令 "G:\\java\\bin\\keytool", "-export", // - export指定為匯出操作 "-keystore", // -keystore指定keystore檔案,這裡是d:/demo.keystore cabook.getCaUrl(), "-alias", // -alias指定別名,這裡是ss "ss", "-file",//-file指向匯出路徑 "d:/"+user.getUName()+cabook.getCaId()+".cer", "-storepass",// 指定金鑰庫的密碼 "123456" }; execCommand(arstringCommand); cabook.setCaStart(CAState.PASS.getDiscribe()); cabook.setCaUrl(url); cABookService.updata(cabook); } public void execCommand(String[] arstringCommand) { for (int i = 0; i < arstringCommand.length; i++) { System.out.print(arstringCommand[i] + " "); } try { Runtime.getRuntime().exec(arstringCommand); } catch (Exception e) { System.out.println(e.getMessage()); } } public void execCommand(String arstringCommand) { try { Runtime.getRuntime().exec(arstringCommand); } catch (Exception e) { System.out.println(e.getMessage()); } } public String getCaselecttype() { return caselecttype; } public void setCaselecttype(String caselecttype) { this.caselecttype = caselecttype; } public List<Cabook> getAdcabooklist() { return adcabooklist; } public void setAdcabooklist(List<Cabook> adcabooklist) { this.adcabooklist = adcabooklist; } public String getAdcaState() { return adcaState; } public void setAdcaState(String adcaState) { this.adcaState = adcaState; } public String getCaCn() { return caCn; } public void setCaCn(String caCn) { this.caCn = caCn; } public String getCaOu() { return caOu; } public void setCaOu(String caOu) { this.caOu = caOu; } public String getCaO() { return caO; } public void setCaO(String caO) { this.caO = caO; } public String getCaL() { return caL; } public void setCaL(String caL) { this.caL = caL; } public String getCaSt() { return caSt; } public void setCaSt(String caSt) { this.caSt = caSt; } public String getCaC() { return caC; } public void setCaC(String caC) { this.caC = caC; } public Integer getCaBookId() { return caBookId; } public void setCaBookId(Integer caBookId) { this.caBookId = caBookId; } public String getCaStorepass() { return caStorepass; } public void setCaStorepass(String caStorepass) { this.caStorepass = caStorepass; } public String getCaKeypass() { return caKeypass; } public void setCaKeypass(String caKeypass) { this.caKeypass = caKeypass; } public String getCaUrl() { return caUrl; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public void setCaUrl(String caUrl) { this.caUrl = caUrl; } public Integer getUId() { return UId; } public void setUId(Integer uId) { UId = uId; } public String getCaStart() { return caStart; } public void setCaStart(String caStart) { this.caStart = caStart; } public List<Cabook> getList() { return list; } public void setList(List<Cabook> list) { this.list = list; } public Integer getDownCaBookId() { return downCaBookId; } public void setDownCaBookId(Integer downCaBookId) { this.downCaBookId = downCaBookId; } public Cabook getCabook() { return cabook; } public void setCabook(Cabook cabook) { this.cabook = cabook; }}
- 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
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
- 417
- 418
- 419
- 420
- 421
- 422
- 423
- 424
- 425
- 426
- 427
- 428
- 429
- 430
- 431
- 432
- 433
- 434
- 435
- 436
- 437
- 438
- 439
- 440
- 441
使用者action
package com.twj.action;import java.util.Map;import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.Result;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;import com.opensymphony.xwork2.ActionContext;import com.sun.net.httpserver.HttpContext;import com.twj.Enum.CAState;import com.twj.Enum.UserType;import com.twj.base.BaseAction;import com.twj.entity.User;import com.twj.service.UserService;@Scope("prototype")@Component@Action(value="UserAction",results={ @Result(name="login", location="/login.jsp"), @Result(name="success" ,type="redirectAction",location="CABookAction!selectdef.action"), @Result(name="admin" ,type="redirectAction",location="CABookAction!adminQueryNoParams.action")})public class UserAction extends BaseAction { /** * */ private static final long serialVersionUID = 1L; @Autowired private UserService userService; private User user; private String UName; private String UPsd; private String UType; private Map<String, Object> session; //註冊 public String register(){ if (UName==null&&UPsd==null&&UType==null) { return "register"; } else { user=new User(); user.setUName(UName); user.setUPsd(UPsd); user.setUType(UType); System.out.println("------------------"+UType+UserType.valueOf(1).getDiscribe()+"----------------"); userService.register(user); User logUser= userService.login(UName, UPsd); session=ActionContext.getContext().getSession(); if (UType.equals(UserType.valueOf(1).getDiscribe())) { session.put("ptUser",logUser); return "login"; }else { session.put("admin",logUser); return "login"; } } } //登陸 private User loginuser=new User(); public String login(){ if (UName==null&&UPsd==null) { return "login"; } else { loginuser=userService.login(UName, UPsd); if (null==loginuser) { return "login"; } session=ActionContext.getContext().getSession(); if (loginuser.getUType().equals(UserType.valueOf(1).getDiscribe())) { session.put("ptUser", loginuser); return "success"; } session.put("admin", loginuser); return "admin"; } } public User getLoginuser() { return loginuser; } public void setLoginuser(User loginuser) { this.loginuser = loginuser; } public String getUName() { return UName; } public void setUName(String uName) { UName = uName; } public String getUPsd() { return UPsd; } public void setUPsd(String uPsd) { UPsd = uPsd; } public String getUType() { return UType; } public void setUType(String uType) { UType = uType; }}
- 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
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
這就是核心程式碼吧!需要原始碼的小夥伴關注我留下郵箱!
再分享一下我老師大神的人工智慧教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智慧的隊伍中來!https://blog.csdn.net/jiangjunshow