Skip to content

Commit

Permalink
新增密码找回,邮箱验证
Browse files Browse the repository at this point in the history
  • Loading branch information
Smith-Cruise committed Feb 23, 2018
1 parent 0c59579 commit 65f29ac
Show file tree
Hide file tree
Showing 20 changed files with 364 additions and 253 deletions.
215 changes: 6 additions & 209 deletions design/database.sql

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions eagle-oj-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
Expand Down
3 changes: 3 additions & 0 deletions eagle-oj-web/src/main/java/com/eagleoj/web/DefaultConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ public class DefaultConfig {

// ACM模式罚时 ms
public static final int ACM_PENALTY_TIME = 20;

// 验证码过期时间 min
public static final int CAPTCHA_EXPIRED_TIME = 5;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class CacheController {

private static Cache<Integer, Object> leaderboardCache;

private static Cache<String, String> settingCache;
private static Cache<String, String> captchaCache;

static {
CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build(true);
Expand Down Expand Up @@ -55,14 +55,14 @@ public class CacheController {
.withExpiry(Expirations.timeToLiveExpiration(Duration.of(DefaultConfig.LEADERBOARD_REFRESH_TIME, TimeUnit.MINUTES)))
.build());

settingCache = cacheManager
.createCache("settingCache",
captchaCache = cacheManager
.createCache("captchaCache",
CacheConfigurationBuilder.newCacheConfigurationBuilder(
String.class,
String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder().heap(10, MemoryUnit.MB))
ResourcePoolsBuilder.newResourcePoolsBuilder().heap(5, MemoryUnit.MB))
.withExpiry(Expirations.timeToLiveExpiration(Duration.of(DefaultConfig.CAPTCHA_EXPIRED_TIME, TimeUnit.MINUTES)))
.build()

);
}

Expand All @@ -76,7 +76,7 @@ public static Cache<Integer, Object> getLeaderboard() {
return leaderboardCache;
}

public static Cache<String, String> getSettingCache() {
return settingCache;
public static Cache<String, String> getCaptchaCache() {
return captchaCache;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import com.alibaba.fastjson.JSONArray;
import com.eagleoj.web.cache.CacheController;
import com.eagleoj.web.controller.exception.WebErrorException;
import com.eagleoj.web.controller.format.index.ForgetPasswordFormat;
import com.eagleoj.web.controller.format.index.IndexLoginFormat;
import com.eagleoj.web.controller.format.index.IndexRegisterFormat;
import com.eagleoj.web.controller.format.index.ResetPasswordFormat;
import com.eagleoj.web.entity.ResponseEntity;
import com.eagleoj.web.entity.UserEntity;
import com.eagleoj.web.mail.MailService;
import com.eagleoj.web.service.UserService;
import com.eagleoj.web.util.JWTUtil;
import io.swagger.annotations.ApiOperation;
Expand Down Expand Up @@ -34,6 +38,9 @@ public class AccountController {
@Autowired
private UserService userService;

@Autowired
private MailService mailService;

@ApiOperation("用户注册")
@PostMapping(value = "/register")
public ResponseEntity register(@RequestBody @Valid IndexRegisterFormat format) {
Expand All @@ -58,4 +65,21 @@ public ResponseEntity login(@RequestBody @Valid IndexLoginFormat format) {

return new ResponseEntity("登入成功", token);
}

@ApiOperation("忘记密码")
@PostMapping("/forget_password")
public ResponseEntity forgetPassword(@RequestBody @Valid ForgetPasswordFormat format) {
UserEntity userEntity = userService.getUserByEmail(format.getEmail());
if (! mailService.sendForgetPasswordMessage(format.getUrl(), userEntity)) {
throw new WebErrorException("邮件发送失败");
}
return new ResponseEntity("邮件发送成功");
}

@ApiOperation("重设密码")
@PostMapping("/reset_password")
public ResponseEntity resetPassword(@RequestBody @Valid ResetPasswordFormat format) {
userService.resetUserPassword(format.getEmail(), format.getPassword(), format.getCode());
return new ResponseEntity("密码重置成功");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.eagleoj.web.controller;

import com.eagleoj.web.setting.SettingEnum;
import com.eagleoj.web.setting.SettingService;
import com.eagleoj.web.entity.AttachmentEntity;
import com.eagleoj.web.entity.ResponseEntity;
Expand Down Expand Up @@ -31,8 +32,12 @@ public class IndexController {
public void getAvatar(@RequestParam("aid") int aid,
HttpServletResponse response) throws IOException {
AttachmentEntity entity = attachmentService.getAvatar(aid);
//String OSS_URL = settingService.getSetting(SettingKeyMapper.OSS_URL);
//response.sendRedirect(OSS_URL+entity.getUrl());
if (! settingService.isOpenStorage()) {
response.getWriter().append("未开启存储功能");
return;
}
String OSS_URL = settingService.getSetting(SettingEnum.OSS_URL);
response.sendRedirect(OSS_URL+entity.getUrl());
}

@RequestMapping("/401")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
package com.eagleoj.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.eagleoj.web.cache.CacheController;
import com.eagleoj.web.controller.exception.WebErrorException;
import com.eagleoj.web.controller.format.user.UpdateUserFormat;
import com.eagleoj.web.controller.format.user.UpdateUserPasswordFormat;
import com.eagleoj.web.controller.format.user.UpdateUserProfileFormat;
import com.eagleoj.web.setting.SettingService;
import com.eagleoj.web.controller.format.user.*;
import com.eagleoj.web.mail.MailService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageRowBounds;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import com.eagleoj.web.controller.exception.WebErrorException;
import com.eagleoj.web.controller.format.user.UpdateUserProfileFormat;
import com.eagleoj.web.entity.ResponseEntity;
import com.eagleoj.web.entity.UserEntity;
import com.eagleoj.web.security.SessionHelper;
import com.eagleoj.web.service.AttachmentService;
import com.eagleoj.web.service.ContestUserService;
import com.eagleoj.web.service.GroupUserService;
import com.eagleoj.web.service.UserService;
import com.eagleoj.web.util.FileUtil;
import com.eagleoj.web.util.WebUtil;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.crypto.hash.Md5Hash;
Expand Down Expand Up @@ -56,7 +48,7 @@ public class UserController {
private ContestUserService contestUserService;

@Autowired
private SettingService settingService;
private MailService mailService;

@ApiOperation("获取当前用户的所有信息")
@GetMapping
Expand Down Expand Up @@ -110,13 +102,49 @@ public ResponseEntity updateUserProfile(@Valid @RequestBody UpdateUserProfileFor
return new ResponseEntity("更新成功");
}

@ApiOperation("发送邮箱确认邮件")
@RequiresAuthentication
@PostMapping("/mail/check")
public ResponseEntity checkUserEmail(@RequestBody @Valid SendMailFormat format) {
if (format.getUrl() == null || format.getUrl().length() == 0) {
throw new WebErrorException("url地址不得为空");
}

UserEntity userEntity = userService.getUserByUid(SessionHelper.get().getUid());
if (! mailService.sendConfirmMessage(format.getUrl(), userEntity)) {
throw new WebErrorException("邮件发送失败");
}
return new ResponseEntity("邮件发送成功");
}

@ApiOperation("进行邮箱确认")
@RequiresAuthentication
@PostMapping("/mail/verify")
public ResponseEntity verifyUserEmail(@RequestBody @Valid VerifyUserEmailFormat format) {
String code = format.getCode();
userService.verifyUserEmail(SessionHelper.get().getUid(), code);
return new ResponseEntity("邮箱验证成功");
}

@ApiOperation("更新邮箱")
@RequiresAuthentication
@PutMapping("/mail")
public ResponseEntity updateUserEmail(@RequestBody @Valid SendMailFormat format) {
String email = format.getMail();
if (email == null || email.length() == 0) {
throw new WebErrorException("邮箱不得为空");
}

userService.updateUserEmail(SessionHelper.get().getUid(), email);
return new ResponseEntity("邮箱更新成功");
}

@ApiOperation("更新用户的密码")
@RequiresAuthentication
@PutMapping("/profile/password")
public ResponseEntity updateUserPassword(@Valid @RequestBody UpdateUserPasswordFormat format) {
int uid = SessionHelper.get().getUid();
userService.updateUserPassword(uid, format.getOriginPassword(), format.getNewPassword());
CacheController.getAuthCache().remove(SessionHelper.get().getToken());
return new ResponseEntity("密码更新成功");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.eagleoj.web.controller.format.index;

import org.hibernate.validator.constraints.Email;

import javax.validation.constraints.NotNull;

/**
* @author Smith
**/
public class ForgetPasswordFormat {

@NotNull
private String url;

@Email
@NotNull
private String email;

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.eagleoj.web.controller.format.index;

import org.hibernate.validator.constraints.Email;

import javax.validation.constraints.NotNull;

/**
* @author Smith
**/
public class ResetPasswordFormat {

@Email
@NotNull
private String email;

@NotNull
private String password;

@NotNull
private String code;

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.eagleoj.web.controller.format.user;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;

/**
* @author Smith
**/
public class SendMailFormat {

private String url;

@Email
private String mail;

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getMail() {
return mail;
}

public void setMail(String mail) {
this.mail = mail;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.eagleoj.web.controller.format.user;

import org.hibernate.validator.constraints.NotBlank;

/**
* @author Smith
**/
public class VerifyUserEmailFormat {

@NotBlank
private String code;

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}
}
Loading

0 comments on commit 65f29ac

Please sign in to comment.