| | |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | import com.linghu.utils.OpenCryptUtil; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestBody; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RequestParam; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import com.linghu.model.entity.User; |
| | | import com.linghu.utils.JwtUtils; |
| | |
| | | @Value("${jwt.secret}") |
| | | private String secretKey; |
| | | |
| | | @PostMapping("/login") |
| | | @ApiOperation(value = "外部登录") |
| | | public ResponseEntity<?> externalLogin( |
| | | @RequestBody User user) { |
| | | |
| | | // 生成JWT令牌 |
| | | JwtUtils jwtUtils = new JwtUtils(secretKey, 3600); |
| | | String token = jwtUtils.generateToken(user); |
| | | |
| | | Map<String, String> response = new HashMap<>(); |
| | | response.put("token", token); |
| | | // 返回JWT令牌 |
| | | return ResponseEntity.ok(response); |
| | | } |
| | | // @PostMapping("/login") |
| | | // @ApiOperation(value = "外部登录") |
| | | // public ResponseEntity<?> externalLogin( |
| | | // @RequestBody User user) { |
| | | // |
| | | // // 生成JWT令牌 |
| | | // JwtUtils jwtUtils = new JwtUtils(secretKey, 3600); |
| | | // String token = jwtUtils.generateToken(user); |
| | | // |
| | | // Map<String, String> response = new HashMap<>(); |
| | | // response.put("token", token); |
| | | // // 返回JWT令牌 |
| | | // return ResponseEntity.ok(response); |
| | | // } |
| | | |
| | | // 获取用户信息 |
| | | @PostMapping("/getUserInfo") |
| | | @ApiOperation(value = "获取用户信息") |
| | | public ResponseEntity<?> getUserInfo(@RequestParam String token) { |
| | | // 解析JWT令牌,获取用户信息 |
| | | JwtUtils jwtUtils = new JwtUtils(secretKey, 3600); |
| | | User user = jwtUtils.parseToken(token); |
| | | // @PostMapping("/getUserInfo") |
| | | // @ApiOperation(value = "获取用户信息") |
| | | // public ResponseEntity<?> getUserInfo(@RequestParam String token) { |
| | | // // 解析JWT令牌,获取用户信息 |
| | | // JwtUtils jwtUtils = new JwtUtils(secretKey, 3600); |
| | | // User user = jwtUtils.parseToken(token); |
| | | // // 返回用户信息 |
| | | // return ResponseEntity.ok(user); |
| | | // } |
| | | //open-crypt解析token |
| | | @GetMapping("/parseToken") |
| | | @ApiOperation(value = "解析token") |
| | | public ResponseEntity<?> parseToken(@RequestParam String token) { |
| | | if (token == null || "".equals(token)){ |
| | | return ResponseEntity.ok("token为空"); |
| | | } |
| | | // 解析token,获取用户信息 |
| | | OpenCryptUtil openCryptUtil = new OpenCryptUtil(); |
| | | String decrypt = openCryptUtil.decrypt(token); |
| | | System.out.println(decrypt); |
| | | System.out.println("----------------"); |
| | | |
| | | // 返回用户信息 |
| | | return ResponseEntity.ok(user); |
| | | return ResponseEntity.ok(decrypt); |
| | | } |
| | | |
| | | @GetMapping("/getToken") |
| | | @ApiOperation(value = "获取token") |
| | | public ResponseEntity<?> getToken( String user) { |
| | | // 创建用户对 |
| | | |
| | | // 创建JwtUtils对象,并生成JWT令牌 |
| | | OpenCryptUtil openCryptUtil = new OpenCryptUtil(); |
| | | String decrypt = openCryptUtil.encrypt(user); |
| | | // 返回JWT令牌 |
| | | return ResponseEntity.ok(decrypt); |
| | | } |
| | | } |