|
@@ -3,9 +3,12 @@ package com.sooka.auth.controller;
|
|
|
import com.sooka.auth.form.LoginBody;
|
|
|
import com.sooka.auth.form.RegisterBody;
|
|
|
import com.sooka.auth.service.SysLoginService;
|
|
|
+import com.sooka.auth.util.RsaUtil;
|
|
|
+import com.sooka.auth.util.SecretKeyBo;
|
|
|
import com.sooka.common.core.domain.R;
|
|
|
import com.sooka.common.core.utils.JwtUtils;
|
|
|
import com.sooka.common.core.utils.StringUtils;
|
|
|
+import com.sooka.common.redis.service.RedisService;
|
|
|
import com.sooka.common.security.auth.AuthUtil;
|
|
|
import com.sooka.common.security.service.TokenService;
|
|
|
import com.sooka.common.security.utils.SecurityUtils;
|
|
@@ -16,7 +19,11 @@ import org.springframework.web.bind.annotation.PostMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* token 控制
|
|
@@ -31,12 +38,61 @@ public class TokenController {
|
|
|
@Autowired
|
|
|
private SysLoginService sysLoginService;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private RedisService redisService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取秘钥接口
|
|
|
+ * 前端请求后该方法会生成一对秘钥,分别为公钥和私钥
|
|
|
+ * 将公钥返回给前端用于加密,私钥存入缓存(60s)用于后台解密
|
|
|
+ * Author 李猛
|
|
|
+ * Pc端调用
|
|
|
+ */
|
|
|
+ @PostMapping("getSecretKey")
|
|
|
+ public R<?> getSecretKey(HttpServletRequest request) {
|
|
|
+ String sessionId = request.getSession().getId();
|
|
|
+ return getSecretKey(sessionId);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 改造获取秘钥接口
|
|
|
+ * App、Pc端通用
|
|
|
+ * Author 李猛
|
|
|
+ */
|
|
|
+ public R<?> getSecretKey(String sessionId) {
|
|
|
+ String publicKey;
|
|
|
+ try {
|
|
|
+ SecretKeyBo bo = RsaUtil.genKeyPair();
|
|
|
+ redisService.setCacheObject(sessionId, bo.getPrivateKey(), 60L, TimeUnit.SECONDS);
|
|
|
+ publicKey = bo.getPublicKey();
|
|
|
+ } catch (NoSuchAlgorithmException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotEmpty(publicKey)) {
|
|
|
+ return R.ok(publicKey, "操作成功");
|
|
|
+ }
|
|
|
+ return R.fail("数据异常,请稍后重试");
|
|
|
+ }
|
|
|
+
|
|
|
@PostMapping("login")
|
|
|
- public R<?> login(@RequestBody LoginBody form) {
|
|
|
+ public R<?> login(@RequestBody LoginBody form, HttpServletRequest request) {
|
|
|
+ //解密密码字符串
|
|
|
+ sysLoginService.decryptPassword(form, request);
|
|
|
+ // 用户登录
|
|
|
+// LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
|
|
|
+ // 获取登录token
|
|
|
+ return R.ok(login(form));
|
|
|
+ }
|
|
|
+
|
|
|
+ public Map login(LoginBody form) {
|
|
|
// 用户登录
|
|
|
LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
|
|
|
// 获取登录token
|
|
|
- return R.ok(tokenService.createToken(userInfo));
|
|
|
+ Map map = tokenService.createToken(userInfo);
|
|
|
+ LoginUser user = tokenService.getLoginUser(map.get("access_token").toString());
|
|
|
+ map.put("userinfo", user);
|
|
|
+ return map;
|
|
|
}
|
|
|
|
|
|
@DeleteMapping("logout")
|