package com.pjb.springbootjjwt.api; import ch.qos.logback.core.net.SyslogOutputStream; import com.alibaba.fastjson.JSONObject; import com.pjb.springbootjjwt.annotation.UserLoginToken; import com.pjb.springbootjjwt.entity.User; import com.pjb.springbootjjwt.service.TokenService; import com.pjb.springbootjjwt.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; @RestController //@RequestMapping("api") public class UserApi { @Autowired UserService userService; @Autowired TokenService tokenService; //登录 @GetMapping("/getToken") public Object login(HttpServletRequest request){ String username = request.getParameter("username"); String password = request.getParameter("password"); String app_secret = request.getParameter("app_secret"); System.out.println("username=>"+username); JSONObject jsonObject=new JSONObject(); User userForBase=userService.findByUsername(username); if(userForBase==null){ jsonObject.put("message","登录失败,用户不存在"); jsonObject.put("success", "1"); return jsonObject; }else { System.out.println("userForBase=>"+userForBase.toString()); if (!userForBase.getPassword().equals(password)){ jsonObject.put("message","登录失败,密码错误"); jsonObject.put("success", "1"); return jsonObject; } if (!userForBase.getApp_secret().equals(app_secret)){ jsonObject.put("message","登录失败,secret验证错误"); jsonObject.put("success", "1"); return jsonObject; } String token = tokenService.getToken(userForBase); jsonObject.put("token", token); jsonObject.put("message","获取token成功"); jsonObject.put("success", "0"); return jsonObject; } } }