Commit b7a68604 by zhongmh

增加开发人员信息保存功能

parent 34ff611e
......@@ -6,7 +6,7 @@
<groupId>cn.yunmaozj.tools</groupId>
<artifactId>maven-site-upload-parent</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
<description>站点上传插件</description>
<modules>
<module>site-upload-maven-plugin</module>
......
......@@ -23,14 +23,20 @@
<artifactId>commons-compress</artifactId>
<version>1.19</version>
</dependency>
<!--<dependency>
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>-->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
......
......@@ -2,6 +2,7 @@ package cn.yunmaozj;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
/**
......@@ -9,6 +10,7 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
* @email yunmaozj@163.com
* @create 2019-09-10 11:26
*/
@EnableWebSecurity
@EnableWebMvc
@SpringBootApplication
public class SiteServer {
......
......@@ -28,6 +28,8 @@ public class SystemFile {
private String applicationName = ".site-server";
private final File workDir;
private final File wwwDir;
private final File userDir;
private final File configDir;
private final File securityDir;
public SystemFile() {
......@@ -45,6 +47,14 @@ public class SystemFile {
if (!securityDir.exists() || !securityDir.isDirectory()) {
securityDir.mkdirs();
}
userDir = new File(securityDir, "users");
if (!userDir.exists() || userDir.isDirectory()) {
userDir.mkdirs();
}
configDir = new File(securityDir, "config");
if (!configDir.exists() || configDir.isDirectory()) {
configDir.mkdirs();
}
}
......@@ -56,6 +66,14 @@ public class SystemFile {
return wwwDir;
}
public File getUserDir() {
return userDir;
}
public File getConfigDir() {
return configDir;
}
public File getSecurityDir() {
return securityDir;
}
......@@ -108,6 +126,5 @@ public class SystemFile {
outputStream.close();
}
zipFile.close();
}
}
package cn.yunmaozj.configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
/**
* @author zhongminghong
* @email yunmaozj@163.com
* @create 2019-09-17 17:14
*/
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private final Logger logger = LoggerFactory.getLogger(WebSecurityConfig.class);
@Bean
public UserDetailsService userDetailsService(){
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
manager.createUser(User.builder().username("user").password("123456").roles("root").build());
return manager;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated().and().formLogin().and().httpBasic();
}
}
package cn.yunmaozj.security;
import cn.yunmaozj.common.SystemFile;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.dom.DOMElement;
import org.dom4j.dom.DOMText;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.tree.DefaultDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @author zhongminghong
* @email yunmaozj@163.com
* @create 2019-09-18 8:52
*/
public class Developer {
private final Logger logger = LoggerFactory.getLogger(Developer.class);
private final SAXReader reader = new SAXReader();
private String id;
private String name;
private SystemFile systemFile;
private SAXReader saxReader = new SAXReader();
private final File userFile;
private final static String DEVELOPER = "developer";
private final static String NAME = "name";
private final static String CREATE_DATE = "create-date";
private final static String UPDATE_DATE = "update-date";
private final static String LAST_LOGIN_IP = "last-login-ip";
private final static String LAST_LOGIN_DATE = "last-login-date";
private final static String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
private final static String TAG_ROLES = "roles";
private final static String TAG_ROLE = "role";
private final static String TAG_PROJECTS = "projects";
private final static String TAG_PROJECT = "project";
private final static String PASSWORD = "password";
private final static String DEFAULT_CHARSET = "UTF-8";
private final static OutputFormat outputFormat;
static {
outputFormat = OutputFormat.createPrettyPrint();
outputFormat.setEncoding(DEFAULT_CHARSET);
outputFormat.setIndent(true); //设置是否缩进
outputFormat.setIndent(" "); //以四个空格方式实现缩进
outputFormat.setNewlines(true); //设置是否换行
}
public Developer(SystemFile systemFile, String id, String name) throws IOException {
this.id = id;
this.name = name;
this.systemFile = systemFile;
userFile = getUserXml(id);
}
private File getUserXml(String id) throws IOException {
if (id.endsWith(".xml")) {
File userFile = new File(systemFile.getUserDir(), id);
if (!userFile.exists()) {
userFile.createNewFile();
initXml(userFile);
}
return userFile;
}
File userFile = new File(systemFile.getUserDir(), id + ".xml");
if (!userFile.exists()) {
userFile.createNewFile();
initXml(userFile);
}
return userFile;
}
public List<String> roles() {
return getChildNodeText(TAG_ROLES, TAG_ROLE);
}
public List<String> projects() {
return getChildNodeText(TAG_PROJECTS, TAG_PROJECT);
}
public List<String> getChildNodeText(String parentTag, String childTag) {
Document document = null;
try {
document = saxReader.read(userFile);
} catch (DocumentException e) {
e.printStackTrace();
return new ArrayList<>();
}
List<Element> elements = document.getRootElement().elements(TAG_ROLES);
if (elements.size() > 0) {
elements = elements.get(0).elements(TAG_ROLE);
} else {
return new ArrayList<>();
}
List<String> texts = new ArrayList<>(elements.size());
elements.stream().forEach(E -> texts.add(E.getText().trim()));
return texts;
}
/**
* 初始化关于用户信息的xml
*/
private void initXml(File userFile) throws IOException {
Document document = new DefaultDocument();
Element element = new DOMElement(DEVELOPER);
document.addComment("开发人员信息");
String dateString = DateFormatUtils.format(new Date(), DEFAULT_DATE_FORMAT);
element.addAttribute(NAME, name);
element.addAttribute(PASSWORD, PASSWORD);
element.addAttribute(CREATE_DATE, dateString);
element.addAttribute(UPDATE_DATE, dateString);
element.addAttribute(LAST_LOGIN_DATE, dateString);
element.addAttribute(LAST_LOGIN_IP, "127.0.0.1");
;
//初始化角色标签
element.addElement(TAG_ROLES);
element.addElement(TAG_PROJECTS);
document.add(element);
logger.info("write userInfo to {}", userFile);
XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(userFile), DEFAULT_CHARSET));
writer.write(document);
writer.close();
}
/**
* 增加项目
*
* @param project
*/
public void addProject(String project) {
addChildNode(TAG_PROJECTS, TAG_PROJECT, project);
}
public void addRole(String role) {
addChildNode(TAG_ROLES, TAG_ROLE, role);
}
public void addChildNode(String parentTag, String childTag, String text) {
try {
Document document = saxReader.read(userFile);
document.getRootElement().addAttribute(UPDATE_DATE, DateFormatUtils.format(new Date(), DEFAULT_DATE_FORMAT));
List<Element> elements = document.getRootElement().elements(parentTag);
Element element;
if (elements.size() > 0) {
element = elements.get(0);
} else {
element = new DOMElement(parentTag);
}
Element projectElement = new DOMElement(childTag);
projectElement.add(new DOMText(text));
element.add(projectElement);
write(document);
} catch (DocumentException e) {
e.printStackTrace();
}
}
/**
* 更新密码
*
* @param password
*/
public void setPassword(String password) {
try {
if (StringUtils.isEmpty(password)) {
throw new NullPointerException("password == null");
}
Document document = saxReader.read(userFile);
document.getRootElement().addAttribute(PASSWORD, password);
write(document);
} catch (DocumentException e) {
e.printStackTrace();
}
}
private synchronized void write(Document document) {
XMLWriter writer = null;
try {
writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(userFile), DEFAULT_CHARSET), outputFormat);
writer.write(document);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
close(writer);
}
}
private void close(XMLWriter writer) {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
spring.application.name=site-server
server.port=51420
logging.level.org.springframework=debug
\ No newline at end of file
server.port=51420
\ No newline at end of file
package cn.boliyuan;
import cn.yunmaozj.SiteServer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author zhongminghong
* @email yunmaozj@163.com
* @create 2019-09-17 17:39
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SiteServer.class)
public class ApplicationTest {
@Test
public void context(){
}
}
package cn.boliyuan.security;
import cn.boliyuan.ApplicationTest;
import cn.yunmaozj.common.SystemFile;
import cn.yunmaozj.security.Developer;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.IOException;
import java.util.Arrays;
/**
* @author zhongminghong
* @email yunmaozj@163.com
* @create 2019-09-18 9:07
*/
public class DeveloperTest extends ApplicationTest {
@Autowired
private SystemFile systemFile;
@Test
public void init() throws IOException {
Developer developer = new Developer(systemFile, "zhongmh", "钟明宏");
developer.setPassword("123456");
developer.addProject("boliyuan-web");
developer.addProject("boliyuan-common-util");
developer.addProject("boliyuan-web-util");
developer.addRole("admin");
developer.addRole("developer");
developer.addRole("master");
System.out.println(Arrays.toString(developer.projects().toArray()));
System.out.println(Arrays.toString(developer.roles().toArray()));
}
}
......@@ -5,7 +5,7 @@
<parent>
<artifactId>maven-site-upload-parent</artifactId>
<groupId>cn.yunmaozj.tools</groupId>
<version>1.0.1</version>
<version>1.0.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -14,7 +14,7 @@ import java.util.Set;
*/
public class Directories {
@Parameter(property = "directory", required = true)
@Parameter(property = "directories", required = true)
private Set<Directory> directories = new HashSet<>();
public Set<Directory> getFileList() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment