parent
1bf068db95
commit
c837ebf949
|
@ -1,38 +0,0 @@
|
|||
package com.health.auth.controller;
|
||||
|
||||
import com.health.auth.service.UserService;
|
||||
import com.health.common.domain.User;
|
||||
import com.health.common.domain.req.ReqLogin;
|
||||
import com.health.common.domain.resp.RespJWT;
|
||||
import com.health.common.result.Result;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/admin")
|
||||
@AllArgsConstructor
|
||||
public class UserController {
|
||||
private final UserService userService;
|
||||
|
||||
@PostMapping("/login")
|
||||
public Result<RespJWT> login(@RequestBody ReqLogin reqLogin){
|
||||
return userService.login(reqLogin);
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/user/info")
|
||||
Result<User> userinfo(HttpServletRequest request){
|
||||
return userService.userinfo(request);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
package com.health.auth.feign;
|
||||
|
||||
import com.health.common.domain.User;
|
||||
import com.health.common.result.Result;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
*/
|
||||
@FeignClient("base_system")
|
||||
public interface AuthFeignSystem {
|
||||
|
||||
/**
|
||||
* 到系统模块去查找用户
|
||||
* @param userName
|
||||
* @return result风格的user对象(用户信息)
|
||||
*/
|
||||
@GetMapping("/user/byUserName/{userName}")
|
||||
Result<User> byUserName(@PathVariable String userName);
|
||||
|
||||
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
package com.health.auth.service;
|
||||
|
||||
import com.health.common.domain.User;
|
||||
import com.health.common.domain.req.ReqLogin;
|
||||
import com.health.common.domain.resp.RespJWT;
|
||||
import com.health.common.result.Result;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
*/
|
||||
public interface UserService {
|
||||
|
||||
|
||||
/**
|
||||
* 登录方法--获取token
|
||||
* @param reqLogin
|
||||
* @return 返回result格式的jwt包含:(token,过期时间)
|
||||
*/
|
||||
Result<RespJWT> login(ReqLogin reqLogin);
|
||||
|
||||
/**
|
||||
* 每次都对请求做权限判断
|
||||
* @param request
|
||||
* @return 返回result格式的User对象包含:(用户信息)
|
||||
*/
|
||||
Result<User> userinfo(HttpServletRequest request);
|
||||
|
||||
|
||||
}
|
|
@ -1,98 +0,0 @@
|
|||
package com.health.auth.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.health.auth.feign.AuthFeignSystem;
|
||||
import com.health.auth.service.UserService;
|
||||
import com.health.common.constants.JwtConstants;
|
||||
import com.health.common.constants.TokenConstants;
|
||||
import com.health.common.domain.User;
|
||||
import com.health.common.domain.req.ReqLogin;
|
||||
import com.health.common.domain.resp.RespJWT;
|
||||
import com.health.common.result.Result;
|
||||
import com.health.common.utils.JwtUtils;
|
||||
import com.health.common.utils.StringUtils;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 用户登录业务层
|
||||
* @author Administrator
|
||||
* @date 2023年10月15日 09点48分
|
||||
*/
|
||||
@Service
|
||||
@Log4j2
|
||||
@AllArgsConstructor
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
private final AuthFeignSystem authFeignSystem;
|
||||
private final RedisTemplate<String,String> redisTemplate;
|
||||
|
||||
|
||||
/**
|
||||
* 登录方法--获取token
|
||||
*
|
||||
* @param reqLogin
|
||||
* @return 返回result格式的jwt包含:(token,过期时间)
|
||||
*/
|
||||
@Override
|
||||
public Result<RespJWT> login(ReqLogin reqLogin) {
|
||||
if (StringUtils.isEmpty(reqLogin.getUsername())){
|
||||
return Result.error("用户名为空!");
|
||||
}
|
||||
if (StringUtils.isEmpty(reqLogin.getPassword())){
|
||||
return Result.error("密码为空!");
|
||||
|
||||
}
|
||||
String username = reqLogin.getUsername();
|
||||
|
||||
|
||||
Result<User> userResult = authFeignSystem.byUserName(username);
|
||||
User data = userResult.getData();
|
||||
if (null == data) {
|
||||
return Result.error("先注册手机号,在登录");
|
||||
}
|
||||
HashMap<String, Object> map = new HashMap<>();
|
||||
String userkey = UUID.randomUUID().toString().replaceAll("-", "");
|
||||
map.put(JwtConstants.USER_KEY, userkey);
|
||||
map.put(JwtConstants.DETAILS_USER_ID, data.getId());
|
||||
String token = JwtUtils.createToken(map);
|
||||
redisTemplate.opsForValue().set(
|
||||
TokenConstants.LOGIN_TOKEN_KEY + userkey,
|
||||
JSONObject.toJSONString(data),
|
||||
TokenConstants.EXPIRATION,
|
||||
TimeUnit.MINUTES
|
||||
);
|
||||
RespJWT respJWT = new RespJWT();
|
||||
respJWT.setToken(token);
|
||||
respJWT.setEidTime("720MIN");
|
||||
|
||||
return Result.success(respJWT);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 每次都对请求做权限判断
|
||||
*
|
||||
* @param request
|
||||
* @return 返回result格式的User对象包含:(用户信息)
|
||||
*/
|
||||
@Override
|
||||
public Result<User> userinfo(HttpServletRequest request) {
|
||||
|
||||
String token = request.getHeader(TokenConstants.TOKEN);
|
||||
String userKey = JwtUtils.getUserKey(token);
|
||||
String s = redisTemplate.opsForValue().get(
|
||||
TokenConstants.LOGIN_TOKEN_KEY + userKey
|
||||
);
|
||||
User user = JSONObject.parseObject(s, User.class);
|
||||
return Result.success(user);
|
||||
|
||||
}
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
package com.health.common.domain;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
*/
|
||||
@Data
|
||||
public class User {
|
||||
|
||||
private Integer id;
|
||||
private String username;
|
||||
private String password;
|
||||
private String emil;
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
package com.health.common.domain.req;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
*/
|
||||
@Data
|
||||
public class ReqLogin {
|
||||
private Integer id;
|
||||
private String username;
|
||||
private String password;
|
||||
private String emil;
|
||||
|
||||
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
package com.health.common.domain.resp;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
*/
|
||||
@Data
|
||||
public class RespJWT {
|
||||
|
||||
private String token;
|
||||
private String eidTime;
|
||||
}
|
Loading…
Reference in New Issue