Logon Interception Function
1. Configure filter in web. xml (put it after the character set filter, otherwise the character filter will fail)
<! -- Logon interception --> <filter> <display-name> LoginFilter </display-name> <filter-name> LoginFilter </filter-name> <filter-class> com. xxx. common. loginFilter </filter-class> </filter> <filter-mapping> <filter-name> LoginFilter </filter-name> <url-pattern>/* </url-pattern> </filter-mapping>
2.interceptor class (optional, registration, static file .js.css, etc)
Package com. xxx. common; import java. io. IOException; import java. io. writer; import javax. servlet. filter; import javax. servlet. filterChain; import javax. servlet. filterConfig; import javax. servlet. servletContext; import javax. servlet. servletException; import javax. servlet. servletRequest; import javax. servlet. servletResponse; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletRespo Neuron; import net. sf. json. JSONObject; import org. springframework. beans. factory. annotation. autowired; import org. springframework. context. applicationContext; import org. springframework. stereotype. component; import org. springframework. web. context. support. webApplicationContextUtils; import com. xxx. entity. user; import com. xxx. service. loginService; import com. xxx. util. commonUtil; public class LoginFilter implement S Filter {private LoginService loginService; public LoginService getLoginService () {return loginService;} public void setLoginService (LoginService loginService) {this. loginService = loginService;} public LoginFilter () {}/*** initialize */public void init (FilterConfig config) throws ServletException {ServletContext context = config. getServletContext (); ApplicationContext ctx = WebApplicationCont ExtUtils. getWebApplicationContext (context); loginService = (LoginService) ctx. getBean (LoginService. class);} public void destroy () {loginService = null;}/*** logon interception */public void doFilter (ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, servletException {HttpServletRequest httpRequest = (HttpServletRequest) req; HttpServletResponse httpResponse = (HttpServletRes Ponse) res; String path = CommonUtil. getRequestURL (httpRequest); if (path. indexOf ("/tologin ")! =-1 | path. indexOf ("/login ")! =-1 | path. indexOf ("/include ")! =-1) {chain. doFilter (req, res);} else {User user = loginService. getCurrentUser (); if (user = null) {boolean isAjaxRequest = isAjaxRequest (httpRequest); if (isAjaxRequest) {httpResponse. setCharacterEncoding ("UTF-8"); Writer out = httpResponse. getWriter (); JSONObject jsonObj = new JSONObject (); jsonObj. put ("success", false); jsonObj. put ("code", "noLogin"); jsonObj. put ("message", "Please log on to the system first! "); Out. write (jsonObj. toString (); out. flush (); out. close ();} else {httpResponse. sendRedirect ("/Project path/Login/tologin") ;}} else {chain. doFilter (req, res) ;}}/ *** determines whether the request is an Ajax request ** @ param request * HttpServletRequest * @ return is true, no false */public static boolean isAjaxRequest (HttpServletRequest request) {String requestType = request. getHeader ("X-Requested-With"); return requestType! = Null & "XMLHttpRequest". equals (requestType );}}