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