-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavaUtils-AuthenticationFilter.template
executable file
·54 lines (46 loc) · 1.82 KB
/
JavaUtils-AuthenticationFilter.template
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.sd.packagename.common.util;
import entity.User;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.servlet. *;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
* @Created with Atom
* @author @author@
* @time @now@
* @description
* 权限验证 工具类
*
**/
public class AuthenticationFilter implements Filter {
private static Log log = LogFactory.getLog(AuthenticationFilter.class);
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)request;
String path = req.getServletPath();
if ((path.indexOf(".action") == -1) && (path.indexOf(".jsp") == -1)) { //只拦截action和jsp
chain.doFilter(request, response);
return;
} else {
String uri = path.substring(path.lastIndexOf("/", path.length()));
if (uri.startsWith("/login!") || path.endsWith("/login.action") || path.endsWith("/login.jsp") || path.endsWith("/register.action") || path.endsWith("/register.jsp")) { //登录无需判断权限
chain.doFilter(request, response);
return;
} else {
HttpSession session = req.getSession();
User user = (User)session.getAttribute("loginUser");
if (user == null) {
log.debug("User doesn't exist in session");
HttpServletResponse res = (HttpServletResponse)response;
res.sendRedirect(req.getContextPath() + "/login.action");
} else {
chain.doFilter(request, response);
}
}
}
}
@Override public void init(FilterConfig filterConfig)throws ServletException {}
@Override public void destroy() {}
}