我先来说一下简单的配置nginx转发吧。
首先,把我们的tomcat项目配置三个,并行启动在本地。端口分别为:8080、8180、8280
然后,安装nginx,具体步骤如下:
package controller;
import java.util.UUID;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import entity.User;
import redis.clients.jedis.Jedis;
import service.UserService;
import utils.RedisUtil;
import utils.SerializeUtil;
/**
*
* @author Yu Yufeng
*
*/
@Controller
public class LoginController {
@Autowired
private UserService userService;
/**
* 跳转到登录视图
*
* @param session
* @param model
* @return
* @throws Exception
*/
@RequestMapping("/login")
public String loginUI(HttpSession session, Model model) throws Exception {
return "login";
}
@RequestMapping("/quit")
public String quit(HttpSession session, Model model, HttpServletResponse response,
@CookieValue(value = "SESSIONUSER", required = false) String sessionUser) throws Exception {
Cookie cookie = new Cookie("SESSIONUSER", null);
Jedis jedis = RedisUtil.getJedis();
jedis.del(sessionUser.getBytes());
jedis.close();
response.addCookie(cookie);
return "redirect:login";
}
/**
* 跳转到注册视图
*
* @param session
* @param model
* @return
* @throws Exception
*/
@RequestMapping("/register")
public String registerUI(HttpSession session, Model model) throws Exception {
return "register";
}
/**
* 登录提交
*
* @param session
* @param model
* @return
* @throws Exception
*/
@RequestMapping("/login-sub")
public String loginSubmit(HttpSession session, Model model, User user, HttpServletRequest request,
HttpServletResponse response) throws Exception {
if (userService.checkUserPwd(user)) {
User record = userService.getUserByName(user.getUserName());
String uuid = UUID.randomUUID().toString();
Jedis jedis = RedisUtil.getJedis();
jedis.setex(("user_"+uuid).getBytes(), 1800, SerializeUtil.serialize(record));
jedis.close();
Cookie cookie = new Cookie("SESSIONUSER", "user_"+uuid);
response.addCookie(cookie);
return "redirect:user/main";
} else {
model.addAttribute("message", "登录失败,用户名或密码错误!");
return "login";
}
}
@RequestMapping("/register-sub")
public String registerSubmit(HttpSession session, Model model, User user) throws Exception {
User record = null;
try {
record = userService.register(user);
} catch (Exception e) {
model.addAttribute("message", "用户名已存在");
}
if (null != record) {
model.addAttribute("user", record);
model.addAttribute("message", "注册成功");
return "login";
}
return "register";
}
}
package interceptor;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import entity.User;
import redis.clients.jedis.Jedis;
import utils.RedisUtil;
import utils.SerializeUtil;
/**
* 测试拦截器
*
* @author yyf
*
*/
public class UserInterceptor implements HandlerInterceptor {
// 执行Handler完成执行此方法
// 应用场景:统一异常处理,统一日志处理
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object,
Exception exception) throws Exception {
}
// 进入Handler方法之后,返回modelAndView之前执行
// 应用场景从模型出发 公用model数据(菜单导航)在这里传到视图,也可以在这里统一指定视图
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object,
ModelAndView modelAndView) throws Exception {
Cookie[] cs = request.getCookies();
for (Cookie c : cs) {
if (c.getName().equals("SESSIONUSER")) {
Jedis jedis = null;
if (c.getValue() != null && !"".equals(c.getValue())) {
try {
jedis = RedisUtil.getJedis();
byte[] userb = jedis.get((c.getValue()).getBytes());
User user = (User) SerializeUtil.unserialize(userb);
request.setAttribute("session_user", user);
String sCart = "cartNumber_" + c.getValue();
String n = jedis.get(sCart);
if (n != null) {
request.setAttribute("cart_quantity", n);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
jedis.close();
}
}
}
}
}
// 进入Handler方法之前
// 用于身份认真、身份授权
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
Cookie[] cs = request.getCookies();
if (cs == null) {
response.sendRedirect(request.getContextPath() + "/login");
return false;
}
for (Cookie c : cs) {
if (c.getName().equals("SESSIONUSER")) {
if (c.getValue() != null && c.getValue() != null) {
Jedis jedis = null;
try {
jedis = RedisUtil.getJedis();
if (jedis.exists(c.getValue().getBytes())) {
jedis.expire("cartNumber_" + c.getValue(), 1800);// 刷新redis时间
jedis.expire("cart_" + c.getValue(), 1800);// 刷新redis时间
jedis.expire(c.getValue(), 1800);// 刷新redis时间
return true;
} else {
Cookie cook = new Cookie("SESSIONUSER", null);
cook.setMaxAge(0);
response.addCookie(cook);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
jedis.close();
}
}
}
}
response.sendRedirect(request.getContextPath() + "/login");
return false;
}
}