...
 
Commits (7)
......@@ -5,7 +5,7 @@
<parent>
<artifactId>maven-document-upload-parent</artifactId>
<groupId>cn.yunmaozj.tools</groupId>
<version>1.0.7</version>
<version>1.0.8</version>
</parent>
<modelVersion>4.0.0</modelVersion>
......
package cn.yunmaozj.configuration;
import cn.yunmaozj.common.SystemFile;
import cn.yunmaozj.security.Developer;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.AsyncHandlerInterceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN;
/**
* 文档上传拦截器,这里会对判断是否是登录的用户
*
* @author zhongminghong
* @email zhongmh@yunmaozj.com
* @create 2019-11-21 10:40
*/
@Component
public class DocumentUploadInterceptor implements HandlerInterceptor {
private SystemFile systemFile;
public DocumentUploadInterceptor(SystemFile systemFile) {
this.systemFile = systemFile;
}
private final Logger logger = LoggerFactory.getLogger(DocumentUploadInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String user = request.getParameter("user");
String password = request.getParameter("password");
logger.info("DocumentUploadInterceptor.preHandle[{}] user[{}] password:[{}]", request.getRequestURL(), user, password);
if (StringUtils.isAnyEmpty(user, password)) {
response.sendError(SC_FORBIDDEN,"您需要输入正确的用户名和密码");
return false;
}
if (Developer.exits(systemFile, user)) {
Developer developer = new Developer(systemFile, user);
if (!password.equals(developer.getPassword())) {
response.sendError(403,"您需要输入正确的用户名和密码");
return false;
}
} else {
response.sendError(403,"您需要输入正确的用户名和密码");
return false;
}
return true;
}
}
......@@ -18,6 +18,8 @@ public class SiteServierMvcConfiguration implements WebMvcConfigurer {
private SystemFile systemFile;
@Autowired
private StaticResourcesHandlerInterceptor interceptor;
@Autowired
private DocumentUploadInterceptor documentUploadInterceptor;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
......@@ -34,5 +36,6 @@ public class SiteServierMvcConfiguration implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(interceptor).addPathPatterns("/**/**html");
registry.addInterceptor(documentUploadInterceptor).addPathPatterns("/site-upload");
}
}
......@@ -81,6 +81,19 @@ public class Developer {
return userFile;
}
/**
* 判断是否存在用户
*
* @return
*/
public static boolean existUser(SystemFile systemFile, String id) {
if (id.endsWith(".xml")) {
File userFile = new File(systemFile.getUserDir(), id);
return userFile.exists();
}
File userFile = new File(systemFile.getUserDir(), id + ".xml");
return userFile.exists();
}
public List<String> roles() {
return getChildNodeText(TAG_ROLES, TAG_ROLE);
......
......@@ -5,7 +5,7 @@
<parent>
<artifactId>maven-document-upload-parent</artifactId>
<groupId>cn.yunmaozj.tools</groupId>
<version>1.0.7</version>
<version>1.0.8</version>
</parent>
<modelVersion>4.0.0</modelVersion>
......@@ -96,7 +96,6 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.76.96</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
......
......@@ -11,6 +11,7 @@ import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.*;
import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
import org.codehaus.plexus.util.StringUtils;
import org.dom4j.Element;
......@@ -58,8 +59,10 @@ public class DocumentUploadMojo extends AbstractMojo {
private String siteServerUserName;
@Parameter(property = "siteServerPassword")
private String siteServerPassword;
@Parameter(property = "securityToken", defaultValue = "上传到服务器的token")
private String securityToken;
@Parameter(property = "uploadUser", required = true)
private String uploadUser;
@Parameter(property = "uploadPassword", required = true)
private String uploadPassword;
@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;
@Parameter(defaultValue = "${settings}", readonly = true, required = true)
......@@ -69,7 +72,7 @@ public class DocumentUploadMojo extends AbstractMojo {
public void execute() throws MojoExecutionException, MojoFailureException {
if (skip) {
getLog().info("skip site-upload");
getLog().info("skip document-upload");
return;
}
for (Directory directory : directories.getFileList()) {
......@@ -107,6 +110,8 @@ public class DocumentUploadMojo extends AbstractMojo {
MultipartBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", name, fileBody)
.addFormDataPart("user", uploadUser)
.addFormDataPart("password", uploadPassword)
.build();
HttpUrl.Builder httpUrlBuilder = new HttpUrl.Builder();
......@@ -124,7 +129,8 @@ public class DocumentUploadMojo extends AbstractMojo {
.post(body).build();
Response response = getClient().newCall(request).execute();
getLog().info("Uploading file:[" + outFile + "] to [" + request.url() + "]");
getLog().info(response.toString());
if (!response.isSuccessful())
getLog().info("Response Body[" + response.body().string() + "]");
}
/**
......@@ -235,6 +241,9 @@ public class DocumentUploadMojo extends AbstractMojo {
for (Developer developer : project.getDevelopers()) {
Element developerElement = new DefaultElement("developer");
developerElement.addAttribute("name", developer.getName());
if (StringUtils.isEmpty(developer.getEmail())) {
throw new NullPointerException(developer.getName() + "的邮箱为空");
}
developerElement.addAttribute("email", developer.getEmail());
developerElement.addAttribute("id", developer.getId());
Element roles = new DefaultElement("roles");
......
......@@ -3,10 +3,15 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.yunmaozj</groupId>
<artifactId>yunmaozj-parent</artifactId>
<version>1.0.0.13</version>
</parent>
<groupId>cn.yunmaozj.tools</groupId>
<artifactId>maven-document-upload-parent</artifactId>
<version>1.0.7</version>
<version>1.0.8</version>
<description>站点上传插件</description>
......@@ -69,7 +74,6 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.0-RELEASE</version>
</plugin>
</plugins>
</build>
......