单应用登录过滤器

编程

判断 url 是否为 登录页面,是则放行

                                         否:获取session,存在 放行,不存在拦截

 2.   税务系统登录:

public class LoginFilter implements Filter {

@Override

public void destroy() {

}

@Override

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest)servletRequest;

HttpServletResponse response = (HttpServletResponse)servletResponse;

String uri = request.getRequestURI();

//判断当前请求地址是否是登录的请求地址

if(!uri.contains("sys/login_")){

//非登录请求

if(request.getSession().getAttribute(Constant.USER) != null){

//说明已经登录过

//判断是否访问纳税服务系统

if(uri.contains("/nsfw/")){

//访问纳税服务子系统

User user = (User)request.getSession().getAttribute(Constant.USER);

//获取spring容器

WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());

PermissionCheck pc = (PermissionCheck)applicationContext.getBean("permissionCheck");

if(pc.isAccessible(user, "nsfw")){

//说明有权限,放行

chain.doFilter(request, response);

} else {

//没有权限,跳转到没有权限提示页面

response.sendRedirect(request.getContextPath() + "/sys/login_toNoPermissionUI.action");

}

} else {

//非访问纳税服务子系统,则直接放行

chain.doFilter(request, response);

}

} else {

//没有登录,跳转到登录页面

response.sendRedirect(request.getContextPath() + "/sys/login_toLoginUI.action");

}

} else {

//登录请求;直接放行

chain.doFilter(request, response);

}

}

@Override

public void init(FilterConfig arg0) throws ServletException {

}

}

      3. 电力系统登录拦截

public class SystemFilter implements Filter {

//存放系统没有Session,但是需要访问的url,像这样的连接需要放行

List<String> list = new ArrayList<String>();

/**当服务启动的时候初始化*/

public void init(FilterConfig config) throws ServletException {

list.add("/index.jsp");

list.add("/image.jsp");

list.add("/system/elecMenuAction_menuHome.do");

list.add("/error.jsp");

list.add("/system/elecMenuAction_logout.do");

}

/**每次访问URL连接之前,都要访问的方法*/

public void doFilter(ServletRequest req, ServletResponse res,

FilterChain chain) throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) req;

HttpServletResponse response = (HttpServletResponse) res;

//记住我的代码放置到过滤器中

String path = request.getServletPath();

this.forwordIndexPage(request,path);

//如果当前访问的连接是定义list中存放的连接,此时需要放行

if(list.contains(path)){

chain.doFilter(request, response);

return;

}

//存在Session(不为空)的时候要放行,Session为空的时候,定向到首页

ElecUser elecUser = (ElecUser)request.getSession().getAttribute("globle_user");

if(elecUser!=null){

//从数据库中查询获取登录状态

WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());

IElecUserService elecUserService = (IElecUserService)ac.getBean(IElecUserService.SERVICE_NAME);

ElecUser user = elecUserService.findElecUserByID(elecUser);

Long logonStateDb = user.getLogonState();

//从Session中查询登录状态

Long logonState = elecUser.getLogonState();

if(logonStateDb.equals(logonState)){

chain.doFilter(request, response);

return;

}

else{

request.setAttribute("errorMsg", "您的账号已经在异地登录,换句话说,您被踢了!");

}

}

//重定向,由于传递request作用域失效

// response.sendRedirect(request.getContextPath()+"/error.jsp");

//转发

request.getRequestDispatcher("/error.jsp").forward(request, response);

}

/**销毁*/

public void destroy() {

}

//记住我的代码放置到过滤器中,在跳转到index.jsp页面之前

private void forwordIndexPage(HttpServletRequest request, String path) {

if(path.equals("/index.jsp")){

String name = "";

String password = "";

String checked = "";

Cookie [] cookies = request.getCookies();

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

for(Cookie cookie:cookies){

if(cookie.getName().equals("name")){

name = cookie.getValue();

//处理中文二进制解码问题

try {

name = URLDecoder.decode(name, "UTF-8");

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

checked = "checked";

}

if(cookie.getName().equals("password")){

password = cookie.getValue();

}

}

}

request.setAttribute("name", name);

request.setAttribute("password", password);

request.setAttribute("checked", checked);

}

}

}

  拦截器拦截 url :

<!-- 自定义过滤器,要放置到struts2的过滤器的上面 -->

<filter>

<filter-name>SystemFilter</filter-name>

<filter-class>cn.itcast.elec.util.SystemFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>SystemFilter</filter-name>

<url-pattern>*.jsp</url-pattern>

<url-pattern>*.do</url-pattern>

</filter-mapping>

 

以上是 单应用登录过滤器 的全部内容, 来源链接: utcz.com/z/512869.html

回到顶部