Skip to content

Commit

Permalink
fix: user password
Browse files Browse the repository at this point in the history
  • Loading branch information
kastnerorz committed Nov 24, 2019
1 parent 3122959 commit 42a983c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
25 changes: 10 additions & 15 deletions src/main/java/cn/kastner/oj/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cn.kastner.oj.domain.security.Authority;
import cn.kastner.oj.domain.security.AuthorityName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.hash.Hashing;
import lombok.Data;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
Expand Down Expand Up @@ -163,28 +164,15 @@ public User(
String lastname,
String school,
List<Authority> authorities) {
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
this.username = username;
this.password = encoder.encode(password);
this.email = email;
this.firstname = firstname;
this.lastname = lastname;
this.name = lastname + firstname;
this.school = school;
this.authorities = authorities;
}

public User(
@NotNull @Size(min = 4, max = 50) String username,
@NotNull String password,
@NotNull String email,
String school,
List<Authority> authorities) {
this.username = username;
this.password = password;
this.email = email;
this.school = school;
this.authorities = authorities;
this.setPassword(password);
}

public User(
Expand All @@ -197,14 +185,15 @@ public User(
String school,
List<Authority> authorities) {
this.username = username;
this.password = password;
this.firstname = firstname;
this.lastname = lastname;
this.name = lastname + firstname;
this.studentNumber = studentNumber;
this.email = email;
this.school = school;
this.authorities = authorities;

this.setMd5Password(password);
}

public User() {
Expand Down Expand Up @@ -254,4 +243,10 @@ public void setPassword(String password) {
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
this.password = encoder.encode(password);
}

public void setMd5Password(String password) {
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
String md5Password = Hashing.md5().hashBytes(password.getBytes()).toString();
this.password = encoder.encode(md5Password);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,14 @@ public Workbook resetMembersPassword(String id) throws GroupException {
header.createCell(3).setCellValue("密码");
for (User user : userSet) {
String randomPassword = CommonUtil.generateStr(8);
user.setPassword(randomPassword);
user.setMd5Password(randomPassword);
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(user.getUsername());
row.createCell(1).setCellValue(user.getStudentNumber());
row.createCell(2).setCellValue(user.getName());
row.createCell(3).setCellValue(randomPassword);
}
userRepository.saveAll(userSet);
return workbook;
}

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/cn/kastner/oj/service/impl/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,10 @@ public PageDTO<JwtUser> generateUser(String groupId, Long quantity) throws Group

for (int i = 0; i < quantity; i++) {
String username = "g" + generateNum(group.getIdx().intValue()) + "#" + generateNum(i);
String password = encoder.encode(username);
String firstname = "临时";
String lastname = "用户";
String email = username + "@temp.com";
User user = new User(username, password, firstname, lastname, "", email, "临时大学", authorities);
User user = new User(username, username, firstname, lastname, "", email, "临时大学", authorities);
user.setTemporary(true);
userList.add(user);
}
Expand Down Expand Up @@ -335,7 +334,7 @@ public PageDTO<JwtUser> generateUser(String groupId, File excel)
}

String username = "g" + generateNum(group.getIdx().intValue()) + "#" + row.getCell(0).getStringCellValue();
String password = encoder.encode(row.getCell(2).getStringCellValue());
String password = row.getCell(2).getStringCellValue();
String studentNumber = row.getCell(0).getStringCellValue();
String firstname = row.getCell(1).getStringCellValue();
String email = username + "@acmoj.shu.edu.cn";
Expand Down

0 comments on commit 42a983c

Please sign in to comment.