Web訪問登陸過濾器控制
web.xml
public class SystemConstants {
public static final String CURRENT_USER = "current_user";
}
AuthFilter.java
<filter>
<filter-name>AuthFilter</filter-name>
<filter-class>com.acconsys.capital.util.AuthFilter</filter-class>
<init-param>
<param-name>excludeUrl</param-name>
<param-value>login.action,loginSubmit.action</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>AuthFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
public class SystemConstants {
public static final String CURRENT_USER = "current_user";
}
AuthFilter.java
/*******************************************************************************
* @project: Capital501
* @package: com.acconsys.capital.util
* @file: AuthFilter.java
* @created: 2014-8-15
* @purpose:
*
* @version: 1.0
*
* Revision History at the end of file.
*
* Copyright 2014 AcconSys All rights reserved.
******************************************************************************/
package com.acconsys.capital.util;
import java.io.IOException;
import java.io.PrintWriter;
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;
import com.acconsys.capital.common.SystemConstants;
public class AuthFilter implements Filter {
private FilterConfig filterConfig = null;
private String sysUrl;
/**
* @see Filter#destroy()
*/
public void destroy() {
this.filterConfig = null;
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
try {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
if(sysUrl == null) {
StringBuffer requestURL = httpRequest.getRequestURL();
String contextPath = httpRequest.getContextPath();
sysUrl = requestURL.substring(0, requestURL.indexOf(contextPath)) + contextPath;
}
if(isExcludeUrl(httpRequest)) {
chain.doFilter(request, response);
return;
}
HttpSession session = httpRequest.getSession(false);
if (session == null || session.getAttribute(SystemConstants.CURRENT_USER) == null) {
PrintWriter out = httpResponse.getWriter();
out.write("<script>window.top.location.href='" +httpRequest.getContextPath()
+ "/login.action'</script>");
}else{
chain.doFilter(request, response);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private boolean isExcludeUrl(HttpServletRequest request) {
String contextPath = request.getContextPath();
String excludeUrl = filterConfig.getInitParameter("excludeUrl");
String requestUri = request.getRequestURI();
int questionIndex = requestUri.indexOf("?");
if (questionIndex > 0) {
requestUri = requestUri.substring(0, questionIndex);
}
String[] actions = excludeUrl.split(",");
for (int i = 0; i < actions.length; i++) {
if (requestUri.equals(contextPath + "/" + actions[i].trim())) {
return true;
}
}
return false;
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
}
/*******************************************************************************
* <B>Revision History</B><BR>
* [type 'revision' and press Alt + / to insert revision block]<BR>
*
*
*
* Copyright 2014 AcconSys All rights reserved.
******************************************************************************/
相關文章
- spring-boot登陸過濾功能Springboot
- koa2+vue實現登陸以及是否登陸控制Vue
- .net core 登入全域性驗證過濾器過濾器
- [BUG反饋]非管理員登陸無法訪問模型管理模型
- 過濾器過濾器
- 登陸框select繞過
- shiro 登陸的問題。
- Flask——訪問控制Flask
- Mongodb訪問控制MongoDB
- web前端技巧分享:vue過濾器的那點事!Web前端Vue過濾器
- 4、過濾器的使用及自定義過濾器過濾器
- 點雲濾波器與過濾器過濾器
- 通過session模擬登陸Session
- CAN過濾器過濾器
- Filter過濾器Filter過濾器
- vue 過濾器Vue過濾器
- NetCore過濾器NetCore過濾器
- 代理過濾器過濾器
- Vue過濾器Vue過濾器
- DataV過濾器過濾器
- hbase過濾器過濾器
- 網站_域名_DNS_埠_web訪問過程網站DNSWeb
- Xor過濾器:比布隆Bloom過濾器更快,更小過濾器OOM
- 瀏覽器 Web 訪問剪下板圖片瀏覽器Web
- 訪問web伺服器--網路實驗Web伺服器
- web專案024-----賬戶登陸Web
- openGauss 訪問控制模型模型
- ABAC訪問控制模型模型
- 類的訪問控制
- 教程:已在其地方登陸或會話已過期,請重新登陸會話
- Spring Cloud Gateway ---GatewayFilter過濾器、過濾器工廠(入門)SpringCloudGatewayFilter過濾器
- web方式訪問sshWeb
- Spring Cloud Gateway過濾器精確控制異常返回(分析篇)SpringCloudGateway過濾器
- PHP 過濾器(Filter)PHP過濾器Filter
- vue---過濾器Vue過濾器
- Vue中過濾器Vue過濾器
- vue filters過濾器VueFilter過濾器
- 布隆過濾器過濾器
- SpringSecurity過濾器原理SpringGse過濾器