2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property and Nokia
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===================================
20 package org.oransc.ric.portal.dashboard.config;
22 import java.io.IOException;
23 import java.lang.invoke.MethodHandles;
25 import org.onap.portalsdk.core.onboarding.crossapi.PortalRestAPIProxy;
26 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
27 import org.oransc.ric.portal.dashboard.DashboardConstants;
28 import org.oransc.ric.portal.dashboard.LoginServlet;
29 import org.oransc.ric.portal.dashboard.controller.AcXappController;
30 import org.oransc.ric.portal.dashboard.controller.AdminController;
31 import org.oransc.ric.portal.dashboard.controller.AnrXappController;
32 import org.oransc.ric.portal.dashboard.controller.AppManagerController;
33 import org.oransc.ric.portal.dashboard.controller.E2ManagerController;
34 import org.oransc.ric.portal.dashboard.portalapi.DashboardUserManager;
35 import org.oransc.ric.portal.dashboard.portalapi.PortalAuthManager;
36 import org.oransc.ric.portal.dashboard.portalapi.PortalAuthenticationFilter;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Value;
40 import org.springframework.boot.web.servlet.ServletRegistrationBean;
41 import org.springframework.context.annotation.Bean;
42 import org.springframework.context.annotation.Configuration;
43 import org.springframework.context.annotation.Profile;
44 import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
45 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
46 import org.springframework.security.config.annotation.web.builders.WebSecurity;
47 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
48 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
49 import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
50 import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
54 @EnableGlobalMethodSecurity(securedEnabled = true)
56 public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
58 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
60 // Although constructor arguments are recommended over field injection,
61 // this results in fewer lines of code.
63 private String userFilePath;
64 @Value("${portalapi.appname}")
65 private String appName;
66 @Value("${portalapi.username}")
67 private String userName;
68 @Value("${portalapi.password}")
69 private String password;
70 @Value("${portalapi.decryptor}")
71 private String decryptor;
72 @Value("${portalapi.usercookie}")
73 private String userCookie;
75 protected void configure(HttpSecurity http) throws Exception {
76 logger.debug("configure: portalapi.username {}", userName);
77 // A chain of ".and()" always baffles me
78 http.authorizeRequests().anyRequest().authenticated();
79 http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
80 http.addFilterBefore(portalAuthenticationFilterBean(), BasicAuthenticationFilter.class);
84 * Resource paths that do not require authentication, especially including
85 * Swagger-generated documentation.
87 public static final String[] OPEN_PATHS = { //
89 "/swagger-resources/**", //
90 "/swagger-ui.html", //
92 PortalApiConstants.API_PREFIX + "/**", //
93 AcXappController.CONTROLLER_PATH + "/" + AcXappController.VERSION_METHOD, //
94 AdminController.CONTROLLER_PATH + "/" + AdminController.HEALTH_METHOD, //
95 AdminController.CONTROLLER_PATH + "/" + AdminController.VERSION_METHOD, //
96 AnrXappController.CONTROLLER_PATH + "/" + AnrXappController.HEALTH_ALIVE_METHOD, //
97 AnrXappController.CONTROLLER_PATH + "/" + AnrXappController.HEALTH_READY_METHOD, //
98 AnrXappController.CONTROLLER_PATH + "/" + AnrXappController.VERSION_METHOD, //
99 AppManagerController.CONTROLLER_PATH + "/" + AppManagerController.HEALTH_ALIVE_METHOD, //
100 AppManagerController.CONTROLLER_PATH + "/" + AppManagerController.HEALTH_READY_METHOD, //
101 AppManagerController.CONTROLLER_PATH + "/" + AppManagerController.VERSION_METHOD, //
102 E2ManagerController.CONTROLLER_PATH + "/" + E2ManagerController.HEALTH_METHOD, //
103 E2ManagerController.CONTROLLER_PATH + "/" + E2ManagerController.VERSION_METHOD, //
104 DashboardConstants.LOGIN_PAGE //
108 public void configure(WebSecurity web) throws Exception {
109 // This disables Spring security, but not the app's filter.
110 web.ignoring().antMatchers(OPEN_PATHS);
114 public PortalAuthManager portalAuthManagerBean()
115 throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
116 return new PortalAuthManager(appName, userName, password, decryptor, userCookie);
120 public DashboardUserManager dashboardUserManagerBean() throws IOException {
121 return new DashboardUserManager(userFilePath);
125 * If this is annotated with @Bean, it is created automatically AND REGISTERED,
126 * and Spring processes annotations in the source of the class. However, the
127 * filter is added in the chain apparently in the wrong order. Alternately, with
128 * no @Bean and added to the chain up in the configure() method in the desired
129 * order, the ignoring() matcher pattern configured above causes Spring to
130 * bypass this filter, which seems to me means the filter participates
133 public PortalAuthenticationFilter portalAuthenticationFilterBean()
134 throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
135 PortalAuthenticationFilter portalAuthenticationFilter = new PortalAuthenticationFilter(portalAuthManagerBean(),
136 dashboardUserManagerBean());
137 return portalAuthenticationFilter;
141 * Instantiates the EPSDK-FW servlet. Needed because this app is not configured
142 * to scan the EPSDK-FW packages; there's also a chance that Spring-Boot does
143 * not automatically process @WebServlet annotations.
145 * @return Servlet registration bean for the Portal Rest API proxy servlet.
148 public ServletRegistrationBean<PortalRestAPIProxy> portalApiProxyServletBean() {
149 PortalRestAPIProxy servlet = new PortalRestAPIProxy();
150 final ServletRegistrationBean<PortalRestAPIProxy> servletBean = new ServletRegistrationBean<>(servlet,
151 PortalApiConstants.API_PREFIX + "/*");
152 servletBean.setName("PortalRestApiProxyServlet");
157 * Instantiates a trivial login servlet that serves a basic page with a link to
158 * authenticate at Portal. The login filter redirects to this page instead of
161 * @return Servlet registration bean for the Dashboard login servlet.
164 public ServletRegistrationBean<LoginServlet> loginServletBean() {
165 LoginServlet servlet = new LoginServlet();
166 final ServletRegistrationBean<LoginServlet> servletBean = new ServletRegistrationBean<>(servlet,
167 DashboardConstants.LOGIN_PAGE);
168 servletBean.setName("LoginServlet");