2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.fe.filters;
24 import org.apache.commons.lang3.StringUtils;
25 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
26 import org.openecomp.sdc.common.log.wrappers.Logger;
28 import javax.servlet.Filter;
29 import javax.servlet.FilterChain;
30 import javax.servlet.FilterConfig;
31 import javax.servlet.ServletException;
32 import javax.servlet.ServletRequest;
33 import javax.servlet.ServletResponse;
34 import javax.servlet.http.Cookie;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
37 import java.io.IOException;
38 import java.util.Arrays;
39 import java.util.List;
40 import java.util.NoSuchElementException;
42 public class SecurityFilter implements Filter {
43 private static final Logger log = Logger.getLogger(SecurityFilter.class.getName());
45 private static final String PORTAL_COOKIE_NAME_IS_NOT_SET = "Portal cookie name is not set in portal.properties file";
47 private List<String> excludedUrls;
49 static final String PORTAL_COOKIE_NAME_KEY = "portal_cookie_name";
50 static final String PORTAL_REDIRECT_URL_KEY = "ecomp_redirect_url";
51 static final String FILTER_EXLUDED_URLS_KEY ="excludedUrls";
54 public void init(FilterConfig filterConfig) throws ServletException {
55 excludedUrls = Arrays.asList(filterConfig.getInitParameter(FILTER_EXLUDED_URLS_KEY).split(","));
59 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
61 final HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
62 final HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
64 log.debug("SecurityFilter received request with URL {}", httpRequest.getRequestURL());
65 //add redirecting to Portal if cookie is not provided
66 if (!excludedUrls.contains(httpRequest.getServletPath()) && !isRequestFromPortal(httpRequest.getCookies())) {
67 //redirect to portal app
68 log.debug("Request {} is not from Portal, redirecting there", httpRequest.getServletPath());
69 httpResponse.sendRedirect(PortalApiProperties.getProperty(PORTAL_REDIRECT_URL_KEY));
72 filterChain.doFilter(servletRequest, servletResponse);
77 public void destroy() {
81 private boolean isRequestFromPortal(Cookie[] cookies) {
82 String portalCookieValue = PortalApiProperties.getProperty(PORTAL_COOKIE_NAME_KEY);
83 if (StringUtils.isEmpty(portalCookieValue)) {
84 log.error(PORTAL_COOKIE_NAME_IS_NOT_SET);
85 throw new NoSuchElementException(PORTAL_COOKIE_NAME_IS_NOT_SET);
87 return cookies != null && Arrays.stream(cookies)
88 .anyMatch(c->StringUtils.equals(c.getName(), portalCookieValue));