A specific instance of using filter to control user logon rights in Java _java

Source: Internet
Author: User

Learn JSP so long time, do the project also has seven or eight, can all the project is the user login directly to its own permissions of the page, or show access to the page link. Use this approach to control access rights naively. Never thought that if I did not log in, the direct input address can also directly access the user's page.

The control of permissions in the JSP is implemented through filter filters, and all development frameworks are integrated with filter, if the development framework is not applicable to the following implementation methods:

Loginfilter.java

Copy Code code as follows:

public class Loginfilter implements Filter {

Private String permiturls[] = null;

Private String gotourl = null;

public void Destroy () {

TODO auto-generated Method Stub

Permiturls = null;

Gotourl = null;

}

public void Dofilter (ServletRequest request, servletresponse response,

Filterchain chain) throws IOException, Servletexception {

TODO auto-generated Method Stub

HttpServletRequest res= (httpservletrequest) request;

HttpServletResponse resp= (httpservletresponse) response;

if (!ispermiturl (request)) {

if (Filtercurrurl (request)) {

SYSTEM.OUT.PRINTLN ("---> Please login");

Resp.sendredirect (Res.getcontextpath () +gotourl);

Return

}

}

SYSTEM.OUT.PRINTLN ("---> Allow access");

Chain.dofilter (request, response);

}

public boolean Filtercurrurl (ServletRequest request) {

Boolean filter=false;

HttpServletRequest res= (httpservletrequest) request;

User user = (user) res.getsession (). getattribute ("user");

if (Null==user)

Filter=true;

return filter;

}

public boolean Ispermiturl (ServletRequest request) {

Boolean ispermit = false;

String Currenturl = currenturl (request);

if (permiturls!= null && permiturls.length > 0) {

for (int i = 0; i < permiturls.length; i++) {

if (Permiturls[i].equals (Currenturl)) {

Ispermit = true;

Break

}

}

}

return ispermit;

}

Request Address

Public String Currenturl (ServletRequest request) {

HttpServletRequest res = (httpservletrequest) request;

String task = Request.getparameter ("task");

String path = Res.getcontextpath ();

String uri = Res.getrequesturi ();

if (task!= null) {//URI format Xx/ser

URI = uri.substring (Path.length (), Uri.length ()) + "?" + "task="

+ task;

} else {

URI = uri.substring (Path.length (), uri.length ());

}

SYSTEM.OUT.PRINTLN ("Current request Address:" + URI);

return URI;

}

public void init (Filterconfig filterconfig) throws Servletexception {

TODO auto-generated Method Stub

String permiturls = Filterconfig.getinitparameter ("Permiturls");

String Gotourl = Filterconfig.getinitparameter ("Gotourl");

This.gotourl = Gotourl;

if (permiturls!= null && permiturls.length () > 0) {

This.permiturls = Permiturls.split (",");

}

}

}


Xml

Copy Code code as follows:

<filter>

<filter-name>loginFilter</filter-name>

<filter-class>filter. Loginfilter</filter-class>

<init-param>

<param-name>ignore</param-name>

<param-value>false</param-value>

</init-param>

<init-param>

<param-name>permitUrls</param-name>

<param-value>/,/servlet/Loginservlet?task=login,/public.jsp,/login.jsp</param-value>

</init-param>

<init-param>

<param-name>gotoUrl</param-name>

<param-value>/login.jsp</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>loginFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

This short code mainly implements the user login filtering, the right filter principle is same. Only need to judge whether or not the user login to have permission on it!

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.