URL Security (filter)

Source: Internet
Author: User

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

 

Related Article

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.