Search in sources :

Example 1 with UserBean

use of com.qiwenshare.file.domain.user.UserBean in project qiwen-file by qiwenshare.

the class UserController method checkUserLoginInfo.

@Operation(summary = "检查用户登录信息", description = "验证token的有效性", tags = { "user" })
@GetMapping("/checkuserlogininfo")
@ResponseBody
public RestResult<UserLoginVo> checkUserLoginInfo() {
    UserLoginVo userLoginVo = new UserLoginVo();
    JwtUser sessionUserBean = SessionUtil.getSession();
    if (sessionUserBean != null && !"anonymousUser".equals(sessionUserBean.getUsername())) {
        LambdaQueryWrapper<UserLoginInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserLoginInfo::getUserId, sessionUserBean.getUserId());
        lambdaQueryWrapper.likeRight(UserLoginInfo::getUserloginDate, DateUtil.getCurrentTime().substring(0, 10));
        userLoginInfoService.remove(lambdaQueryWrapper);
        UserLoginInfo userLoginInfo = new UserLoginInfo();
        userLoginInfo.setUserId(sessionUserBean.getUserId());
        userLoginInfo.setUserloginDate(DateUtil.getCurrentTime());
        userLoginInfoService.save(userLoginInfo);
        UserBean user = userService.getById(sessionUserBean.getUserId());
        BeanUtil.copyProperties(user, userLoginVo);
        return RestResult.success().data(userLoginVo);
    } else {
        return RestResult.fail().message("用户暂未登录");
    }
}
Also used : UserLoginVo(com.qiwenshare.file.vo.user.UserLoginVo) UserBean(com.qiwenshare.file.domain.user.UserBean) JwtUser(com.qiwenshare.common.util.security.JwtUser) UserLoginInfo(com.qiwenshare.file.domain.UserLoginInfo) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) Operation(io.swagger.v3.oas.annotations.Operation)

Example 2 with UserBean

use of com.qiwenshare.file.domain.user.UserBean in project qiwen-file by qiwenshare.

the class UserService method loadUserByUsername.

@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
    UserBean user = userMapper.selectById(Long.valueOf(s));
    if (user == null) {
        throw new UsernameNotFoundException(String.format("用户不存在"));
    }
    List<Role> roleList = selectRoleListByUserId(user.getUserId());
    List<SimpleGrantedAuthority> authorities = new ArrayList<>();
    for (Role role : roleList) {
        SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority("ROLE_" + role.getRoleName());
        authorities.add(simpleGrantedAuthority);
    }
    JwtUser jwtUser = new JwtUser(user.getUserId(), user.getUsername(), user.getPassword(), user.getAvailable(), authorities);
    return jwtUser;
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) Role(com.qiwenshare.file.domain.user.Role) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) UserBean(com.qiwenshare.file.domain.user.UserBean) ArrayList(java.util.ArrayList) JwtUser(com.qiwenshare.common.util.security.JwtUser)

Example 3 with UserBean

use of com.qiwenshare.file.domain.user.UserBean in project qiwen-file by qiwenshare.

the class UserController method addUser.

@Operation(summary = "用户注册", description = "注册账号", tags = { "user" })
@PostMapping(value = "/register")
@MyLog(operation = "用户注册", module = CURRENT_MODULE)
@ResponseBody
public RestResult<String> addUser(@Valid @RequestBody RegisterDTO registerDTO) {
    RestResult<String> restResult = null;
    UserBean userBean = new UserBean();
    BeanUtil.copyProperties(registerDTO, userBean);
    restResult = userService.registerUser(userBean);
    return restResult;
}
Also used : UserBean(com.qiwenshare.file.domain.user.UserBean) Operation(io.swagger.v3.oas.annotations.Operation) MyLog(com.qiwenshare.common.anno.MyLog)

Example 4 with UserBean

use of com.qiwenshare.file.domain.user.UserBean in project qiwen-file by qiwenshare.

