1
22
23 package com.liferay.portal.servlet.filters.sso.cas;
24
25 import com.liferay.portal.util.PortalUtil;
26 import com.liferay.portal.util.PrefsPropsUtil;
27 import com.liferay.portal.util.PropsUtil;
28 import com.liferay.util.CollectionFactory;
29 import com.liferay.util.servlet.filters.DynamicFilterConfig;
30
31 import java.io.IOException;
32
33 import java.util.Map;
34
35 import javax.servlet.Filter;
36 import javax.servlet.FilterChain;
37 import javax.servlet.FilterConfig;
38 import javax.servlet.ServletContext;
39 import javax.servlet.ServletException;
40 import javax.servlet.ServletRequest;
41 import javax.servlet.ServletResponse;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44 import javax.servlet.http.HttpSession;
45
46 import org.apache.commons.logging.Log;
47 import org.apache.commons.logging.LogFactory;
48
49
56 public class CASFilter implements Filter {
57
58 public static void reload(long companyId) {
59 _casFilters.remove(new Long(companyId));
60 }
61
62 public void init(FilterConfig config) throws ServletException {
63 }
64
65 public void doFilter(
66 ServletRequest req, ServletResponse res, FilterChain chain)
67 throws IOException, ServletException {
68
69 try {
70 HttpServletRequest httpReq = (HttpServletRequest)req;
71
72 long companyId = PortalUtil.getCompanyId(httpReq);
73
74 if (PrefsPropsUtil.getBoolean(
75 companyId, PropsUtil.CAS_AUTH_ENABLED)) {
76
77 String pathInfo = httpReq.getPathInfo();
78
79 if (pathInfo.indexOf("/portal/logout") != -1) {
80 HttpServletResponse httpRes = (HttpServletResponse)res;
81 HttpSession httpSes = httpReq.getSession();
82
83 httpSes.invalidate();
84
85 String logoutUrl = PrefsPropsUtil.getString(
86 companyId, PropsUtil.CAS_LOGOUT_URL);
87
88 httpRes.sendRedirect(logoutUrl);
89 }
90 else {
91 Filter casFilter = getCASFilter(companyId);
92
93 casFilter.doFilter(req, res, chain);
94 }
95 }
96 else {
97 chain.doFilter(req, res);
98 }
99 }
100 catch (Exception e) {
101 _log.error(e, e);
102 }
103 }
104
105 public void destroy() {
106 }
107
108 protected Filter getCASFilter(long companyId) throws Exception {
109 Long companyIdObj = new Long(companyId);
110
111 edu.yale.its.tp.cas.client.filter.CASFilter casFilter =
112 (edu.yale.its.tp.cas.client.filter.CASFilter)_casFilters.get(
113 companyIdObj);
114
115 if (casFilter == null) {
116 casFilter = new edu.yale.its.tp.cas.client.filter.CASFilter();
117
118 DynamicFilterConfig config = new DynamicFilterConfig(
119 _filterName, _ctx);
120
121 config.addInitParameter(
122 edu.yale.its.tp.cas.client.filter.CASFilter.LOGIN_INIT_PARAM,
123 PrefsPropsUtil.getString(companyId, PropsUtil.CAS_LOGIN_URL));
124 config.addInitParameter(
125 edu.yale.its.tp.cas.client.filter.CASFilter.SERVICE_INIT_PARAM,
126 PrefsPropsUtil.getString(companyId, PropsUtil.CAS_SERVICE_URL));
127 config.addInitParameter(
128 edu.yale.its.tp.cas.client.filter.CASFilter.VALIDATE_INIT_PARAM,
129 PrefsPropsUtil.getString(
130 companyId, PropsUtil.CAS_VALIDATE_URL));
131
132 casFilter.init(config);
133
134 _casFilters.put(companyIdObj, casFilter);
135 }
136
137 return casFilter;
138 }
139
140 private static Log _log = LogFactory.getLog(CASFilter.class);
141
142 private static Map _casFilters = CollectionFactory.getSyncHashMap();
143
144 private String _filterName;
145 private ServletContext _ctx;
146
147 }