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!