登陆功能采用Session方案,即登陆用户的信息存放在Session中。
接口信息
实体类
参数DTO
在包com.swx.easypan.entity.dto
,下创建 LoginDTO 实体类,将下面代码放入:
LoginDTO@Data public class LoginDTO {
@NotNull @Email private String email;
@NotNull private String password;
@NotNull private String checkCode; }
|
视图VO
在包com.swx.easypan.entity.vo
,下创建 SessionWebUserVO 实体类,将下面代码放入:
SessionWebUserVO@Data public class SessionWebUserVO implements Serializable { private String nickname; private String id; private Boolean isAdmin; private String avatar; }
|
定义常量
用户状态枚举常量,在com.swx.easypan.entity.enums
包下创建 UserStatusEnum 枚举类:
UserStatusEnumpublic enum UserStatusEnum {
ENABLE(1, "启用"), DISABLE(0, "禁用");
private Integer status; private String desc;
UserStatusEnum(Integer status, String desc) { this.status = status; this.desc = desc; }
public Integer status() { return this.status; } public String desc() { return this.desc; } }
|
静态常量,Constants 类中定义常量
Constantspublic static final String SESSION_KEY = "session_key";
|
定义Service
找到 UserInfoService接口,在其中添加:
UserInfoServicepublic interface UserInfoService extends IService<UserInfo> {
SessionWebUserVO login(String email, String password); }
|
在 UserInfoServiceImpl 中实现该方法
UserInfoServiceImpl@Service public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService { @Resource private AppConfig appConfig; @Override public SessionWebUserVO login(String email, String password) { UserInfo userInfo = getOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getEmail, email)); if (null == userInfo || !userInfo.getPassword().equals(MD5.encrypt(password))) { throw new BizException("账号或者密码错误"); }
if (UserStatusEnum.DISABLE.status().equals(userInfo.getStatus())) { throw new BizException("账号已禁用"); } UserInfo updateInfo = new UserInfo(); updateInfo.setLastLoginTime(LocalDateTime.now()); updateById(updateInfo);
SessionWebUserVO sessionWebUserVO = new SessionWebUserVO(); sessionWebUserVO.setNickname(userInfo.getNickname()); sessionWebUserVO.setId(userInfo.getId()); sessionWebUserVO.setAvatar(userInfo.getQqAvatar()); sessionWebUserVO.setIsAdmin( ArrayUtils.contains(appConfig.getEmails().split(","), email)); return sessionWebUserVO; } }
|
定义Controller
在 UserInfoController 下定义登陆方法,登陆成功后将用户信息放入到Session中,并将验证码从Session中删除,防止验证码重复使用。
UserInfoController@RestController("userInfoController") @ResponseResult @Validated public class UserInfoController {
private final UserInfoService userInfoService; public UserInfoController(UserInfoService userInfoService) { this.userInfoService = userInfoService; } @PostMapping("/login") public SessionWebUserVO login(HttpSession session, @Valid @RequestBody LoginDTO loginDto) { try { if (!loginDto.getCheckCode().equalsIgnoreCase((String) session.getAttribute(Constants.CHECK_CODE_KEY))) { throw new BizException("图片验证码错误"); } SessionWebUserVO sessionWebUserVo = userInfoService.login(loginDto.getEmail(), loginDto.getPassword()); session.setAttribute(Constants.SESSION_KEY, sessionWebUserVo); return sessionWebUserVo; } finally { session.removeAttribute(Constants.CHECK_CODE_KEY); } } }
|