1
22
23 package com.liferay.portal.servlet.filters.sso.opensso;
24
25 import com.liferay.portal.kernel.util.GetterUtil;
26 import com.liferay.portal.kernel.util.Validator;
27 import com.liferay.portal.util.PortalUtil;
28 import com.liferay.portal.util.PrefsPropsUtil;
29 import com.liferay.portal.util.PropsUtil;
30 import com.liferay.portal.util.WebKeys;
31 import com.liferay.util.CookieUtil;
32
33 import java.io.IOException;
34
35 import javax.servlet.Filter;
36 import javax.servlet.FilterChain;
37 import javax.servlet.FilterConfig;
38 import javax.servlet.ServletException;
39 import javax.servlet.ServletRequest;
40 import javax.servlet.ServletResponse;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpServletResponse;
43 import javax.servlet.http.HttpSession;
44
45 import org.apache.commons.logging.Log;
46 import org.apache.commons.logging.LogFactory;
47
48
55 public class OpenSSOFilter implements Filter {
56
57 public void init(FilterConfig filterConfig) throws ServletException {
58 }
59
60 public void doFilter(
61 ServletRequest req, ServletResponse res, FilterChain chain)
62 throws IOException, ServletException {
63
64 try {
65 HttpServletRequest httpReq = (HttpServletRequest)req;
66 HttpServletResponse httpRes = (HttpServletResponse)res;
67
68 long companyId = PortalUtil.getCompanyId(httpReq);
69
70 boolean enabled = PrefsPropsUtil.getBoolean(
71 companyId, PropsUtil.OPEN_SSO_AUTH_ENABLED);
72 String loginUrl = PrefsPropsUtil.getString(
73 companyId, PropsUtil.OPEN_SSO_LOGIN_URL);
74 String logoutUrl = PrefsPropsUtil.getString(
75 companyId, PropsUtil.OPEN_SSO_LOGOUT_URL);
76 String serviceUrl = PrefsPropsUtil.getString(
77 companyId, PropsUtil.OPEN_SSO_SERVICE_URL);
78 String cookieName = PrefsPropsUtil.getString(
79 companyId, PropsUtil.OPEN_SSO_SUBJECT_COOKIE_NAME);
80
81 if (!enabled || Validator.isNull(loginUrl) ||
82 Validator.isNull(logoutUrl) || Validator.isNull(serviceUrl) ||
83 Validator.isNull(cookieName)) {
84
85 chain.doFilter(req, res);
86
87 return;
88 }
89
90 String requestURI = GetterUtil.getString(httpReq.getRequestURI());
91
92 if (requestURI.endsWith("/portal/logout")) {
93 HttpSession httpSes = httpReq.getSession();
94
95 httpSes.invalidate();
96
97 httpRes.sendRedirect(logoutUrl);
98 }
99 else {
100 if (isAuthenticated(httpReq, cookieName)) {
101 chain.doFilter(req, res);
102 }
103 else {
104 httpRes.sendRedirect(loginUrl);
105 }
106 }
107 }
108 catch (Exception e) {
109 _log.error(e, e);
110 }
111 }
112
113 public void destroy() {
114 }
115
116 protected boolean isAuthenticated(
117 HttpServletRequest req, String cookieName) {
118
119 String cookieValue = CookieUtil.get(req.getCookies(), cookieName);
120
121 if (Validator.isNotNull(cookieValue)) {
122 HttpSession ses = req.getSession();
123
124 ses.setAttribute(WebKeys.OPEN_SSO_LOGIN, cookieValue);
125
126 return true;
127 }
128 else {
129 return false;
130 }
131 }
132
133 private static Log _log = LogFactory.getLog(OpenSSOFilter.class);
134
135 }