Apply filter in Java to control permissions and sessions

Source: Internet
Author: User
Filter is used to prevent users from accessing some unauthorized resources. For example, a user is not allowed to access some pages of the website without logon, and the page is redirected to the page requiring user login, the following is a related example:

Package com. DRP. util. filter;

Import java. Io. ioexception;

Import javax. servlet. filter;
Import javax. servlet. filterchain;
Import javax. servlet. filterconfig;
Import javax. servlet. servletexception;
Import javax. servlet. servletrequest;
Import javax. servlet. servletresponse;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import javax. servlet. http. httpsession;

Public class authfilter implements filter {

Public void destroy (){

}

Public void dofilter (servletrequest, servletresponse,
Filterchain) throws ioexception, servletexception {
/** 1. The first parameter of the dofilter method is the servletrequest object. This object provides the filter with access information (including
* Full Access to form data, cookies, and HTTP request headers. The second parameter is servletresponse.
* Ignore this parameter in filter. The last parameter is filterchain, which is used to call servlet or JSP pages.
*/

Httpservletrequest request = (httpservletrequest) servletrequest;
/** If an HTTP request is processed and you need to access such as getheader or getcookies in servletrequest
* If the method is not available, construct the request object as httpservletrequest.
*/
Httpservletresponse response = (httpservletresponse) servletresponse;

String currenturl = request. getrequesturi (); // obtain the absolute path of the root directory:

String TargetUrl = currenturl. substring (currenturl. indexof ("/", 1 ),
Currenturl. Length (); // capture the current file name for comparison

Httpsession session = request. getsession (false );

If (! "/Login. jsp". Equals (TargetUrl )){
// Determine whether the current page is a redirected logon page. If yes, the session will not be judged to prevent endless loops.
If (session = NULL | session. getattribute ("user") = NULL ){
// * You must manually add a session after logging on.
System. Out. println ("request. getcontextpath () =" + request. getcontextpath ());
Response. sendredirect (request. getcontextpath () + "/login. jsp ");
// If the session is empty, the user is redirected to the login. jsp page without logon.
Return;
}
}
// Add the filter chain to continue the downstream execution
Filterchain. dofilter (request, response );
/** Call the dofilter method of the filterchain object. The dofilter method of the filter interface obtains a filterchain object
* Is a parameter. When the dofilter method of this object is called, The next related filter is activated. If no other
* If a filter is associated with a servlet or JSP page, the servlet or JSP page is activated.
*/
}

Public void Init (filterconfig) throws servletexception {

}
}

Add the following in the configuration file web. xml: (Please note that filtering is first introduced in Serlvet 2.3. Therefore, the Web. xml file must use a version later than DTD 2.3 .)

<Filter>
<Filter-Name> authfilter </filter-Name>
<Filter-class> com. DRP. util. Filter. authfilter </filter-class>
</Filter>

<Filter-mapping>
<Filter-Name> authfilter </filter-Name>
<URL-pattern> *. jsp </url-pattern> // indicates that it is valid for all JSP files.
</Filter-mapping>

In this way, the user will go to the logon page without logon.

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.