f4819d307d79863b0173685d054217c0b14d57d6
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / config / WebSecurityConfiguration.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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===================================
19  */
20 package org.oransc.ric.portal.dashboard.config;
21
22 import java.lang.invoke.MethodHandles;
23 import java.lang.reflect.InvocationTargetException;
24
25 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
26 import org.oransc.ric.portal.dashboard.DashboardUserManager;
27 import org.oransc.ric.portal.dashboard.controller.A1MediatorController;
28 import org.oransc.ric.portal.dashboard.controller.AdminController;
29 import org.oransc.ric.portal.dashboard.controller.AppManagerController;
30 import org.oransc.ric.portal.dashboard.controller.E2ManagerController;
31 import org.oransc.ric.portal.dashboard.controller.SimpleErrorController;
32 import org.oransc.ric.portal.dashboard.portalapi.PortalAuthManager;
33 import org.oransc.ric.portal.dashboard.portalapi.PortalAuthenticationFilter;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.beans.factory.annotation.Value;
38 import org.springframework.context.annotation.Bean;
39 import org.springframework.context.annotation.Configuration;
40 import org.springframework.context.annotation.Profile;
41 import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
42 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
43 import org.springframework.security.config.annotation.web.builders.WebSecurity;
44 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
45 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
46 import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
47 import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
48
49 @Configuration
50 @EnableWebSecurity
51 @EnableGlobalMethodSecurity(securedEnabled = true)
52 @Profile("!test")
53 public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
54
55         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
56
57         // Although constructor arguments are recommended over field injection,
58         // this results in fewer lines of code.
59         @Value("${portalapi.security}")
60         private Boolean portalapiSecurity;
61         @Value("${portalapi.appname}")
62         private String appName;
63         @Value("${portalapi.username}")
64         private String userName;
65         @Value("${portalapi.password}")
66         private String password;
67         @Value("${portalapi.decryptor}")
68         private String decryptor;
69         @Value("${portalapi.usercookie}")
70         private String userCookie;
71
72         @Autowired
73         DashboardUserManager userManager;
74
75         @Override
76         protected void configure(HttpSecurity http) throws Exception {
77                 logger.debug("configure: portalapi.username {}", userName);
78                 // A chain of ".and()" always baffles me
79                 http.authorizeRequests().anyRequest().authenticated();
80                 http.headers().frameOptions().disable();
81                 http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
82                 http.addFilterBefore(portalAuthenticationFilterBean(), BasicAuthenticationFilter.class);
83         }
84
85         /**
86          * Resource paths that do not require authentication, especially including
87          * Swagger-generated documentation.
88          */
89         protected static final String[] OPEN_PATHS = { //
90                         "/v2/api-docs", //
91                         "/swagger-resources/**", //
92                         "/swagger-ui.html", //
93                         "/webjars/**", //
94                         PortalApiConstants.API_PREFIX + "/**", //
95                         A1MediatorController.CONTROLLER_PATH + "/" + A1MediatorController.VERSION_METHOD, //
96                         AdminController.CONTROLLER_PATH + "/" + AdminController.HEALTH_METHOD, //
97                         AdminController.CONTROLLER_PATH + "/" + AdminController.VERSION_METHOD, //
98                         AppManagerController.CONTROLLER_PATH + "/" + AppManagerController.HEALTH_ALIVE_METHOD, //
99                         AppManagerController.CONTROLLER_PATH + "/" + AppManagerController.HEALTH_READY_METHOD, //
100                         AppManagerController.CONTROLLER_PATH + "/" + AppManagerController.VERSION_METHOD, //
101                         E2ManagerController.CONTROLLER_PATH + "/" + E2ManagerController.HEALTH_METHOD, //
102                         E2ManagerController.CONTROLLER_PATH + "/" + E2ManagerController.VERSION_METHOD, //
103                         SimpleErrorController.ERROR_PATH };
104
105         @Override
106         public void configure(WebSecurity web) throws Exception {
107                 // This disables Spring security, but not the app's filter.
108                 web.ignoring().antMatchers(OPEN_PATHS);
109         }
110
111         @Bean
112         public PortalAuthManager portalAuthManagerBean() throws ClassNotFoundException, IllegalAccessException,
113                         InstantiationException, InvocationTargetException, NoSuchMethodException {
114                 return new PortalAuthManager(appName, userName, password, decryptor, userCookie);
115         }
116
117         /*
118          * If this is annotated with @Bean, it is created automatically AND REGISTERED,
119          * and Spring processes annotations in the source of the class. However, the
120          * filter is added in the chain apparently in the wrong order. Alternately, with
121          * no @Bean and added to the chain up in the configure() method in the desired
122          * order, the ignoring() matcher pattern configured above causes Spring to
123          * bypass this filter, which seems to me means the filter participates
124          * correctly.
125          */
126         public PortalAuthenticationFilter portalAuthenticationFilterBean() throws ClassNotFoundException,
127                         IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
128                 return new PortalAuthenticationFilter(portalapiSecurity, portalAuthManagerBean(), this.userManager);
129         }
130
131 }