Add front user authentication
This commit is contained in:
@@ -61,6 +61,18 @@
|
|||||||
<artifactId>ruoyi-generator</artifactId>
|
<artifactId>ruoyi-generator</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-core</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@@ -93,4 +105,4 @@
|
|||||||
<finalName>${project.artifactId}</finalName>
|
<finalName>${project.artifactId}</finalName>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
package com.ruoyi.web.service.front;
|
||||||
|
|
||||||
|
import com.ruoyi.common.constant.Constants;
|
||||||
|
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.FrontUser;
|
||||||
|
import com.ruoyi.generator.service.front.IFrontUserService;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
|
public class FrontAuthServiceTest
|
||||||
|
{
|
||||||
|
private FrontAuthService frontAuthService;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private IFrontUserService frontUserService;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private TokenService tokenService;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception
|
||||||
|
{
|
||||||
|
frontAuthService = new FrontAuthService();
|
||||||
|
setField("frontUserService", frontUserService);
|
||||||
|
setField("tokenService", tokenService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void loginCreatesFrontToken()
|
||||||
|
{
|
||||||
|
FrontUser user = new FrontUser();
|
||||||
|
user.setUserId(100L);
|
||||||
|
user.setUsername("demo");
|
||||||
|
user.setNickname("Demo");
|
||||||
|
user.setPassword(SecurityUtils.encryptPassword("admin123"));
|
||||||
|
user.setStatus("0");
|
||||||
|
user.setDelFlag("0");
|
||||||
|
|
||||||
|
when(frontUserService.selectByUsername("demo")).thenReturn(user);
|
||||||
|
when(tokenService.createToken(any(LoginUser.class))).thenAnswer(invocation -> {
|
||||||
|
LoginUser loginUser = invocation.getArgument(0);
|
||||||
|
assertEquals(Constants.LOGIN_TYPE_FRONT, loginUser.getLoginType());
|
||||||
|
assertEquals(Long.valueOf(100L), loginUser.getUserId());
|
||||||
|
return "front-token";
|
||||||
|
});
|
||||||
|
|
||||||
|
String token = frontAuthService.login("demo", "admin123");
|
||||||
|
|
||||||
|
assertEquals("front-token", token);
|
||||||
|
verify(frontUserService).updateLastLoginTime(100L);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setField(String name, Object value) throws Exception
|
||||||
|
{
|
||||||
|
Field field = FrontAuthService.class.getDeclaredField(name);
|
||||||
|
field.setAccessible(true);
|
||||||
|
field.set(frontAuthService, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,9 +5,9 @@ import java.util.List;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import com.ruoyi.common.exception.ServiceException;
|
import com.ruoyi.common.exception.ServiceException;
|
||||||
|
import com.ruoyi.common.utils.SecurityUtils;
|
||||||
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.common.utils.StringUtils;
|
||||||
import com.ruoyi.generator.domain.front.FrontUser;
|
import com.ruoyi.generator.domain.front.FrontUser;
|
||||||
import com.ruoyi.generator.domain.front.dto.FrontLoginBody;
|
|
||||||
import com.ruoyi.generator.domain.front.dto.FrontRegisterBody;
|
import com.ruoyi.generator.domain.front.dto.FrontRegisterBody;
|
||||||
import com.ruoyi.generator.mapper.front.FrontUserMapper;
|
import com.ruoyi.generator.mapper.front.FrontUserMapper;
|
||||||
|
|
||||||
@@ -18,55 +18,71 @@ public class FrontUserServiceImpl implements IFrontUserService
|
|||||||
private FrontUserMapper frontUserMapper;
|
private FrontUserMapper frontUserMapper;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FrontUser login(FrontLoginBody loginBody)
|
public FrontUser selectByUsername(String username)
|
||||||
{
|
{
|
||||||
if (loginBody == null || StringUtils.isEmpty(loginBody.getUsername()) || StringUtils.isEmpty(loginBody.getPassword()))
|
return frontUserMapper.selectFrontUserByUsername(username);
|
||||||
{
|
|
||||||
throw new ServiceException("用户名或密码不能为空");
|
|
||||||
}
|
|
||||||
FrontUser user = frontUserMapper.selectFrontUserByUsername(loginBody.getUsername());
|
|
||||||
if (user == null || !StringUtils.equals(user.getPassword(), loginBody.getPassword()))
|
|
||||||
{
|
|
||||||
throw new ServiceException("用户名或密码错误");
|
|
||||||
}
|
|
||||||
user.setLastLoginTime(new Date());
|
|
||||||
frontUserMapper.updateFrontUser(user);
|
|
||||||
return user;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FrontUser register(FrontRegisterBody registerBody)
|
public FrontUser selectById(Long userId)
|
||||||
{
|
|
||||||
if (registerBody == null || StringUtils.isEmpty(registerBody.getUsername()) || StringUtils.isEmpty(registerBody.getPassword()))
|
|
||||||
{
|
|
||||||
throw new ServiceException("用户名或密码不能为空");
|
|
||||||
}
|
|
||||||
if (frontUserMapper.selectFrontUserByUsername(registerBody.getUsername()) != null)
|
|
||||||
{
|
|
||||||
throw new ServiceException("用户名已存在");
|
|
||||||
}
|
|
||||||
FrontUser user = new FrontUser();
|
|
||||||
user.setUsername(registerBody.getUsername());
|
|
||||||
user.setPassword(registerBody.getPassword());
|
|
||||||
user.setNickname(registerBody.getNickname());
|
|
||||||
user.setEmail(registerBody.getEmail());
|
|
||||||
user.setPhone(registerBody.getPhone());
|
|
||||||
user.setStatus("0");
|
|
||||||
user.setDelFlag("0");
|
|
||||||
frontUserMapper.insertFrontUser(user);
|
|
||||||
return user;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public FrontUser selectFrontUserById(Long userId)
|
|
||||||
{
|
{
|
||||||
return frontUserMapper.selectFrontUserById(userId);
|
return frontUserMapper.selectFrontUserById(userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int register(FrontRegisterBody body)
|
||||||
|
{
|
||||||
|
if (body == null || StringUtils.isEmpty(body.getUsername()))
|
||||||
|
{
|
||||||
|
throw new ServiceException("用户名不能为空");
|
||||||
|
}
|
||||||
|
if (body.getUsername().length() < 2 || body.getUsername().length() > 50)
|
||||||
|
{
|
||||||
|
throw new ServiceException("用户名长度必须在2到50个字符之间");
|
||||||
|
}
|
||||||
|
if (StringUtils.isEmpty(body.getPassword()))
|
||||||
|
{
|
||||||
|
throw new ServiceException("密码不能为空");
|
||||||
|
}
|
||||||
|
if (body.getPassword().length() < 5 || body.getPassword().length() > 50)
|
||||||
|
{
|
||||||
|
throw new ServiceException("密码长度必须在5到50个字符之间");
|
||||||
|
}
|
||||||
|
if (frontUserMapper.selectFrontUserByUsername(body.getUsername()) != null)
|
||||||
|
{
|
||||||
|
throw new ServiceException("前台账号已存在");
|
||||||
|
}
|
||||||
|
|
||||||
|
FrontUser user = new FrontUser();
|
||||||
|
user.setUsername(body.getUsername());
|
||||||
|
user.setPassword(SecurityUtils.encryptPassword(body.getPassword()));
|
||||||
|
user.setNickname(StringUtils.isEmpty(body.getNickname()) ? body.getUsername() : body.getNickname());
|
||||||
|
user.setEmail(body.getEmail());
|
||||||
|
user.setPhone(body.getPhone());
|
||||||
|
user.setStatus("0");
|
||||||
|
user.setDelFlag("0");
|
||||||
|
return frontUserMapper.insertFrontUser(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int updateLastLoginTime(Long userId)
|
||||||
|
{
|
||||||
|
FrontUser user = new FrontUser();
|
||||||
|
user.setUserId(userId);
|
||||||
|
user.setLastLoginTime(new Date());
|
||||||
|
return frontUserMapper.updateFrontUser(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FrontUser selectFrontUserById(Long userId)
|
||||||
|
{
|
||||||
|
return selectById(userId);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FrontUser selectFrontUserByUsername(String username)
|
public FrontUser selectFrontUserByUsername(String username)
|
||||||
{
|
{
|
||||||
return frontUserMapper.selectFrontUserByUsername(username);
|
return selectByUsername(username);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -2,13 +2,14 @@ package com.ruoyi.generator.service.front;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import com.ruoyi.generator.domain.front.FrontUser;
|
import com.ruoyi.generator.domain.front.FrontUser;
|
||||||
import com.ruoyi.generator.domain.front.dto.FrontLoginBody;
|
|
||||||
import com.ruoyi.generator.domain.front.dto.FrontRegisterBody;
|
import com.ruoyi.generator.domain.front.dto.FrontRegisterBody;
|
||||||
|
|
||||||
public interface IFrontUserService
|
public interface IFrontUserService
|
||||||
{
|
{
|
||||||
public FrontUser login(FrontLoginBody loginBody);
|
public FrontUser selectByUsername(String username);
|
||||||
public FrontUser register(FrontRegisterBody registerBody);
|
public FrontUser selectById(Long userId);
|
||||||
|
public int register(FrontRegisterBody body);
|
||||||
|
public int updateLastLoginTime(Long userId);
|
||||||
public FrontUser selectFrontUserById(Long userId);
|
public FrontUser selectFrontUserById(Long userId);
|
||||||
public FrontUser selectFrontUserByUsername(String username);
|
public FrontUser selectFrontUserByUsername(String username);
|
||||||
public List<FrontUser> selectFrontUserList(FrontUser frontUser);
|
public List<FrontUser> selectFrontUserList(FrontUser frontUser);
|
||||||
|
|||||||
Reference in New Issue
Block a user