When creating a java web application, it often involves three URL security issues:
1. If you have not logged on, enter the corresponding URL to go to the page;
2. For permission issues, common users can also access the Administrator page without permission restrictions.
3. When the seesion fails, the refresh will fail.
4. Prevent Users From directly connecting to a resource on my website from other website URLs, or certain pages must be passed in from a page. directly entering a url may result in missing data and an error. In this case, you can view the url of the previous page of the request to determine. The statement is as follows: httpRequest. getHeader ("referer ")This statement can obtain the url of the previous page.
The solution is to configure a filter to filter requests:
The corresponding solution is:
1. Solutions for points 1 and 3 and 4:
Configure the user logon filter to check whether a user exists in the session. The implementation is as follows:
@ WebFilter (filterName = "loginFilter", urlPatterns = {"*. do "LoginFilter String [] exit_url = {"/login/index. do ","/login/logOff. do "}; doFilter (ServletRequest request, ServletResponse response, FilterChain chain) HttpServletRequest httpRequest = HttpServletResponse httpResponse = String url = System. out. println (url + ": do login Filer! "HttpSession session = isExit = (url. indexOf (u)> = 0 isExit = (url. contains ("/login/verify. do "(httpRequest. getHeader ("referer ")! = & HttpRequest. getHeader ("referer "). contains ("/login/index. do "isExit =}{ httpResponse. sendRedirect ("/managerSystem/login/index. do "(! IsExit & session. getAttribute ("user") = httpResponse. sendRedirect ("/managerSystem/login/index. do ";}View Code
2. Solution to the second point:
Configure the permission management Filter
@ WebFilter (filterName = "permitFilter", urlPatterns = {"*. do "PermitFilter String [] user_canot_access_url = {" login/adminHomePage. do "," signature/adminUserSignature. do "," user/adminUserManager. do "doFilter (ServletRequest request, ServletResponse response, FilterChain chain) HttpServletRequest httpRequest = HttpServletResponse httpResponse = String uri = System. out. println (uri + ": do permit Filer! "HttpSession session = User user = (User) session. getAttribute (" user "(user! = & User. getUser_type (). equals ("user" httpResponse. sendRedirect ("/managerSystem/login/index. do "}View Code