Conflicts:
	src/main/java/com/codesdream/ase/controller/LeavesController.java
This commit is contained in:
Vindicator645 2020-03-21 13:59:41 +08:00
commit 62ee044d13
21 changed files with 4673 additions and 39 deletions

View File

@ -144,6 +144,12 @@
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>

View File

@ -7,11 +7,11 @@ import org.springframework.stereotype.Component;
// SHA1算法不可逆加密 主要用于JSON签名
@Component
public class SHA1Encoder {
String encode(CharSequence charSequence){
public String encode(CharSequence charSequence){
return DigestUtils.sha1Hex(charSequence.toString());
}
boolean match(CharSequence charSequence, String s){
public boolean match (CharSequence charSequence, String s){
return s.equals(encode(charSequence));
}
}

View File

@ -0,0 +1,12 @@
package com.codesdream.ase.component.datamanager;
import lombok.Data;
// 储存字符串标识的文件并可以转换为json进行传输
@Data
public class StringFile {
private String strData = null;
private String sha1Checker = null;
private Integer size = null;
private String type = "none";
}

View File

@ -0,0 +1,92 @@
package com.codesdream.ase.component.datamanager;
import com.codesdream.ase.component.auth.SHA1Encoder;
import com.codesdream.ase.exception.StringFileConvertException;
import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.*;
import java.util.Base64;
import java.util.Optional;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
// 将文件处理成可发送的字符串文件对象
@Component
public class StringFileGenerator {
@Resource
private SHA1Encoder encoder;
// 用过读入流创建一个字符串文件
public Optional<StringFile> generateStringFile(InputStream stream){
StringFile file = new StringFile();
// 字符串内容计算
file.setStrData(generateFile2String(stream));
if(file.getStrData() == null) return Optional.empty();
// 相关校验值计算
file.setSha1Checker(generateSHA1Checker(file.getStrData()));
file.setSize(file.getStrData().length());
return Optional.of(file);
}
private byte[] readSteamAll(InputStream stream) {
try {
byte[] bytes = new byte[stream.available()];
//检查文件书否完全读取
if (stream.read(bytes) != bytes.length) return null;
else return bytes;
} catch (IOException e){
return null;
}
}
private String generateFile2String(InputStream stream){
ByteArrayOutputStream zipDataStream = new ByteArrayOutputStream();
try {
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(zipDataStream);
byte[] bytes = readSteamAll(stream);
if(bytes == null) return null;
gzipOutputStream.write(bytes);
gzipOutputStream.close();
return Base64.getEncoder().encodeToString(zipDataStream.toByteArray());
} catch (IOException e) {
return null;
}
}
// 生成字符串文件的校验码
private String generateSHA1Checker(String str){
return encoder.encode(str);
}
// 检查文件内容是否正确包括大小与校验码
public boolean checkStringFile(StringFile file){
return file.getStrData().length() == file.getSize()
&& encoder.match(file.getStrData(), file.getSha1Checker());
}
// 从字符串文件中读取真实的文件数据
public InputStream readFileString(StringFile file){
try {
// 字符串转换为二进制数据
byte[] bytes = Base64.getDecoder().decode(file.getStrData());
GZIPInputStream stream = new GZIPInputStream(new ByteArrayInputStream(bytes), bytes.length);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// 数据解压缩
int readBits = 0;
byte[] rawBytes = new byte[1024];
while ((readBits = stream.read(rawBytes)) != -1) {
outputStream.write(rawBytes, 0, readBits);
}
stream.close();
return new ByteArrayInputStream(outputStream.toByteArray());
} catch (IOException e) {
throw new StringFileConvertException("Read FileString Failed");
}
}
}

View File

@ -30,15 +30,9 @@ import java.util.Optional;
@Slf4j
public class ASEJSONTokenAuthenticationFilter extends OncePerRequestFilter {
@Resource
private JSONParameter jsonParameter;
@Resource
private JSONRandomCodeGenerator randomCodeGenerator;
@Resource
private AJAXRequestChecker ajaxRequestChecker;
@Resource
private AuthService authService;

View File

@ -3,8 +3,6 @@ package com.codesdream.ase.controller;
import com.alibaba.fastjson.JSONObject;
import com.codesdream.ase.component.datamanager.JSONParameter;
import com.codesdream.ase.component.json.request.UserLeaveRequest;
//import com.codesdream.ase.component.json.respond.FailedSONRespond;
import com.codesdream.ase.component.json.respond.JSONBaseRespondObject;
import com.codesdream.ase.component.permission.ASEUsernameEncoder;
import com.codesdream.ase.service.LeavesService;
import lombok.extern.slf4j.Slf4j;
@ -35,13 +33,13 @@ public class LeavesController {
@Resource
private ASEUsernameEncoder usernameEncoder;
@RequestMapping(value = "/")
@RequestMapping(value = "/leave")
String printLeave(Model model) {
return "Leave";
}
//提交请假申请
@RequestMapping(value = "/Leave/check", method = RequestMethod.POST)
@RequestMapping(value = "/leave/check", method = RequestMethod.POST)
@ResponseBody
String requestLeave(HttpServletRequest request){

View File

@ -37,8 +37,6 @@ public class LoginController {
@Resource
private IUserService userService;
@Resource
private ASEUsernameEncoder usernameEncoder;
@RequestMapping(value = "/login")
String printLogin(Model model) {
@ -92,8 +90,6 @@ public class LoginController {
// 返回失败对象
return quickJSONRespond.getRespond400("CheckType Mismatch");
}
}

View File

@ -78,7 +78,7 @@ public class RegisterController {
BaseStudentInfo studentInfo = baseInformationService.findStudentInfoByStudentId(student_id);
// 根据基本信息生成对应用户
User user = userService.getUserByStudentInfo(studentInfo);
User user = userService.createUserByStudentInfo(studentInfo);
// 填充密保问题
user.getUserAuth().setUserQuestion(user_question);

View File

@ -0,0 +1,12 @@
package com.codesdream.ase.exception;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
public class StringFileConvertException extends RuntimeException {
public StringFileConvertException(String msg){
super(msg);
}
}

View File

@ -12,7 +12,7 @@ import javax.persistence.*;
@Table(name = "base_major")
public class BaseMajor {
@Id
private int id;
private String id;
private String name;
}

View File

@ -4,9 +4,10 @@ import com.codesdream.ase.model.permission.User;
import lombok.Data;
import java.util.*;
import javax.persistence.*;
@Data
@Entity
@Table(name = "leave")
@Table(name = "leaves")
public class Leave {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@ -23,15 +24,15 @@ public class Leave {
//请假类型 病假事假等
@Column(name = "type", nullable = false)
@Column(nullable = false)
private String type;
//批准状态
@Column(name = "Authentication", nullable = false)
private String Authentication;
@Column(nullable = false)
private String authentication;
//审核备注
@Column
private String Comment;
private String comment;
//开始时间
@Column(nullable = false)
private Date startTime;

View File

@ -7,6 +7,6 @@ import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface BaseMajorRepository extends CrudRepository<BaseMajor, Integer> {
public interface BaseMajorRepository extends CrudRepository<BaseMajor, String> {
Optional<BaseMajor> findByName(String name);
}

View File

@ -7,6 +7,5 @@ import java.util.Optional;
public interface LeaveRepository extends CrudRepository<Leave, Integer>{
Optional<Leave> findByTitle(String title) ;
Optional<Leave> findByCreator(String creatorName);
}

View File

@ -16,7 +16,7 @@ public interface IUserService {
User getDefaultUser();
// 由学生基本信息生成对应用户
User getUserByStudentInfo(BaseStudentInfo studentInfo);
User createUserByStudentInfo(BaseStudentInfo studentInfo);
List<User> findAll();

View File

@ -97,14 +97,13 @@ public class UserService implements IUserService {
if(userRepository.findByUsername(user.getUsername()).isPresent())
throw new UsernameAlreadyExistException(user.getUsername());
// 用户信息一般性规范检查
// 用户关键信息一般性规范检查
if(user.getUserAuth().getUserAnswer() == null
|| user.getUserAuth().getUserQuestion() == null
|| user.getUserAuth().getStudentID() == null
|| user.getUserDetail().getRealName() == null
|| user.getUserAuth().getMail() == null){
throw new RuntimeException("Key Information IS NULL");
throw new RuntimeException("Some Key Information IS NULL");
}
@ -145,7 +144,7 @@ public class UserService implements IUserService {
}
@Override
public User getUserByStudentInfo(BaseStudentInfo studentInfo) {
public User createUserByStudentInfo(BaseStudentInfo studentInfo) {
User user = getDefaultUser();
// 根据学生id生成用户名
generateRandomUsernameByStudentID(user, studentInfo.getStudentId());

View File

@ -0,0 +1,24 @@
server.port=8080
spring.thymeleaf.prefix=classpath:templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
spring.jpa.generate-ddl=false
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jooq.sql-dialect=org.hibernate.dialect.MariaDB102Dialect
spring.jpa.open-in-view=true
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
spring.datasource.url=jdbc:h2:mem:test
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
server.error.whitelabel.enabled=false
logging.level.root=info
logging.level.org.springframework.security=info

View File

@ -21,5 +21,3 @@ server.error.whitelabel.enabled=false
logging.level.root=info
logging.level.org.springframework.security=info
server.servlet.session.timeout=30m

File diff suppressed because it is too large Load Diff

View File

@ -3,22 +3,28 @@ package com.codesdream.ase.test;
import com.codesdream.ase.component.ASESpringUtil;
import com.codesdream.ase.component.datamanager.*;
import com.codesdream.ase.repository.permission.UserRepository;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Optional;
/**
* 测试DataModel相关查找器
*/
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test")
public class DataManagerTest {
@Resource
ASESpringUtil springUtil;
@ -133,6 +139,7 @@ public class DataManagerTest {
Assert.assertEquals(iterator.next(), "16");
}
// Excel表格导入测试
@Test
public void dataTableImportTest(){
DataTable table = springUtil.getBean(DataTable.class);
@ -144,6 +151,7 @@ public class DataManagerTest {
Assert.assertEquals(iterator.next(), "18");
}
// Excel表格导出测试
@Test
public void dataTableExportTest(){
DataTable table = springUtil.getBean(DataTable.class);
@ -164,5 +172,32 @@ public class DataManagerTest {
table.exportTable(new DataExcelGenerator("DataTableExport.xlsx"));
}
// 字符串文件测试
@Test
public void File2StringTest() throws IOException {
FileInputStream stream = new FileInputStream("test.pdf");
StringFileGenerator generator = springUtil.getBean(StringFileGenerator.class);
Optional<StringFile> file = generator.generateStringFile(stream);
// 检查是否转换成功
Assert.assertTrue(file.isPresent());
// 检查字符串文件的校验功能
Assert.assertTrue(generator.checkStringFile(file.get()));
// 输出转化
FileOutputStream outputStream = new FileOutputStream("testOut.pdf");
InputStream inputStream = generator.readFileString(file.get());
// 输出文件
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.close();
}
}

View File

@ -10,9 +10,12 @@ import javafx.util.Pair;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import javax.swing.text.html.Option;
import java.util.Optional;
/**
* 用户基本表单元测试
@ -20,6 +23,7 @@ import javax.annotation.Resource;
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test")
public class UserTest {
@Resource
@ -30,12 +34,6 @@ public class UserTest {
*/
@Test
public void UserBaseTest_1(){
// 查找数据库中是否有重复项
Pair<Boolean, User> checker = userService.checkIfUserExists("Tim");
if(checker.getKey()){
userService.delete(checker.getValue());
}
User user = userService.getDefaultUser();
user.setUsername("Tim");
user.setPassword("123456");
@ -44,6 +42,7 @@ public class UserTest {
user.getUserAuth().setUserQuestion("Your favourite animal?");
user.getUserAuth().setUserAnswer("Cat");
user.getUserDetail().setAtSchool(true);
user.getUserDetail().setRealName("提姆");
userService.save(user);
user = userService.findUserByUsername("Tim");
@ -51,6 +50,7 @@ public class UserTest {
assertEquals(user.getUsername(), "Tim");
assertEquals(user.getPassword(),
"8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92");
// 检查账号状态
assertTrue(user.isEnabled());
assertFalse(user.isDeleted());
@ -65,7 +65,19 @@ public class UserTest {
@Test
public void UserBaseTest_2(){
User user = userService.findUserByUsername("Tim");
assertNotNull(user);
user.setEnabled(false);
user.getUserAuth().setMail("saturneric@163.com");
user.getUserDetail().setRealName("张三丰");
user = userService.update(user);
assertEquals(user.getUserAuth().getMail(), "saturneric@163.com");
assertEquals(user.getUserDetail().getRealName(), "张三丰");
assertFalse(user.isEnabled());
}
}