Prevent users from directly accessing URL permission control (using filters)

Source: Internet
Author: User

This is the content of a filter,


[Java]
Public void dofilter (servletrequest request, servletresponse response,
Filterchain chain) throws ioexception, servletexception {
Httpservletrequest Req = (httpservletrequest) request;
Httpservletresponse resp = (httpservletresponse) response;
String constring = "";
Constring = req. getheader ("Referer"); // get the parent URL -- if it is not directly entered, it is the previously accessed page. If it is entered by the user, this parent URL does not exist.
If ("". Equals (constring) | null = constring) {// if the previous directory is empty, the user directly enters the URL for access.
String servletpath = Req. getservletpath (); // the current request URL, removing several pages that can be accessed directly.
If (servletpath. Contains ("index. jsp") | servletpath. Contains ("admin/login. jsp") {// skip index. jsp and log on to login. jsp
Chain. dofilter (request, response );
} Else {
Resp. sendredirect ("/ejuornal/index. jsp"); // jump back to the homepage
}
} Else {
Chain. dofilter (request, response );
}
}

Public void dofilter (servletrequest request, servletresponse response,
Filterchain chain) throws ioexception, servletexception {
Httpservletrequest Req = (httpservletrequest) request;
Httpservletresponse resp = (httpservletresponse) response;
String constring = "";
Constring = req. getheader ("Referer"); // get the parent URL -- if it is not directly entered, it is the previously accessed page. If it is entered by the user, this parent URL does not exist.
If ("". Equals (constring) | null = constring) {// if the previous directory is empty, the user directly enters the URL for access.
String servletpath = Req. getservletpath (); // the current request URL, removing several pages that can be accessed directly.
If (servletpath. Contains ("index. jsp") | servletpath. Contains ("admin/login. jsp") {// skip index. jsp and log on to login. jsp
Chain. dofilter (request, response );
} Else {
Resp. sendredirect ("/ejuornal/index. jsp"); // jump back to the homepage
}
} Else {
Chain. dofilter (request, response );
}
} The following is the configuration file of the filter.


[HTML]
<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: Web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
Version = "2.5">
<Display-Name> </display-Name>
<Welcome-file-List>
<Welcome-File> index. jsp </welcome-File>
</Welcome-file-List>
<Filter>
<Filter-Name> filterpages </filter-Name>
<Filter-class> com. ejuornal. Filter. filterpages </filter-class>
</Filter>
<Filter-mapping>
<Filter-Name> filterpages </filter-Name>
<URL-pattern> *. jsp </url-pattern>
</Filter-mapping>
</Web-app>

<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: Web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
Version = "2.5">
<Display-Name> </display-Name>
<Welcome-file-List>
<Welcome-File> index. jsp </welcome-File>
</Welcome-file-List>
<Filter>
<Filter-Name> filterpages </filter-Name>
<Filter-class> com. ejuornal. Filter. filterpages </filter-class>
</Filter>
<Filter-mapping>
<Filter-Name> filterpages </filter-Name>
<URL-pattern> *. jsp </url-pattern>
</Filter-mapping>
</Web-app> This will jump back to the home page when you directly enter the URL.

 


However, there are two issues that need attention. The usage of chain. dofilter (request, response); in the filter ---------- error is as follows:


[HTML]
Public void dofilter (servletrequest request, servletresponse response,
Filterchain chain) throws ioexception, servletexception {
Httpservletrequest Req = (httpservletrequest) request;
Httpservletresponse resp = (httpservletresponse) response;
String constring = "";
Constring = Req. getheader ("Referer"); // obtain the parent URL
If ("". Equals (constring) | null = constring ){
String servletpath = Req. getservletpath (); // the current request URL
If (servletpath. Contains ("index. jsp") | servletpath. Contains ("admin/login. jsp ")){
Chain. dofilter (request, response );
} Else {
Resp. sendredirect ("/ejuornal/index. jsp ");
}
}
<Span style = "color: # ff0000"> chain. dofilter (request, response); </span>
}

Public void dofilter (servletrequest request, servletresponse response,
Filterchain chain) throws ioexception, servletexception {
Httpservletrequest Req = (httpservletrequest) request;
Httpservletresponse resp = (httpservletresponse) response;
String constring = "";
Constring = Req. getheader ("Referer"); // obtain the parent URL
If ("". Equals (constring) | null = constring ){
String servletpath = Req. getservletpath (); // the current request URL
If (servletpath. Contains ("index. jsp") | servletpath. Contains ("admin/login. jsp ")){
Chain. dofilter (request, response );
} Else {
Resp. sendredirect ("/ejuornal/index. jsp ");
}
}
Chain. dofilter (request, response );
}
If this is done, it will be as follows:

 

 
 


Two pages overlap because two chains. dofilter (request, response); are executed.

That is to say, executing chain. dofilter (request, response); is the execution of a request.

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.