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;
	}
}

用户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;
	}

}

这就是核心代码吧!源码地址:https://github.com/twjitm/pki

好一个大布丁 CSDN认证博客专家 Java Redis 分布式
手游服务器研发工程师。有参与射击类型手游《雷霆战机》。MMO《末日危机》,SLG+ARPG《末日生存》开发经验。现在主要研究全球游戏服务器SLG类型游戏开发。研究分布式和微服务在游戏服务器中的应用。
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页