Logon Interception Function

Source: Internet
Author: User

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 );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.