Skip to content

Commit

Permalink
若无法申请,可自行添加证书和密钥文件
Browse files Browse the repository at this point in the history
  • Loading branch information
potongren committed Jan 4, 2024
1 parent bbad9d8 commit b883ea4
Show file tree
Hide file tree
Showing 4 changed files with 273 additions and 23 deletions.
65 changes: 61 additions & 4 deletions src/main/java/com/podcast/Servlet/UserServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
import com.podcast.Type.Type;
import com.podcast.Utils.TimeFormat;
import com.podcast.loader.PluginLoader;
import com.podcast.pojo.ChannelDataShow;
import com.podcast.pojo.ChannelDate;
import com.podcast.pojo.Download;
import com.podcast.pojo.PodcastUser;
import com.podcast.pojo.*;
import com.podcast.service.ChannelService;
import com.podcast.service.PodcastUserService;
import com.podcast.update.Update;
import com.podcast.update.UpdateInit;
import org.apache.commons.io.FileUtils;
import io.github.yajuhua.podcast2API.*;
import org.slf4j.Logger;
Expand Down Expand Up @@ -47,8 +45,67 @@ public class UserServlet extends BaseServlet{
public static List<String> CREATE_UUID = new ArrayList<>();//首次更新的UUID
public static Map<String,Update> FIRST_UPDATE = new HashMap<>();
private static final Logger LOGGER = LoggerFactory.getLogger("UserServlet");
private static final String CERT_PATH="/opt/tomcat/tomcat8/cert/";//证书和密钥存放位置
private Gson gson = new Gson();

/**
* 上传CA证书
* 证书文件 podcast2.crt
* 密钥文件 podcast2.key
* @param request
* @param response
* @throws Exception
*/
public void uploadCertServlet(HttpServletRequest request, HttpServletResponse response) throws Exception {
//上传CA证书
LOGGER.debug("certPath:"+CERT_PATH);
SystemServlet.upload(request,response,CERT_PATH);
}

/**
* 删除CA认证
* @param request
* @param response
* @throws Exception
*/
public void deleteCertServlet(HttpServletRequest request, HttpServletResponse response) throws Exception {
File[] certList = new File(CERT_PATH).listFiles();
String fileName = request.getParameter("fileName");
System.out.println("fileName" + fileName);
for (File file : certList) {
if (file.getName().equals(fileName)){
boolean flag = FileUtils.deleteQuietly(file);
response.getWriter().write(flag==true?"deleteCertOK":"deleteCertErr");
break;
}
}
}

/**
* 删除所有CA认证
* @param request
* @param response
* @throws Exception
*/
public void deleteAllCertServlet(HttpServletRequest request, HttpServletResponse response) throws Exception {
File[] certList = new File(CERT_PATH).listFiles();
for (File cert : certList) {
FileUtils.deleteQuietly(cert);
}
//删除成功!
response.getWriter().write("deleteAllCertOK");
}

public void getAllCertInfoServlet(HttpServletRequest request, HttpServletResponse response) throws Exception {
File[] certList = new File(CERT_PATH).listFiles();
List<CertFile> certName = new ArrayList<>();
for (File cert : certList) {
certName.add(new CertFile(cert.getName()));
}
//删除成功!
response.getWriter().write(gson.toJson(certName));
}

/**
* 申请CA证书
* @param request
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/podcast/pojo/CertFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.podcast.pojo;

public class CertFile {
private String fileName;

public CertFile(String fileName) {
this.fileName = fileName;
}

public String getFileName() {
return fileName;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

@Override
public String toString() {
return "CertFile{" +
"fileName='" + fileName + '\'' +
'}';
}
}
163 changes: 147 additions & 16 deletions src/main/webapp/js/manage.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ new Vue({
{ key: "代号", value: "" }
],
fileList: [],
certFileList: [],
multipleSelection: [],
preparingForUpdates:false,
downloadData:[],
Expand All @@ -31,7 +32,9 @@ new Vue({
importDataDialog:false,
customDomainNameData:null,
certificatesDomain:null,
certloading: false
certloading: false,
multipleCertFile: [],
certFileData: []
};
}, mounted: function () {
// 页面加载完成后,发送异步请求,查询数据
Expand All @@ -42,6 +45,9 @@ new Vue({
//获取频道所有信息
this.getAllChannelData();

//获取CA认证信息
this.getAllCertInfo();

//获取下载器信息
this.getDownloaderInfo();

Expand Down Expand Up @@ -324,9 +330,15 @@ new Vue({
handleRemove(file, fileList) {
console.log(file, fileList);
},
certHandleRemove(file, fileList) {
console.log(file, fileList);
},
handlePreview(file) {
console.log(file);
},
certHandlePreview(file) {
console.log(file);
},
handleResponse(response, file, fileList) {
if (response == "uploadok") {
this.$message({
Expand All @@ -339,6 +351,18 @@ new Vue({
this.$message.error('添加失败!');
}
},
certHandleResponse(response, file, fileList) {
if (response == "uploadok") {
this.$message({
message: '导入成功!',
type: 'success'
});
this.certFileList=[];//清空
this.getAllCertInfo();//重新获取证书信息
} else {
this.$message.error('导入失败!');
}
},
handleRemove2(file, fileList) {
console.log(file, fileList);
},
Expand Down Expand Up @@ -488,6 +512,11 @@ new Vue({
this.multipleDoneSelection = val;
console.log(this.multipleDoneSelection);
},
//选择删除要的下载记录
certFileHandleSelectionChange(val) {
this.multipleCertFile = val;
console.log(this.multipleCertFile);
},
//批量删除下载完成记录
deleteDownloadRecords(){

Expand Down Expand Up @@ -651,37 +680,139 @@ new Vue({
//申请CA证书
certificates(){
_this=this;
if (this.certificatesDomain==null){
//确认删除提示框
this.$confirm('即将开始申请,预计2分钟左右,是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
if (this.certificatesDomain==null){

this.$message.error('请输入你的域名!');
this.$message.error('请输入你的域名!');

} else if (window.location.protocol == 'https:'){
} else if (window.location.protocol == 'https:'){
this.$message({
message: '目前已经开启https!',
type: 'success'
});
}else {
_this.certloading = true
//发送到服务端
axios({
method:"post",
url:"./user/certificateServlet",
data:"domain="+_this.certificatesDomain
}).then(function (resp) {
_this.certloading = false;
if (resp.data == "0"){
//提示成功
_this.$message({
message: '申请CA证书成功!请重启容器,并使用https端口访问。',
type: 'success'
});
}else {
//失败
_this.$message.error('申请CA证书失败!请检测域名是否正确或容器是否开放80端口');
}
})
}
}).catch(() => {
this.$message({
message: '目前已经开启https!',
type: 'success'
type: 'info',
message: '已取消申请'
});
}else {
_this.certloading = true
//发送到服务端
});

},
//上传证书到服务器
uploadCert(){
var rs = this.$refs.uploadCert.submit().data;
alert(rs)
},
certBeforeUpload(file){
// 获取文件名
const fileName = file.name;

// 校验文件名是否符合要求
if (fileName !== 'podcast2.key' && fileName !== 'podcast2.crt') {
this.$message.error('文件名错误!必须以podcast2.key和podcast2.cert命名');
return false; // 中止上传
}
// 继续上传
return true;
},
getAllCertInfo(){
_this=this;
axios({
method:"get",
url:"./user/getAllCertInfoServlet",
}).then(function (resp) {
_this.certFileData=resp.data;
})
},
//删除证书
deleteCert(fileName){
_this=this;

//确认删除提示框
this.$confirm('此操作将永久删除'+ fileName +'文件, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
axios({
method:"post",
url:"./user/certificateServlet",
data:"domain="+_this.certificatesDomain
url:"./user/deleteCertServlet",
data:"fileName="+fileName
}).then(function (resp) {
_this.certloading = false;
if (resp.data == "0"){
if (resp.data=='deleteCertOK'){
_this.getAllCertInfo();//重新获取证书信息
//提示成功
_this.$message({
message: '申请CA证书成功!请重启容器,并使用https端口访问。',
message: '删除 '+ fileName +' 成功!',
type: 'success'
});
}else {
//失败
_this.$message.error('申请CA证书失败!请检测域名是否正确或容器是否开放80端口');
_this.$message.error('删除 '+ fileName +' 失败!');
}
})
}
}).catch(() => {
this.$message({
type: 'info',
message: '已取消删除'
});
});
},
//批量删除证书
multipleDeleteCert(){
_this=this;

//确认删除提示框
this.$confirm('此操作将永久删除批量所选文件, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
for (let i = 0; i < _this.multipleCertFile.length; i++) {
axios({
method:"post",
url:"./user/deleteCertServlet",
data:"fileName="+_this.multipleCertFile[i].fileName
})
}
_this.getAllCertInfo();//重新获取证书信息
//提示成功
_this.$message({
message: '删除成功!',
type: 'success'
});
}).catch(() => {
this.$message({
type: 'info',
message: '已取消删除'
});
});
}
},
});
44 changes: 41 additions & 3 deletions src/main/webapp/manage.html
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,51 @@
<!-- 开启https -->
<div v-show="activeMenu === 'certificates'"><el-form :inline="true" :model="certificatesDomain"
class="demo-form-inline">
<el-form-item label="开启https">
<el-form-item label="申请CA证书">
<el-input v-model="certificatesDomain" placeholder="如www.domain.org"></el-input>
</el-form-item>

<el-button type="primary" @click="certificates">申请</el-button>
<el-button type="primary" @click="certificates" >申请</el-button>
<br>
</el-form-item>
</el-form></div>


<el-upload class="upload-demo" ref="uploadCert" action="./user/uploadCertServlet" :on-preview="certHandlePreview"
:on-remove="certHandleRemove" :file-list="certFileList" :auto-upload="false"
:on-success="certHandleResponse"
:before-upload="certBeforeUpload">
<el-button slot="trigger" size="small" type="primary">手动导入</el-button>
<el-button style="margin-left: 10px;" size="small" type="success"
@click="uploadCert">上传到服务器</el-button>
</el-upload>
<!-- 显示证书和密钥 -->
<el-table ref="multipleCertFile" :data="certFileData" tooltip-effect="dark" style="width: 100%"
@selection-change="certFileHandleSelectionChange">
<el-table-column type="selection" width="55">
</el-table-column>
<el-table-column type="index" width="50">
</el-table-column>
<el-table-column label="证书和密钥文件" width="120" prop="fileName">
</el-table-column>
<el-table-column
label="类型">
<el-tag :type="success" slot-scope="scope">{{scope.row.fileName=="podcast2.crt"?"证书":"密钥"}}</el-tag>
</el-table-column>

<!-- 相关操作 -->
<el-table-column label="操作">
<template slot-scope="scope">
<el-button size="mini" type="danger" plain @click="deleteCert(scope.row.fileName)">删除</el-button>
</template>
</el-table-column>

</el-table>

</el-form>
<div class="footer">
<el-button type="danger" size="small" round v-if="multipleCertFile.length > 1" @click="multipleDeleteCert">批量删除</el-button>
</div>
</div>


<!--数据迁移-->
Expand Down

0 comments on commit b883ea4

Please sign in to comment.