Add front user authentication
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
package com.ruoyi.web.controller.front;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import com.ruoyi.common.constant.Constants;
|
||||
import com.ruoyi.common.core.controller.BaseController;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.core.domain.model.LoginUser;
|
||||
import com.ruoyi.common.utils.SecurityUtils;
|
||||
import com.ruoyi.framework.web.service.TokenService;
|
||||
import com.ruoyi.generator.domain.front.dto.FrontLoginBody;
|
||||
import com.ruoyi.generator.domain.front.dto.FrontRegisterBody;
|
||||
import com.ruoyi.generator.service.front.IFrontUserService;
|
||||
import com.ruoyi.web.service.front.FrontAuthService;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/front/auth")
|
||||
public class FrontAuthController extends BaseController
|
||||
{
|
||||
@Autowired
|
||||
private FrontAuthService frontAuthService;
|
||||
|
||||
@Autowired
|
||||
private IFrontUserService frontUserService;
|
||||
|
||||
@Autowired
|
||||
private TokenService tokenService;
|
||||
|
||||
@PostMapping("/register")
|
||||
public AjaxResult register(@RequestBody FrontRegisterBody body)
|
||||
{
|
||||
return toAjax(frontUserService.register(body));
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
public AjaxResult login(@RequestBody FrontLoginBody body)
|
||||
{
|
||||
AjaxResult ajax = AjaxResult.success();
|
||||
String token = frontAuthService.login(body.getUsername(), body.getPassword());
|
||||
ajax.put(Constants.TOKEN, token);
|
||||
return ajax;
|
||||
}
|
||||
|
||||
@GetMapping("/profile")
|
||||
public AjaxResult profile()
|
||||
{
|
||||
return AjaxResult.success(frontUserService.selectById(SecurityUtils.getUserId()));
|
||||
}
|
||||
|
||||
@PostMapping("/logout")
|
||||
public AjaxResult logout()
|
||||
{
|
||||
LoginUser loginUser = SecurityUtils.getLoginUser();
|
||||
tokenService.delLoginUser(loginUser.getToken());
|
||||
return AjaxResult.success();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.ruoyi.web.service.front;
|
||||
|
||||
import java.util.Collections;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import com.ruoyi.common.constant.Constants;
|
||||
import com.ruoyi.common.core.domain.entity.SysUser;
|
||||
import com.ruoyi.common.core.domain.model.LoginUser;
|
||||
import com.ruoyi.common.enums.UserStatus;
|
||||
import com.ruoyi.common.exception.ServiceException;
|
||||
import com.ruoyi.common.utils.SecurityUtils;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.framework.web.service.TokenService;
|
||||
import com.ruoyi.generator.domain.front.FrontUser;
|
||||
import com.ruoyi.generator.service.front.IFrontUserService;
|
||||
|
||||
@Component
|
||||
public class FrontAuthService
|
||||
{
|
||||
@Autowired
|
||||
private IFrontUserService frontUserService;
|
||||
|
||||
@Autowired
|
||||
private TokenService tokenService;
|
||||
|
||||
public String login(String username, String password)
|
||||
{
|
||||
FrontUser frontUser = frontUserService.selectByUsername(username);
|
||||
if (frontUser == null)
|
||||
{
|
||||
throw new ServiceException("前台账号或密码错误");
|
||||
}
|
||||
if (UserStatus.DISABLE.getCode().equals(frontUser.getStatus()))
|
||||
{
|
||||
throw new ServiceException("前台账号已停用");
|
||||
}
|
||||
if (UserStatus.DELETED.getCode().equals(frontUser.getDelFlag()))
|
||||
{
|
||||
throw new ServiceException("前台账号已删除");
|
||||
}
|
||||
if (!SecurityUtils.matchesPassword(password, frontUser.getPassword()))
|
||||
{
|
||||
throw new ServiceException("前台账号或密码错误");
|
||||
}
|
||||
|
||||
SysUser tokenUser = new SysUser();
|
||||
tokenUser.setUserId(frontUser.getUserId());
|
||||
tokenUser.setUserName(frontUser.getUsername());
|
||||
tokenUser.setNickName(StringUtils.nvl(frontUser.getNickname(), frontUser.getUsername()));
|
||||
tokenUser.setPassword(frontUser.getPassword());
|
||||
|
||||
LoginUser loginUser = new LoginUser(frontUser.getUserId(), null, tokenUser, Collections.emptySet());
|
||||
loginUser.setLoginType(Constants.LOGIN_TYPE_FRONT);
|
||||
String token = tokenService.createToken(loginUser);
|
||||
frontUserService.updateLastLoginTime(frontUser.getUserId());
|
||||
return token;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user