PKI证书签发系统(web版)

                       

这几天没事干,学校安排小学期做一个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

猜你喜欢

转载自blog.csdn.net/qq_43746757/article/details/86303159