問題:使用Spring Boot開發(fā),然后在服務(wù)器上使用Nginx反向代理8080端口,然后問題就出現(xiàn)了,在網(wǎng)站后臺登錄的時候,用戶名和密碼都正確,也提示登錄成功,但是重定向的時候又回到了登錄頁,這里是加了攔截器的,直接訪問后臺的鏈接也直接回到登錄頁,感覺就像是登錄成功后,session沒有創(chuàng)建一樣。如果加上8080端口的話就不會出現(xiàn)這樣的問題,大家有經(jīng)驗的幫忙看一下,謝謝了。
相關(guān)代碼:
對登錄頁面的請求:
@GetMapping(value = "/login")
public String login(HttpSession session,Model model){
model.addAttribute("options",HaloConst.OPTIONS);
User user = (User) session.getAttribute("user");
//如果session存在,跳轉(zhuǎn)到后臺首頁
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);
//重置用戶的登錄狀態(tài)為正常
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;
}
//否則攔截并跳轉(zhuǎn)到登錄
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相關(guān)配置,請勿刪除或修改下一行帶注釋的404規(guī)則
#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;
#緩存相關(guān)配置
#proxy_cache cache_one;
#proxy_cache_key $host$request_uri$is_args$args;
#proxy_cache_valid 200 304 301 302 1h;
#持久化連接相關(guān)配置
#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;
}
}
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。