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;
}
}
分享到:
相关推荐
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 ...
【用友UAP的Portal开发】是针对企业级Web应用的一种解决方案,旨在提供一个集成了多种功能和服务的统一入口。UAP Portal是基于JSR 286规范,遵循J2EE技术体系,具备强大的系统集成和组装能力。它不仅支持产品展示...
华为外置Portal认证配置手册主要介绍了Portal认证服务器的相关配置,包括Portal的基本概念、环境要求、软件部署、网络需求、配置思路和具体的配置步骤。 首先,我们来了解一下Portal的概念。Portal在英语中是入口的...
Portal 服务器搭建 Portal 认证是指对用户上网流量(即所有穿过防火墙的流量)进行认证,以确保用户身份的安全性。Portal 认证的典型组网由三个基本要素组成:认证客户端、接入设备、Portal 认证/计费服务器。 ...
《NC65portal开发文档》是一份针对NC65平台的开发者手册,旨在为软件工程师提供详尽的指导,帮助他们理解和开发基于NC65portal的应用程序。NC65portal是一个集成了多种功能的Web应用平台,专为满足企业级业务需求而...
### H3C IMC Portal 认证操作手册关键知识点解析 #### 一、Portal认证概述 **Portal认证**,在英语中意为“入口”,在IT领域特指一种通过Web页面来验证用户身份的方式,以此实现对用户网络访问权限的控制。在采用...
标题中的“72341852_STEP7_TIA_Portal_V15_HSP_en_TIAPORTAL15_HSP_V15下载HSP_”暗示了这是一个关于西门子TIA Portal V15 Hotfix Service Package (HSP) 的资源下载链接,主要面向英文用户。描述简单明了,表明这是...
Portal认证是一种网络接入控制机制,它要求用户在访问网络资源之前,必须通过浏览器访问特定的Portal服务器并完成身份验证。这种技术常用于公共场所的Wi-Fi接入,例如酒店、机场和咖啡厅等。当iOS设备连接到支持...
### Portal系统介绍及组成 #### Portal概念解析 Portal在英文中的原意是“入口”,在IT领域特指一种网络认证模式——Portal认证,通常被称为Web认证。这种认证方式通过特定的门户网站来实现用户的身份验证。当用户...
SANGFOR AC v11.8 本地 Portal 服务器搭建与华为 Portal 控制器对接配置指导书 本文档主要讲述了 SANGFOR AC v11.8 本地 Portal 服务器的搭建和华为 Portal 控制器对接的配置指导书。该文档面向针对读者对象是 IT ...
**TIA Portal V13 授权详解** TIA Portal(全称为 Totally Integrated Automation Portal)是西门子(Siemens)推出的一款集成自动化工程软件平台,主要用于工业自动化领域的系统设计、编程、模拟和调试。V13是该...
### 中国移动WLAN业务Portal设备规范v3.0.0 关键知识点解析 #### 一、范围与适用性 **1.1 范围** 本标准旨在为中国移动WLAN业务提供Portal设备的规范,涵盖了设备在网络中的位置、功能要求、流程要求、接口要求...
"卸载 STEP 7 (TIA Portal) 软件" STEP 7 (TIA Portal) 软件是西门子公司开发的PLC编程软件,广泛应用于自动化和工业控制领域。然而,在卸载 STEP 7 (TIA Portal) 软件之前,需要备份项目、库和授权,以免数据丢失...
Portal认证是无线网络中常见的用户认证方式,它要求用户在未认证的状态下只能访问特定的门户网站,这个网站通常被称为Portal页面。当用户想要访问互联网中其他资源时,必须在这个Portal页面上进行认证,认证通过后,...
【创业医院系统Portal无限多开】是指在医院信息管理系统(HIS)中实现门户(Portal)应用程序的无限制同时打开功能。这对于医院信息化管理来说,是一个重要的优化措施,旨在提高工作效率,满足医疗工作者多任务并行...
根据提供的文件信息,我们将深入探讨“TIA PORTAL 最新授权”的相关知识点。 ### TIA Portal 最新授权 #### 一、TIA Portal 概述 TIA (Totally Integrated Automation) Portal 是西门子公司推出的一款集成化工程...
1、TIA Portal Multiuser Engineering TIA允许多个用户同时工作在同一个项目上,增加协作的能力,另外还可以通过委托减少工作时间。 2、TIA Portal Teamcenter Gateway Teamcenter网关让您保存和...
OpenWRT实现Portal认证是一种网络认证方式,它涉及在OpenWRT开源路由器操作系统中配置特定软件来实现用户登录和身份验证。Portal认证通常用于无线网络环境中,使得用户在连接到无线热点时,必须通过一个登录页面输入...
下载链接包含博图V15全套软件,其中包含SIMATIC_PLCSIM_Advanced_V2、TIA_Portal_STEP_7_Pro_WINCC_Adv_V15 TIA_Portal_STEP_7_Pro_WINCC_Pro_V15 .... 下载链接包含博图V15全套软件,其中包含SIMATIC_PLCSIM_...