Use Filter in java to control user logon permissions.

Source: Internet
Author: User

After learning jsp for such a long time, there are also seven or eight projects, but all the projects will jump directly to the page with permissions when users log on, or display accessible page links. This method is used to control the access permission naively. I never thought that if I did not log on, I could directly enter the address to access the user's page.

In jsp, permission control is implemented through the Filter. Filter is integrated in all development frameworks. If the development framework is not applicable, the following implementation methods are available:

LoginFilter. java

Copy codeThe Code is 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 ("---> log on ");

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 (",");

}

}

}

Web. xml

Copy codeThe Code is 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 filters user logon. The permission filtering principle is the same. You only need to replace the user's logon status with the user's permissions!

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.