为了账号安全,请及时绑定邮箱和手机立即绑定

Nginx反向代理的问题

Nginx反向代理的问题

白猪掌柜的 2019-01-20 02:40:46
问题:使用Spring Boot开发,然后在服务器上使用Nginx反向代理8080端口,然后问题就出现了,在网站后台登录的时候,用户名和密码都正确,也提示登录成功,但是重定向的时候又回到了登录页,这里是加了拦截器的,直接访问后台的链接也直接回到登录页,感觉就像是登录成功后,session没有创建一样。如果加上8080端口的话就不会出现这样的问题,大家有经验的帮忙看一下,谢谢了。 相关代码: 对登录页面的请求: @GetMapping(value = "/login") public String login(HttpSession session,Model model){ model.addAttribute("options",HaloConst.OPTIONS); User user = (User) session.getAttribute("user"); //如果session存在,跳转到后台首页 if(null!=user){ return "redirect:/admin"; } return "admin/admin_login"; } 登录请求: @PostMapping(value = "/getLogin") @ResponseBody public String getLogin(@ModelAttribute("loginName") String loginName, @ModelAttribute("loginPwd") String loginPwd, HttpSession session){ String status = "false"; try { User aUser = userService.findUser(); User user = null; if("false".equals(aUser.getLoginEnable())){ status = "disable"; }else{ //验证是否是邮箱登录 Pattern patternEmail = Pattern.compile("\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}"); Matcher matcher = patternEmail.matcher(loginName); if(matcher.find()){ user = userService.userLoginByEmail(loginName,HaloUtil.getMD5(loginPwd)).get(0); }else{ user = userService.userLoginByName(loginName,HaloUtil.getMD5(loginPwd)).get(0); } if(aUser==user){ session.setAttribute(HaloConst.USER_SESSION_KEY, user); //重置用户的登录状态为正常 userService.updateUserNormal(); userService.updateUserLoginLast(new Date()); logsService.saveByLogs(new Logs(LogsRecord.LOGIN,LogsRecord.LOGIN_SUCCESS,HaloUtil.getIpAddr(request), HaloUtil.getDate())); status = "true"; } } }catch (Exception e){ Integer errorCount = userService.updateUserLoginError(); if(errorCount>=5){ userService.updateUserLoginEnable("false"); } userService.updateUserLoginLast(new Date()); logsService.saveByLogs(new Logs(LogsRecord.LOGIN,LogsRecord.LOGIN_ERROR+"["+loginName+","+loginPwd+"]",HaloUtil.getIpAddr(request),new Date())); log.error("登录失败!:{0}",e.getMessage()); } return status; } 拦截器: @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { Object obj = request.getSession().getAttribute(HaloConst.USER_SESSION_KEY); //如果user不为空则放行 if(null!=obj){ return true; } //否则拦截并跳转到登录 response.sendRedirect("/admin/login"); return false; } 注册拦截器: @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(loginInterceptor) .addPathPatterns("/admin/**") .excludePathPatterns("/admin/login") .excludePathPatterns("/admin/getLogin") .excludePathPatterns("/static/**"); } nginx配置(使用的宝塔面板配置的反向代理): server { listen 80; server_name 域名; index index.php index.html index.htm default.php default.htm default.html; root /www/wwwroot/halo; #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则 #error_page 404/404.html; #SSL-END #ERROR-PAGE-START 错误页配置,可以注释、删除或修改 error_page 404 /404.html; error_page 502 /502.html; #ERROR-PAGE-END #PHP-INFO-START PHP引用配置,可以注释或修改 #PROXY-START location ~ /purge(/.*) { proxy_cache_purge cache_one $host$request_uri$is_args$args; #access_log /www/wwwlogs/slogc.cc_purge_cache.log; } location / { proxy_pass http://ip:8090; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header REMOTE-HOST $remote_addr; #缓存相关配置 #proxy_cache cache_one; #proxy_cache_key $host$request_uri$is_args$args; #proxy_cache_valid 200 304 301 302 1h; #持久化连接相关配置 #proxy_connect_timeout 30s; #proxy_read_timeout 86400s; #proxy_send_timeout 30s; #proxy_http_version 1.1; #proxy_set_header Upgrade $http_upgrade; #proxy_set_header Connection "upgrade"; #add_header X-Cache $upstream_cache_status; expires 12h; } location ~ .*\.(php|jsp|cgi|asp|aspx|flv|swf|xml)?$ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header REMOTE-HOST $remote_addr; proxy_pass http://123.207.101.207:8090; } }
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 507 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信