登录token
This commit is contained in:
@@ -1,43 +1,56 @@
|
||||
package com.lovenav.controller;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.lovenav.entity.User;
|
||||
import com.lovenav.service.UserService;
|
||||
import com.lovenav.utils.MD5Utils;
|
||||
import com.lovenav.utils.RandomValidateCode;
|
||||
import com.lovenav.utils.TokenUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import javax.websocket.Session;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
public class UserController {
|
||||
@Autowired
|
||||
UserService userService;
|
||||
@Autowired
|
||||
TokenUtils tokenUtils;
|
||||
//发送邮箱验证码
|
||||
@GetMapping("/sendActiveCode")
|
||||
public String sendActiveCode(HttpSession session, User user){
|
||||
String activecode=userService.sendEmailActivecode(user);
|
||||
session.setAttribute("realactivecode",activecode);
|
||||
session.setAttribute(user.getUserEmail(),activecode);
|
||||
return "发送验证码成功!";
|
||||
}
|
||||
@RequestMapping("/register")
|
||||
public String userRegister(HttpSession session,User user){
|
||||
|
||||
// 比较验证码
|
||||
if (!user.getActiveCode().equalsIgnoreCase((String) session.getAttribute("realactivecode"))) {
|
||||
if (!user.getActiveCode().equals((String) session.getAttribute(user.getUserEmail()))) {
|
||||
return "验证码不正确";
|
||||
}
|
||||
System.out.println(user.getUserLogin());
|
||||
System.out.println(user.getUserEmail());
|
||||
// 用户注册之前根据用户名称查询该用户是否存在如果不存在的情况下才可以注册 如果存在的话就无法注册
|
||||
User user1=userService.selectUserAlreadyExist(user);
|
||||
System.out.println("zhuce");
|
||||
|
||||
if (user1 != null) {
|
||||
return "用户名: " + user1.getUserEmail() + "已被注册!";
|
||||
}
|
||||
//用户数据注册
|
||||
|
||||
int register = userService.UserRegister(user);
|
||||
if (register <= 0) {
|
||||
// 注册失败了 //转发到错误页面
|
||||
@@ -45,5 +58,47 @@ public class UserController {
|
||||
}
|
||||
return "注册成功!";
|
||||
}
|
||||
@RequestMapping("/login")
|
||||
public Map<String,Object>login(User user,String code,HttpSession session){
|
||||
Map<String,Object> map=new HashMap<>();
|
||||
String sessionCode = (String) session.getAttribute(RandomValidateCode.RANDOMVALIDATECODE);
|
||||
if (!sessionCode.equals(code)){
|
||||
map.put("msg","验证码错误");
|
||||
return map;
|
||||
}
|
||||
map.put("code",0);
|
||||
if(StringUtils.isEmpty(user.getUserLogin())||StringUtils.isEmpty(user.getUserPassword())){
|
||||
map.put("msg","用户或密码为空!");
|
||||
return map;
|
||||
}
|
||||
|
||||
User user1 = userService.userLogin(user);
|
||||
|
||||
if(user1!=null){
|
||||
|
||||
String token= tokenUtils.sign(user1);
|
||||
map.put("cod",1);
|
||||
map.put("data",user1);
|
||||
map.put("token",token);
|
||||
}else {
|
||||
map.put("msg","用户名或密码错误!");
|
||||
}
|
||||
return map;
|
||||
}
|
||||
/*图片验证码*/
|
||||
@RequestMapping("/verifyCode")
|
||||
public void verifyCode(HttpServletRequest request,HttpServletResponse response){
|
||||
response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
|
||||
response.setHeader("Pragma", "No-cache");//设置响应头信息,告诉浏览器不要缓存此内容
|
||||
response.setDateHeader("Expire", 0);
|
||||
RandomValidateCode randomValidateCode = new RandomValidateCode();
|
||||
try {
|
||||
randomValidateCode.getRandcode( request, response);//输出图片方法
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user