拦截器

This commit is contained in:
Zhang Liguo 2023-12-22 11:47:07 +08:00
parent 39048bbcfc
commit a35f4a6527
6 changed files with 36 additions and 9 deletions

View File

@ -0,0 +1,14 @@
package com.lovenav.configuration;
import com.lovenav.filter.UserInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyWebConfigurer implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new UserInterceptor()).addPathPatterns("/*/**").excludePathPatterns("/error","/login","/register","/findThePassword","/verifyCode","/sendActiveCode");
WebMvcConfigurer.super.addInterceptors(registry);
}
}

View File

@ -32,6 +32,7 @@ public class UserController {
//发送邮箱验证码
@GetMapping("/sendActiveCode")
public String sendActiveCode(HttpSession session, User user){
String activecode=userService.sendEmailActivecode(user);
session.setAttribute(user.getUserEmail(),activecode);
return "发送验证码成功!";
@ -58,10 +59,15 @@ public class UserController {
}
return "注册成功!";
}
@RequestMapping("/login")
@RequestMapping(value = "/login",produces = {"application/json;charset=UTF-8"})
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==null){
map.put("msg","验证码空的");
return map;
}
if (!sessionCode.equals(code)){
map.put("msg","验证码错误");
return map;
@ -109,7 +115,9 @@ public class UserController {
@RequestMapping("/findThePassword")
public Map<String,Object> findThePassword(User user,HttpSession session){
Map<String,Object> map=new HashMap<>();
User user1=userService.selectUserAlreadyExist(user);
System.out.println(user1);
if (user1==null){
map.put("msg","无此邮箱");
return map;

View File

@ -23,7 +23,7 @@ public interface UserDao {
User selectByEmail(String user_email);
User selectByUserLogin(String user_Login);
User selectByUserLogin(String user_login);
int updateByEmail(User user);

View File

@ -1,18 +1,19 @@
package com.lovenav.filter;
import com.alibaba.fastjson.JSON;
import com.lovenav.utils.TokenUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
public class LoginInterceptor implements HandlerInterceptor {
public class UserInterceptor implements HandlerInterceptor {
//Controller逻辑执行之前
@ -31,22 +32,25 @@ public class LoginInterceptor implements HandlerInterceptor {
// 拦截每个请求
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) {
Map<String,Object> map=new HashMap<>();
System.out.println("开始进入拦截器检验jwt头部是否含有Authorization方法");
// 通过url得到token请求头是否包含Authorization
response.setCharacterEncoding("UTF-8");
String jwt = request.getHeader("Authorization");
System.out.println(jwt);
try {
// 检测请求头是否为空
if (jwt == null) {
System.out.println("用户未登录,验证失败");
map.put("msg","用户未登录,验证失败");
response.getWriter().print(JSON.toJSONString(map));
return false;
} else {
Claims c = tokenUtils.parseJWT(jwt);
System.out.println("用户[ " + c.get("username") + " ]已是登录状态");
System.out.println("用户[ " + c.get("userLogin") + " ]已是登录状态");
System.out.println("结束进入拦截器检验jwt头部是否含有Authorization方法");
return true;
}
System.out.println("token解析错误验证失败");
response.getWriter().write("未登录,请重新登录后操作");
} catch (Exception e) {
e.printStackTrace();
}

View File

@ -80,6 +80,7 @@ public class UserServiceImpl implements UserService {
@Override
public User selectUserAlreadyExist(User user) {
System.out.println(user.getUserEmail());
User user1=userDao.selectByEmail(user.getUserEmail());
return user1;
}

View File

@ -27,7 +27,7 @@ public class EmailUtils {
//邮箱 lx_teach@163.com
String myAccount = "482370576@qq.com";
//授权码 java168
String myPass = "rgggscegbprzbhcc";
String myPass = "hlilinclupppbice";
//邮箱服务器
String SMTPHost = "smtp.qq.com";
//设置属性信息