项目名称:中国电信运营支持系统-网络版(十一)
登录检查
如果不做登录检查,会导致任何非法请求都可登录系统,系统安全系会很差。
一般登录检查都会使用到过滤器Filter。基于此我们添加此功能。
案例演示:
工程案例目录结构
web.xml
<?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" 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>netctoss</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>main</servlet-name>
<servlet-class>web.MainServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>main</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 每页显示行数 -->
<context-param>
<param-name>size</param-name>
<param-value>5</param-value>
</context-param>
<!--
错误页面:当服务器捕获到此类异常时,
它会自动转发到对应的错误页面。
服务器在做此转发行为时,会自动补充项目名,
因此转发页面的绝对路径不需要加项目名了。
-->
<!-- 1.指定某类型的异常对应某错误页面 -->
<!-- <error-page>
<exception-type>
java.lang.Exception
</exception-type>
<location>/WEB-INF/error.jsp</location>
</error-page> -->
<!-- 2.指定某编号的错误对应某错误页面 -->
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/error.jsp</location>
</error-page>
<error-page>
<error-code>405</error-code>
<location>/WEB-INF/error.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/error.jsp</location>
</error-page>
<!-- 登录检查过滤器 -->
<filter>
<filter-name>login</filter-name>
<filter-class>web.LoginFilter</filter-class>
<!-- 声明不需要检查的路径 -->
<init-param>
<param-name>ignorePath</param-name>
<param-value>
/toLogin.do,/createImg.do,/login.do
</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>login</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
</web-app>
LoginFilter.java
package web;
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;
/**
* 登录检查过滤器:
* 在所以的(不包括登录相关的请求)请求前,
* 判断用户是否已登录,若已登录则请求继续,
* 否则拒绝用户的请求,重定向到登录页。
* @author Cher_du
*
*/
public class LoginFilter implements Filter{
private String[] paths;
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest req,
ServletResponse res, FilterChain chain)
throws IOException, ServletException {
//Tomcat传入的参数是HTTP相关参数,
//就是当前参数的子类型,可以强转。
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)res;
//删除掉不需要校验的请求
// String[] paths = new String[]{
// "/toLogin.do",
// "/createImg.do",
// "/login.do"
// };
for(String path:paths){
if(path.equals(request.getServletPath())){
//如果当前路径等于排查的路径,
//则当前路径不需要检验了,放行
chain.doFilter(req, res);
return;
}
}
System.out.println("过滤放行。。。下面将校验登录状态。。");
//从session中获取adminCode,
//以此来判断用户是否已登录。
HttpSession session = request.getSession();
Object adminCode= session.getAttribute("adminCode");
if(adminCode == null){
System.out.println("未登录。。。");
//未登录,重定向到登录页
response.sendRedirect(request.getContextPath()+"/toLogin.do");
}else{
System.out.println("已登录。。。");
//已登录,请求继续
chain.doFilter(req,res);
System.out.println("登录状态过滤后。。。");
}
}
public void init(FilterConfig cfg) throws ServletException {
String ignorePath = cfg.getInitParameter("ignorePath");
paths =ignorePath.split(",");
}
}
将netctoss工程部署到Tomcat上,运行Tomcat启动案例工程,
浏览器录入http://localhost:8088/netctoss/findCost.do【错误的请求路径】即可:如果没有错误,最终页面显示效果应如下图:
由于处于未登录状态 所以被重定向到登录页
若已登录后,再次录入http://localhost:8088/netctoss/findCost.do请求 则正常访问资费查询页
对应【退出】按钮旁边的账号 caocao 在每个功能页添加如下账号信息info.jsp页面即可
info.jsp
<%@page pageEncoding="utf-8"%>
<span>${cookie.adminCode.value }</span>
<a href="/netctoss/logout.do">[退出]</a>
findCost.jsp、add.jsp、update.jsp 查询页面的 Logo区域 添加 info.jsp
点击退出按钮的请求 logout.do 对应后台
MainServlet.java 中添加红线圈中的代码即可:
点击退出按钮后即可退出重定向到登录页面:
A. A
B.
拓展:
在JSP上获取项目名
JSP表达式
- <%=request.getContextPath()%>
- 修改info.jsp
EL表达式
- ${contextPath} -> request.getAttribute("contextPath")
- ${pageContext.request} -> request
- ${pageContext.request.contextPath}
-
默认情况下EL为调用getAttribute(),获得对象后再访问bean属性(get方法)。
因此若EL为contextPath则等价于request.getAttribute("contextPath")。那么就需要先获取request,然后访问它的属性(contextPath)-> request.getContextPath();
修改MainServlet与info.jsp 即可使用此EL表达式指定基础路径
MainServlet.java
info.jsp