Rev 1814 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1814 | Rev 1830 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | package pt.estgp.estgweb.web.filters; |
1 | package pt.estgp.estgweb.web.filters; |
2 | 2 | ||
3 | import org.apache.log4j.Logger; |
3 | import org.apache.log4j.Logger; |
4 | import pt.estgp.estgweb.utils.Globals; |
- | |
5 | import pt.estgp.estgweb.domain.UserSessionImpl; |
4 | import pt.estgp.estgweb.domain.UserSessionImpl; |
- | 5 | import pt.estgp.estgweb.utils.Globals; |
|
- | 6 | import pt.estgp.estgweb.web.UserSessionProxy; |
|
6 | 7 | ||
7 | import javax.servlet.*; |
8 | import javax.servlet.*; |
8 | import javax.servlet.http.HttpServletRequest; |
9 | import javax.servlet.http.HttpServletRequest; |
9 | import javax.servlet.http.HttpServletResponse; |
- | |
10 | import java.io.IOException; |
10 | import java.io.IOException; |
11 | 11 | ||
12 | /** |
12 | /** |
13 | * |
13 | * |
14 | * @author Jorge Machado |
14 | * @author Jorge Machado |
15 | */ |
15 | */ |
16 | 16 | ||
17 | public class UserFilter implements 1.5.0/docs/api/java/util/logging/Filter.html">Filter { |
17 | public class AuthenticationAllFilter extends UserFilter { |
18 | 18 | ||
19 | private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(UserFilter.class); |
19 | private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(AuthenticationAllFilter.class); |
20 | 20 | ||
21 | 21 | ||
22 | public UserFilter() |
22 | public AuthenticationAllFilter() |
23 | { |
23 | { |
24 | } |
24 | } |
25 | 25 | ||
- | 26 | static 1.5.0/docs/api/java/lang/String.html">String[] allowedPatternsContains; |
|
- | 27 | static 1.5.0/docs/api/java/lang/String.html">String[] allowedPatternsStartsWith; |
|
- | 28 | static 1.5.0/docs/api/java/lang/String.html">String[] allowedPatternsEqual; |
|
- | 29 | ||
26 | public void init(FilterConfig config) |
30 | public void init(FilterConfig config) |
27 | { |
31 | { |
28 | - | ||
- | 32 | allowedPatternsContains = config.getInitParameter("allowedPatternsContains").replaceAll(" ","").replaceAll("\t","").replaceAll("\r","").replaceAll("\n","").trim().split(","); |
|
- | 33 | allowedPatternsStartsWith = config.getInitParameter("allowedPatternsStartsWith").replaceAll(" ","").replaceAll("\t","").replaceAll("\r","").replaceAll("\n","").trim().split(","); |
|
- | 34 | allowedPatternsEqual = config.getInitParameter("allowedPatternsEqual").replaceAll(" ","").replaceAll("\t","").replaceAll("\r","").replaceAll("\n","").trim().split(",");; |
|
29 | } |
35 | } |
30 | 36 | ||
- | 37 | static 1.5.0/docs/api/java/lang/String.html">String linkSubmitLogin = Globals.SYSTEM_REDIRECTIONS_POLICY_AUTHENTICATION_ACTION; |
|
31 | /** |
38 | /** |
32 | * |
39 | * |
33 | * @param request The servlet request we are processing |
40 | * @param request The servlet request we are processing |
34 | * @param chain The filter chain we are processing |
41 | * @param chain The filter chain we are processing |
35 | * |
42 | * |
Line 37... | Line 44... | ||
37 | * @exception javax.servlet.ServletException if a servlet error occurs |
44 | * @exception javax.servlet.ServletException if a servlet error occurs |
38 | */ |
45 | */ |
39 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) |
46 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) |
40 | throws 1.5.0/docs/api/java/io/IOException.html">IOException, ServletException |
47 | throws 1.5.0/docs/api/java/io/IOException.html">IOException, ServletException |
41 | { |
48 | { |
- | 49 | //String linkLogin = Globals.SYSTEM_REDIRECTIONS_POLICY_INDEX_WELCOME; |
|
- | 50 | ||
42 | HttpServletRequest hrequest = (HttpServletRequest) request; |
51 | HttpServletRequest hrequest = (HttpServletRequest) request; |
- | 52 | ||
43 | if(((HttpServletRequest)request).getServletPath().indexOf("errorNoCookies.do")>=0 || !hrequest.isRequestedSessionIdFromCookie()) |
53 | UserSessionImpl userSession = (UserSessionImpl) UserSessionProxy.loadUserSessionFromRequest(hrequest); |
- | 54 | if(userSession != null && userSession.isAuthenticated()) |
|
- | 55 | { |
|
44 | chain.doFilter(request,response); |
56 | chain.doFilter(request,response); |
- | 57 | return; |
|
- | 58 | } |
|
- | 59 | ||
45 | 60 | ||
46 | UserSessionImpl userSession = (UserSessionImpl) request.getAttribute(Globals.USER_SESSION_KEY); |
- | |
47 | 61 | ||
48 | if(userSession != null && userSession.getUser() != null) |
62 | if(hrequest.getServletPath().equals("") |
- | 63 | || hrequest.getServletPath().equals("/") |
|
- | 64 | || hrequest.getServletPath().startsWith(linkSubmitLogin)) |
|
- | 65 | { |
|
49 | chain.doFilter(request,response); |
66 | chain.doFilter(request,response); |
50 | else if(userSession == null) |
67 | return; |
- | 68 | } |
|
- | 69 | ||
- | 70 | if(FilterUtils.checkIfIsIgnoredExtension(request, hrequest)) |
|
51 | { |
71 | { |
52 | logger.info("User lost Session"); |
72 | chain.doFilter(request, response); |
- | 73 | return; |
|
53 | } |
74 | } |
- | 75 | ||
54 | else |
76 | for(1.5.0/docs/api/java/lang/String.html">String patternEqual : allowedPatternsEqual) |
55 | { |
77 | { |
- | 78 | if(hrequest.getServletPath().equals(patternEqual)) |
|
56 | try{ |
79 | { |
- | 80 | chain.doFilter(request,response); |
|
- | 81 | return; |
|
- | 82 | } |
|
- | 83 | } |
|
57 | 84 | ||
- | 85 | for(1.5.0/docs/api/java/lang/String.html">String patternEqual : allowedPatternsContains) |
|
- | 86 | { |
|
- | 87 | if(hrequest.getServletPath().contains(patternEqual)) |
|
- | 88 | { |
|
- | 89 | chain.doFilter(request,response); |
|
- | 90 | return; |
|
- | 91 | } |
|
- | 92 | } |
|
58 | 93 | ||
59 | // ((HttpServletResponse)response).setStatus(401); |
94 | for(1.5.0/docs/api/java/lang/String.html">String patternEqual : allowedPatternsStartsWith) |
- | 95 | { |
|
60 | ((HttpServletResponse)response).sendError(401); |
96 | if(hrequest.getServletPath().startsWith(patternEqual)) |
61 | }catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable e) |
- | |
62 | { |
97 | { |
63 | logger.error("Error don't sending error, probably already sent: " + e); |
98 | chain.doFilter(request,response); |
- | 99 | return; |
|
64 | } |
100 | } |
65 | } |
101 | } |
- | 102 | ||
- | 103 | ||
- | 104 | logger.warn("URL nao permitido, enviando filtragem para a Super Classe UserFilter"); |
|
- | 105 | super.doFilter(request,response,chain); |
|
66 | } |
106 | } |
67 | 107 | ||
68 | 108 | ||
- | 109 | ||
- | 110 | ||
69 | public void destroy() |
111 | public void destroy() |
70 | { |
112 | { |
71 | // Nothing |
113 | // Nothing |
72 | } |
114 | } |
73 | 115 |