You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
2.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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;
}
}
}