鉴权拦截器-跳转登录页面

当我们校验到没有登录的用户,需要转发到登录页面的时候可以参考

先上核心代码

package com.xxl.job.admin.controller.interceptor;

import org.springframework.web.servlet.AsyncHandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @author jianyongchao
* @date: 2022-05-06 16:07:45
* @company: tianbo
* @description:
* AsyncHandlerInterceptor
*/
public class JycIntercepter implements AsyncHandlerInterceptor {


@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {

//模拟没有登录的情况
String loginUser = null;
if (loginUser == null) {
//声明要跳转
response.setStatus(302);
//声明往哪跳
response.setHeader("location" , request.getContextPath() + "/toLogin");
return false;
}
return AsyncHandlerInterceptor.super.preHandle(request, response, handler);
}
}
---------------------------------------------------------

/**
* index controller
*
* @author jianyongchao
*/
@Controller
public class IndexController {


@Resource
private LoginService loginService;



@RequestMapping("/toLogin")
@PermissionLimit(limit = false)
public ModelAndView toLogin(HttpServletRequest request, HttpServletResponse response, ModelAndView modelAndView) {
//判断下子 没登录就滚一边去(去登录)
if (loginService.ifLogin(request, response) != null) {
modelAndView.setView(new RedirectView("/" , true, false));
return modelAndView;
}
return new ModelAndView("login");
}

@RequestMapping(value = "login" , method = RequestMethod.POST)
@ResponseBody
@PermissionLimit(limit = false)
public ReturnT<String> loginDo(HttpServletRequest request, HttpServletResponse response, String userName, String password, String ifRemember) {
boolean ifRem = (ifRemember != null && ifRemember.trim().length() > 0 && "on".equals(ifRemember)) ? true : false;
return loginService.login(request, response, userName, password, ifRem);
}

@RequestMapping(value = "logout" , method = RequestMethod.POST)
@ResponseBody
@PermissionLimit(limit = false)
public ReturnT<String> logout(HttpServletRequest request, HttpServletResponse response) {
return loginService.logout(request, response);
}

}

-------------------------------------------------------
参考

 

 



posted @ 2022-05-06 16:29  My_blog_s  阅读(481)  评论(0编辑  收藏  举报