the class UserController method userLogin.

@Operation(summary = "用户登录", description = "用户登录认证后才能进入系统", tags = { "user" })
@GetMapping("/login")
@MyLog(operation = "用户登录", module = CURRENT_MODULE)
@ResponseBody
public RestResult<UserLoginVo> userLogin(@Parameter(description = "登录手机号") String telephone, @Parameter(description = "登录密码") String password) {
    RestResult<UserLoginVo> restResult = new RestResult<UserLoginVo>();
    String salt = userService.getSaltByTelephone(telephone);
    String hashPassword = new SimpleHash("MD5", password, salt, 1024).toHex();
    UserBean result = userService.selectUserByTelephoneAndPassword(telephone, hashPassword);
    if (result == null) {
        return RestResult.fail().message("手机号或密码错误!");
    }
    Map<String, Object> param = new HashMap<>();
    param.put("userId", result.getUserId());
    String token = "";
    try {
        token = jwtComp.createJWT(JSON.toJSONString(param));
    } catch (Exception e) {
        log.info("登录失败:{}", e);
        return RestResult.fail().message("创建token失败!");
    }
    UserBean sessionUserBean = userService.findUserInfoByTelephone(telephone);
    if (sessionUserBean.getAvailable() != null && sessionUserBean.getAvailable() == 0) {
        return RestResult.fail().message("用户已被禁用");
    }
    UserLoginVo userLoginVo = new UserLoginVo();
    BeanUtil.copyProperties(sessionUserBean, userLoginVo);
    userLoginVo.setToken("Bearer " + token);
    restResult.setData(userLoginVo);
    restResult.setSuccess(true);
    restResult.setCode(200001);
    return restResult;
}
Also used : RestResult(com.qiwenshare.common.result.RestResult) UserLoginVo(com.qiwenshare.file.vo.user.UserLoginVo) UserBean(com.qiwenshare.file.domain.user.UserBean) HashMap(java.util.HashMap) SimpleHash(org.apache.shiro.crypto.hash.SimpleHash) Operation(io.swagger.v3.oas.annotations.Operation) MyLog(com.qiwenshare.common.anno.MyLog)

Example 5 with UserBean

use of com.qiwenshare.file.domain.user.UserBean in project qiwen-file by qiwenshare.

the class UserService method getUserIdByToken.

@Override
public Long getUserIdByToken(String token) {
    Claims c = null;
    if (StringUtils.isEmpty(token)) {
        return null;
    }
    token = token.replace("Bearer ", "");
    token = token.replace("Bearer%20", "");
    try {
        c = jwtComp.parseJWT(token);
    } catch (Exception e) {
        log.error("解码异常:" + e);
        return null;
    }
    if (c == null) {
        log.info("解码为空");
        return null;
    }
    String subject = c.getSubject();
    log.debug("解析结果:" + subject);
    UserBean tokenUserBean = JSON.parseObject(subject, UserBean.class);
    UserBean user = userMapper.selectById(tokenUserBean.getUserId());
    if (user != null) {
        return user.getUserId();
    }
    return null;
}
Also used : Claims(io.jsonwebtoken.Claims) UserBean(com.qiwenshare.file.domain.user.UserBean) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException)

Aggregations

UserBean (com.qiwenshare.file.domain.user.UserBean)5 Operation (io.swagger.v3.oas.annotations.Operation)3 MyLog (com.qiwenshare.common.anno.MyLog)2 JwtUser (com.qiwenshare.common.util.security.JwtUser)2 UserLoginVo (com.qiwenshare.file.vo.user.UserLoginVo)2 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)2 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)1 RestResult (com.qiwenshare.common.result.RestResult)1 UserLoginInfo (com.qiwenshare.file.domain.UserLoginInfo)1 Role (com.qiwenshare.file.domain.user.Role)1 Claims (io.jsonwebtoken.Claims)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 SimpleHash (org.apache.shiro.crypto.hash.SimpleHash)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1