|
@@ -3,21 +3,26 @@ package com.fd.controller;
|
|
|
import com.fd.constant.MsgCode;
|
|
|
import com.fd.entity.User;
|
|
|
import com.fd.repository.UserRepository;
|
|
|
+import com.fd.shiro.JWTToken;
|
|
|
import com.fd.shiro.JWTUtil;
|
|
|
import com.fd.util.R;
|
|
|
import lombok.extern.log4j.Log4j2;
|
|
|
import org.apache.shiro.SecurityUtils;
|
|
|
+import org.apache.shiro.authc.UsernamePasswordToken;
|
|
|
import org.apache.shiro.authz.UnauthorizedException;
|
|
|
import org.apache.shiro.authz.annotation.Logical;
|
|
|
import org.apache.shiro.authz.annotation.RequiresAuthentication;
|
|
|
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
|
|
import org.apache.shiro.authz.annotation.RequiresRoles;
|
|
|
+import org.apache.shiro.mgt.SecurityManager;
|
|
|
import org.apache.shiro.subject.Subject;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* Created by Owen on 2019/11/11 0011 16:50
|
|
@@ -29,28 +34,58 @@ public class UserController {
|
|
|
@Autowired
|
|
|
private UserRepository userRepository;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate redisTemplate;
|
|
|
+
|
|
|
+ private static final String SALT = "cesium_";
|
|
|
+
|
|
|
@PostMapping("/login")
|
|
|
public R login(@RequestParam("username") String username,
|
|
|
@RequestParam("password") String password) {
|
|
|
- log.info("run login , username:{}, password:{}", username, password);
|
|
|
+ log.warn("run login , username:{}, password:{}", username, password);
|
|
|
User user = userRepository.findByUsername(username);
|
|
|
if (user == null) {
|
|
|
return new R(53000, MsgCode.U53000);
|
|
|
}
|
|
|
if (!password.equals(user.getPassword())) {
|
|
|
// 密码不相等
|
|
|
- throw new UnauthorizedException();
|
|
|
+ throw new UnauthorizedException("error");
|
|
|
}
|
|
|
- return new R(200, (Object) JWTUtil.sign(username, password));
|
|
|
+
|
|
|
+
|
|
|
+// // 判断用户是否在redis存在的代码可以删除
|
|
|
+// boolean hasKey = redisTemplate.hasKey(user.getUsername());
|
|
|
+//
|
|
|
+// log.info("hasKey: {}", hasKey);
|
|
|
+//
|
|
|
+// // 存在
|
|
|
+// if (hasKey) {
|
|
|
+// //踢出上一个登录用户,注销用户
|
|
|
+//// String oldToken = (String) redisTemplate.opsForValue().get(user.getUsername());
|
|
|
+// log.info("before user is out");
|
|
|
+// }
|
|
|
+
|
|
|
+ // 创建新token
|
|
|
+ String token = JWTUtil.sign(username, password);
|
|
|
+
|
|
|
+ // 更新到 redis, 有效期30min, 旧token无效
|
|
|
+ redisTemplate.opsForValue().set(user.getUsername(), token, Long.parseLong("30"), TimeUnit.MINUTES);
|
|
|
+ log.info("token: {}", token);
|
|
|
+
|
|
|
+ log.warn("end login");
|
|
|
+ return new R(200, (Object) token);
|
|
|
}
|
|
|
|
|
|
|
|
|
@GetMapping("/logout")
|
|
|
- public R logout() {
|
|
|
+ public R logout(HttpServletRequest request) {
|
|
|
log.info("run logout");
|
|
|
- Subject subject = SecurityUtils.getSubject();
|
|
|
- subject.logout();
|
|
|
|
|
|
+ String token = request.getHeader("Authorization");
|
|
|
+ String username = JWTUtil.getUsername(token);
|
|
|
+ redisTemplate.delete(username);
|
|
|
+
|
|
|
+ log.info("end logout");
|
|
|
return new R(200, MsgCode.SUCCESS);
|
|
|
}
|
|
|
|
|
@@ -146,7 +181,7 @@ public class UserController {
|
|
|
@RequestMapping(path = "/401")
|
|
|
@ResponseStatus(HttpStatus.UNAUTHORIZED)
|
|
|
public R unauthorized() {
|
|
|
- return new R(401, "Unauthorized");
|
|
|
+ return new R(40001, "Unauthorized");
|
|
|
}
|
|
|
|
|
|
|