mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-20 02:08:15 +08:00
APP端用户登录测试代码
This commit is contained in:
parent
e2de22bdce
commit
d36fa174cc
@ -126,6 +126,7 @@ sa-token:
|
|||||||
security:
|
security:
|
||||||
# 排除路径
|
# 排除路径
|
||||||
excludes:
|
excludes:
|
||||||
|
- /app/appLogin #app用户登录放行
|
||||||
- /login
|
- /login
|
||||||
- /logout
|
- /logout
|
||||||
- /register
|
- /register
|
||||||
|
|||||||
@ -52,18 +52,30 @@ public class LoginUtils {
|
|||||||
* 获取用户类型
|
* 获取用户类型
|
||||||
*/
|
*/
|
||||||
public static UserType getUserType() {
|
public static UserType getUserType() {
|
||||||
String loginId = StpUtil.getLoginIdAsString();
|
String loginId = null;
|
||||||
|
try {
|
||||||
|
loginId = StpUtil.getLoginIdAsString();
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("获取loginId出错,返回空");
|
||||||
|
}
|
||||||
return getUserType(loginId);
|
return getUserType(loginId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UserType getUserType(Object loginId) {
|
public static UserType getUserType(Object loginId) {
|
||||||
|
if (StringUtils.isEmpty(loginId)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
if (StringUtils.contains(loginId.toString(), UserType.SYS_USER.getUserType())) {
|
if (StringUtils.contains(loginId.toString(), UserType.SYS_USER.getUserType())) {
|
||||||
return UserType.SYS_USER;
|
return UserType.SYS_USER;
|
||||||
} else if (StringUtils.contains(loginId.toString(), UserType.APP_USER.getUserType())){
|
} else if (StringUtils.contains(loginId.toString(), UserType.APP_USER.getUserType())){
|
||||||
return UserType.APP_USER;
|
return UserType.APP_USER;
|
||||||
} else {
|
} else {
|
||||||
throw new UtilException("登录用户: LoginId异常 => " + loginId);
|
// throw new UtilException("登录用户: LoginId异常 => " + loginId);
|
||||||
|
return null;//这里如果查不出用户类型 就返回null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getToken() {
|
||||||
|
return StpUtil.getTokenValue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,51 @@
|
|||||||
|
package com.ruoyi.demo.app.controller;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
|
import com.ruoyi.common.core.controller.BaseController;
|
||||||
|
import com.ruoyi.common.core.domain.AjaxResult;
|
||||||
|
import com.ruoyi.common.enums.UserType;
|
||||||
|
import com.ruoyi.common.utils.LoginUtils;
|
||||||
|
import com.ruoyi.common.utils.SecurityUtils;
|
||||||
|
import com.ruoyi.common.utils.StringUtils;
|
||||||
|
import com.ruoyi.demo.app.domain.AppUser;
|
||||||
|
import com.ruoyi.demo.app.domain.vo.AppUserVo;
|
||||||
|
import com.ruoyi.demo.app.utils.AppSecurityUtils;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/*
|
||||||
|
*@DESCRIPTION
|
||||||
|
*@author ye
|
||||||
|
*@create 2021/9/28 19:10
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequiredArgsConstructor(onConstructor_ = @Autowired)
|
||||||
|
@RequestMapping("app")
|
||||||
|
public class AppController extends BaseController {
|
||||||
|
|
||||||
|
@GetMapping("appLogin")
|
||||||
|
public AjaxResult appLogin(String code) {
|
||||||
|
UserType userType = LoginUtils.getUserType();
|
||||||
|
if (userType == UserType.SYS_USER) {
|
||||||
|
return error("系统用户不能登录");
|
||||||
|
} else if (userType == UserType.APP_USER) {
|
||||||
|
return AjaxResult.success("请求头有用户信息", AppSecurityUtils.getUser());
|
||||||
|
}
|
||||||
|
//说明没有token或者token为空 这时候需要根据code来获取openid存到数据库
|
||||||
|
String openid = "code" + code;//代表获取了openid
|
||||||
|
//保存一条数据
|
||||||
|
AppUserVo appUser = new AppUserVo();
|
||||||
|
appUser.setId(123L);//设置主键id
|
||||||
|
appUser.setOpenid(openid);//设置唯一用户标识
|
||||||
|
appUser.setName("app" + openid);//设置一个昵称
|
||||||
|
LoginUtils.login(appUser.getId(), UserType.APP_USER);//登录
|
||||||
|
String token = LoginUtils.getToken();//获取token
|
||||||
|
appUser.setToken(token);//将token传回给前端
|
||||||
|
|
||||||
|
return AjaxResult.success("新用户注册成功",appUser);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,16 @@
|
|||||||
|
package com.ruoyi.demo.app.domain;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/*
|
||||||
|
*@DESCRIPTION
|
||||||
|
*@author ye
|
||||||
|
*@create 2021/9/28 19:18
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class AppUser {
|
||||||
|
|
||||||
|
private Long id;
|
||||||
|
private String openid;
|
||||||
|
private String name;
|
||||||
|
}
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
package com.ruoyi.demo.app.domain.vo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/*
|
||||||
|
*@DESCRIPTION
|
||||||
|
*@author ye
|
||||||
|
*@create 2021/9/28 19:22
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class AppUserVo {
|
||||||
|
|
||||||
|
private Long id;
|
||||||
|
private String openid;
|
||||||
|
private String name;
|
||||||
|
private String token;
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
package com.ruoyi.demo.app.mapper;
|
||||||
|
|
||||||
|
import com.ruoyi.common.core.mybatisplus.core.BaseMapperPlus;
|
||||||
|
import com.ruoyi.demo.app.domain.AppUser;
|
||||||
|
|
||||||
|
public interface AppUserMapper extends BaseMapperPlus<AppUser> {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
package com.ruoyi.demo.app.service;
|
||||||
|
|
||||||
|
import com.ruoyi.demo.app.domain.AppUser;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 接口
|
||||||
|
* */
|
||||||
|
public interface IAppUserService {
|
||||||
|
|
||||||
|
AppUser queryById(Long id);
|
||||||
|
}
|
||||||
@ -0,0 +1,27 @@
|
|||||||
|
package com.ruoyi.demo.app.service.impl;
|
||||||
|
|
||||||
|
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
|
||||||
|
import com.ruoyi.demo.app.domain.AppUser;
|
||||||
|
import com.ruoyi.demo.app.domain.vo.AppUserVo;
|
||||||
|
import com.ruoyi.demo.app.mapper.AppUserMapper;
|
||||||
|
import com.ruoyi.demo.app.service.IAppUserService;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/*
|
||||||
|
*@DESCRIPTION
|
||||||
|
*@author ye
|
||||||
|
*@create 2021/9/28 19:20
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class AppUserServiceImpl extends ServicePlusImpl<AppUserMapper, AppUser, AppUserVo> implements IAppUserService {
|
||||||
|
|
||||||
|
/*模拟查询用户*/
|
||||||
|
@Override
|
||||||
|
public AppUser queryById(Long id) {
|
||||||
|
AppUser appUser = new AppUser();
|
||||||
|
appUser.setId(id);
|
||||||
|
appUser.setName("app" + id);
|
||||||
|
return appUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,37 @@
|
|||||||
|
package com.ruoyi.demo.app.utils;
|
||||||
|
|
||||||
|
import cn.hutool.http.HttpStatus;
|
||||||
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
||||||
|
import com.ruoyi.common.core.service.IUserService;
|
||||||
|
import com.ruoyi.common.exception.ServiceException;
|
||||||
|
import com.ruoyi.common.utils.LoginUtils;
|
||||||
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
||||||
|
import com.ruoyi.demo.app.domain.AppUser;
|
||||||
|
import com.ruoyi.demo.app.service.IAppUserService;
|
||||||
|
|
||||||
|
/*
|
||||||
|
*@DESCRIPTION
|
||||||
|
*@author ye
|
||||||
|
*@create 2021/9/28 19:55
|
||||||
|
*/
|
||||||
|
public class AppSecurityUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户ID
|
||||||
|
**/
|
||||||
|
public static Long getUserId() {
|
||||||
|
return LoginUtils.getUserId();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取用户
|
||||||
|
**/
|
||||||
|
public static AppUser getUser() {
|
||||||
|
try {
|
||||||
|
return SpringUtils.getBean(IAppUserService.class).queryById(getUserId());
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new ServiceException("获取用户信息异常", HttpStatus.HTTP_UNAUTHORIZED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -8,6 +8,7 @@ import cn.hutool.http.useragent.UserAgentUtil;
|
|||||||
import com.ruoyi.common.constant.Constants;
|
import com.ruoyi.common.constant.Constants;
|
||||||
import com.ruoyi.common.core.domain.dto.UserOnlineDTO;
|
import com.ruoyi.common.core.domain.dto.UserOnlineDTO;
|
||||||
import com.ruoyi.common.core.domain.entity.SysUser;
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
||||||
|
import com.ruoyi.common.enums.UserType;
|
||||||
import com.ruoyi.common.utils.RedisUtils;
|
import com.ruoyi.common.utils.RedisUtils;
|
||||||
import com.ruoyi.common.utils.SecurityUtils;
|
import com.ruoyi.common.utils.SecurityUtils;
|
||||||
import com.ruoyi.common.utils.ServletUtils;
|
import com.ruoyi.common.utils.ServletUtils;
|
||||||
@ -28,6 +29,10 @@ public class MySaTokenListener implements SaTokenListener {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void doLogin(String loginType, Object loginId, SaLoginModel loginModel) {
|
public void doLogin(String loginType, Object loginId, SaLoginModel loginModel) {
|
||||||
|
/*这里的loginType在移动端登录的时候是login*/
|
||||||
|
System.out.println(UserType.SYS_USER.getUserType());//sys_user:
|
||||||
|
if (loginType.equals(UserType.SYS_USER.getUserType())) {
|
||||||
|
/*系统用户才处理*/
|
||||||
UserAgent userAgent = UserAgentUtil.parse(ServletUtils.getRequest().getHeader("User-Agent"));
|
UserAgent userAgent = UserAgentUtil.parse(ServletUtils.getRequest().getHeader("User-Agent"));
|
||||||
String ip = ServletUtils.getClientIP();
|
String ip = ServletUtils.getClientIP();
|
||||||
SysUser user = SecurityUtils.getUser();
|
SysUser user = SecurityUtils.getUser();
|
||||||
@ -46,6 +51,7 @@ public class MySaTokenListener implements SaTokenListener {
|
|||||||
RedisUtils.setCacheObject(Constants.ONLINE_TOKEN_KEY + tokenValue, userOnlineDTO);
|
RedisUtils.setCacheObject(Constants.ONLINE_TOKEN_KEY + tokenValue, userOnlineDTO);
|
||||||
log.info("user doLogin, useId:{}, token:{}", loginId, tokenValue);
|
log.info("user doLogin, useId:{}, token:{}", loginId, tokenValue);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 每次注销时触发
|
* 每次注销时触发
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user