`
sctom123
  • 浏览: 111242 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论
阅读更多

package com.phoenix.demoweb.util.web.interceptor;

import java.io.IOException;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.acegisecurity.AccessDeniedException;
import org.acegisecurity.Authentication;
import org.acegisecurity.ConfigAttributeDefinition;
import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.event.authorization.AuthorizationFailureEvent;
import org.acegisecurity.event.authorization.AuthorizedEvent;
import org.acegisecurity.intercept.AbstractSecurityInterceptor;
import org.acegisecurity.intercept.InterceptorStatusToken;
import org.acegisecurity.intercept.ObjectDefinitionSource;
import org.acegisecurity.intercept.web.FilterInvocation;
import org.acegisecurity.intercept.web.FilterInvocationDefinitionSource;

import com.phoenix.demoweb.module.security.service.AuthenticationService;

public class FilterSecurityInterceptor extends AbstractSecurityInterceptor
  implements Filter {

 private AuthenticationService authenticationService;

 private static final String FILTER_APPLIED = "__acegi_filterSecurityInterceptor_filterApplied";

 private ServletContext application;

 private FilterInvocationDefinitionSource objectDefinitionSource;

 private boolean observeOncePerRequest = true;

 public void destroy() {
 }

 public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException {
  String userName = null;
  HttpSession session = ((HttpServletRequest) request).getSession(true);
  application = session.getServletContext();
  userName = (String) application.getAttribute(session.getId());
  List menuList = (List) application.getAttribute("menuList");
  if (menuList == null) {
   menuList = this.getAuthenticationService().getMenus();
   application.setAttribute("menuList", this.getAuthenticationService().getMenus());
  }
  if (userName != null) {
   // 在portal server中不需要检查密码。故传密码null
   Authentication au = this.getAuthenticationService().attemptAuthentication(
     ((HttpServletRequest) request), userName,null);
   session.setAttribute("userName", userName);
   SecurityContextHolder.getContext().setAuthentication(au);
   application.removeAttribute(session.getId());
  }
  FilterInvocation fi = new FilterInvocation(request, response, chain);
  invoke(fi);
 }

 public void invoke(FilterInvocation fi) throws IOException,
   ServletException {
  if ((fi.getRequest() != null)
    && (fi.getRequest().getAttribute(FILTER_APPLIED) != null)
    && observeOncePerRequest) {
   fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
  } else {
   if (fi.getRequest() != null) {
    fi.getRequest().setAttribute(FILTER_APPLIED, Boolean.TRUE);
   }
   InterceptorStatusToken token = this.beforeInvocation(fi);
   try {
    fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
   } finally {
    super.afterInvocation(token, null);
   }
  }
 }

 protected InterceptorStatusToken beforeInvocation(Object object) {
  /**
   * put the role and url into ConfigAttributeDefinition Attribute
   */
  FilterInvocation filterInvocation = (FilterInvocation) object;
  ServletRequest request = filterInvocation.getRequest();
  HttpSession session = ((HttpServletRequest) request).getSession(true);
  application = session.getServletContext();
  List menuList = (List) application.getAttribute("menuList");
  ConfigAttributeDefinition attr = null;
  attr = this.getAuthenticationService().getAcegiConfig(object, menuList);
  if (attr == null) {

   return null; // no further work post-invocation
  }

  Authentication authenticated = null;

  if (!SecurityContextHolder.getContext().getAuthentication()
    .isAuthenticated()
    || super.isAlwaysReauthenticate()) {
   try {
    authenticated = super.getAuthenticationManager().authenticate(
      SecurityContextHolder.getContext().getAuthentication());
   } catch (Exception e) {
    e.printStackTrace();
   }

   SecurityContextHolder.getContext().setAuthentication(authenticated);
  } else {
   authenticated = SecurityContextHolder.getContext()
     .getAuthentication();

  }

  try {
   super.getAccessDecisionManager()
     .decide(authenticated, object, attr);
  } catch (AccessDeniedException accessDeniedException) {
   AuthorizationFailureEvent event = new AuthorizationFailureEvent(
     object, attr, authenticated, accessDeniedException);
   throw accessDeniedException;
  }
  AuthorizedEvent event = new AuthorizedEvent(object, attr, authenticated);
  Authentication runAs = this.getRunAsManager().buildRunAs(authenticated,
    object, attr);

  if (runAs == null) {

   return new InterceptorStatusToken(authenticated, false, attr,
     object);
  } else {

   SecurityContextHolder.getContext().setAuthentication(runAs);

   return new InterceptorStatusToken(authenticated, true, attr, object);
  }
 }

 public FilterInvocationDefinitionSource getObjectDefinitionSource() {
  return this.objectDefinitionSource;
 }

 public Class getSecureObjectClass() {
  return FilterInvocation.class;
 }

 /**
  * Not used (we rely on IoC container lifecycle services instead)
  *
  * @param arg0
  *            ignored
  *
  * @throws ServletException
  *             never thrown
  */
 public void init(FilterConfig araeg0) throws ServletException {
 }

 /**
  * Indicates whether once-per-request handling will be observed. By default
  * this is <code>true</code>, meaning the
  * <code>FilterSecurityInterceptor</code> will only execute
  * once-per-request. Sometimes users may wish it to execute more than once
  * per request, such as when JSP forwards are being used and filter security
  * is desired on each included fragment of the HTTP request.
  *
  * @return <code>true</code> (the default) if once-per-request is
  *         honoured, otherwise <code>false</code> if
  *         <code>FilterSecurityInterceptor</code> will enforce
  *         authorizations for each and every fragment of the HTTP request.
  */
 public boolean isObserveOncePerRequest() {
  return observeOncePerRequest;
 }

 public ObjectDefinitionSource obtainObjectDefinitionSource() {
  return this.objectDefinitionSource;
 }

 public void setObjectDefinitionSource(
   FilterInvocationDefinitionSource newSource) {
  this.objectDefinitionSource = newSource;
 }

 public void setObserveOncePerRequest(boolean observeOncePerRequest) {
  this.observeOncePerRequest = observeOncePerRequest;
 }

 

 public AuthenticationService getAuthenticationService() {
  return authenticationService;
 }

 public void setAuthenticationService(AuthenticationService authenticationService) {
  this.authenticationService = authenticationService;
 }

}

分享到:
评论

相关推荐

    IBM Websphere Portal Primer

    The dictionary defines a portal as a grand or imposing door or entrance; hence, that picture on the cover. In this book, you will learn why the word portal has become a major buzzword in the world of ...

    OSSH免费版华为外置Portal认证配置手册

    华为外置Portal认证配置手册主要介绍了Portal认证服务器的相关配置,包括Portal的基本概念、环境要求、软件部署、网络需求、配置思路和具体的配置步骤。 首先,我们来了解一下Portal的概念。Portal在英语中是入口的...

    Portal开发Portal开发

    【Portal开发详解】 Portal开发是构建企业级门户系统的关键技术,它旨在整合各种信息资源,提供统一的访问入口,以提升用户体验和工作效率。在本文中,我们将深入探讨Portal开发涉及的多个方面,包括核心概念、关键...

    NC65portal开发文档.rar

    《NC65portal开发文档》是一份针对NC65平台的开发者手册,旨在为软件工程师提供详尽的指导,帮助他们理解和开发基于NC65portal的应用程序。NC65portal是一个集成了多种功能的Web应用平台,专为满足企业级业务需求而...

    H3C IMC portal认证操作手册

    ### H3C IMC Portal 认证操作手册关键知识点解析 #### 一、Portal认证概述 **Portal认证**,在英语中意为“入口”,在IT领域特指一种通过Web页面来验证用户身份的方式,以此实现对用户网络访问权限的控制。在采用...

    72341852_STEP7_TIA_Portal_V15_HSP_en_TIAPORTAL15_HSP_V15下载HSP_

    标题中的“72341852_STEP7_TIA_Portal_V15_HSP_en_TIAPORTAL15_HSP_V15下载HSP_”暗示了这是一个关于西门子TIA Portal V15 Hotfix Service Package (HSP) 的资源下载链接,主要面向英文用户。描述简单明了,表明这是...

    BRAS 设备在 iOS 终端无法弹出 Portal 认证页面或弹出速度慢的解决方法

    Portal认证是一种网络接入控制机制,它要求用户在访问网络资源之前,必须通过浏览器访问特定的Portal服务器并完成身份验证。这种技术常用于公共场所的Wi-Fi接入,例如酒店、机场和咖啡厅等。当iOS设备连接到支持...

    portal介绍,portal系统组成

    ### Portal系统介绍及组成 #### Portal概念解析 Portal在英文中的原意是“入口”,在IT领域特指一种网络认证模式——Portal认证,通常被称为Web认证。这种认证方式通过特定的门户网站来实现用户的身份验证。当用户...

    SANGFOR_AC_v11.8本地Portal服务器搭建与华为Portal控制器对接配置指导书.pdf

    SANGFOR AC v11.8 本地 Portal 服务器搭建与华为 Portal 控制器对接配置指导书 本文档主要讲述了 SANGFOR AC v11.8 本地 Portal 服务器的搭建和华为 Portal 控制器对接的配置指导书。该文档面向针对读者对象是 IT ...

    中国移动WLAN业务portal设备规范v3.0.0

    ### 中国移动WLAN业务Portal设备规范v3.0.0 关键知识点解析 #### 一、范围与适用性 **1.1 范围** 本标准旨在为中国移动WLAN业务提供Portal设备的规范,涵盖了设备在网络中的位置、功能要求、流程要求、接口要求...

    如何完全卸载 STEP 7 (TIA Portal) 软件.doc

    "卸载 STEP 7 (TIA Portal) 软件" STEP 7 (TIA Portal) 软件是西门子公司开发的PLC编程软件,广泛应用于自动化和工业控制领域。然而,在卸载 STEP 7 (TIA Portal) 软件之前,需要备份项目、库和授权,以免数据丢失...

    TIA Portal V16 软件安装包

     1、TIA Portal Multiuser Engineering  TIA允许多个用户同时工作在同一个项目上,增加协作的能力,另外还可以通过委托减少工作时间。  2、TIA Portal Teamcenter Gateway  Teamcenter网关让您保存和...

    OpenWRT 下实现 Portal 认证(WEB 认证)

    Portal认证是无线网络中常见的用户认证方式,它要求用户在未认证的状态下只能访问特定的门户网站,这个网站通常被称为Portal页面。当用户想要访问互联网中其他资源时,必须在这个Portal页面上进行认证,认证通过后,...

    TIA PORTAL 最新授权

    根据提供的文件信息,我们将深入探讨“TIA PORTAL 最新授权”的相关知识点。 ### TIA Portal 最新授权 #### 一、TIA Portal 概述 TIA (Totally Integrated Automation) Portal 是西门子公司推出的一款集成化工程...

    OpenWRT实现Portal认证

    OpenWRT实现Portal认证是一种网络认证方式,它涉及在OpenWRT开源路由器操作系统中配置特定软件来实现用户登录和身份验证。Portal认证通常用于无线网络环境中,使得用户在连接到无线热点时,必须通过一个登录页面输入...

    (His)创业医院系统portal无限多开.zip

    【创业医院系统Portal无限多开】是指在医院信息管理系统(HIS)中实现门户(Portal)应用程序的无限制同时打开功能。这对于医院信息化管理来说,是一个重要的优化措施,旨在提高工作效率,满足医疗工作者多任务并行...

    可拖拽布局组件easyui-portal

    在IT行业中,构建用户友好的界面是至关重要的,这正是`EasyUI-Portal`组件发挥作用的地方。`EasyUI-Portal`是一个基于JavaScript和CSS的开源框架,专为创建可定制、可拖拽的布局而设计,它使得开发人员能够轻松构建...

    博图 TIA Portal V15 全套软件

    下载链接包含博图V15全套软件,其中包含SIMATIC_PLCSIM_Advanced_V2、TIA_Portal_STEP_7_Pro_WINCC_Adv_V15 TIA_Portal_STEP_7_Pro_WINCC_Pro_V15 .... 下载链接包含博图V15全套软件,其中包含SIMATIC_PLCSIM_...

    Camstar PortalStudio中文用户手册

    根据提供的文件信息,本文将详细说明“Camstar PortalStudio中文用户手册”中的知识点,内容将覆盖用户手册的核心部分,包括软件的基本介绍、用户界面功能、以及页面创建和管理等方面的详细指南。 首先,文档开头...

Global site tag (gtag.js) - Google Analytics