2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property
6 * Modifications Copyright (C) 2019 Nordix Foundation
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ========================LICENSE_END===================================
21 package org.oransc.ric.portal.dashboard.config;
23 import java.io.IOException;
24 import java.lang.invoke.MethodHandles;
25 import java.lang.reflect.InvocationTargetException;
27 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
28 import org.oransc.ric.portal.dashboard.DashboardUserManager;
29 import org.oransc.ric.portal.dashboard.controller.SimpleErrorController;
30 import org.oransc.ric.portal.dashboard.portalapi.PortalAuthManager;
31 import org.oransc.ric.portal.dashboard.portalapi.PortalAuthenticationFilter;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.beans.factory.annotation.Value;
36 import org.springframework.context.annotation.Bean;
37 import org.springframework.context.annotation.Configuration;
38 import org.springframework.context.annotation.Profile;
39 import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
40 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
41 import org.springframework.security.config.annotation.web.builders.WebSecurity;
42 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
43 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
44 import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
45 import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
46 import org.oransc.ric.portal.dashboard.controller.PolicyController;
50 @EnableGlobalMethodSecurity(securedEnabled = true)
52 public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
54 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
56 // Although constructor arguments are recommended over field injection,
57 // this results in fewer lines of code.
58 @Value("${portalapi.security}")
59 private Boolean portalapiSecurity;
60 @Value("${portalapi.appname}")
61 private String appName;
62 @Value("${portalapi.username}")
63 private String userName;
64 @Value("${portalapi.password}")
65 private String password;
66 @Value("${portalapi.decryptor}")
67 private String decryptor;
68 @Value("${portalapi.usercookie}")
69 private String userCookie;
72 DashboardUserManager userManager;
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.headers().frameOptions().disable();
80 http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
81 http.addFilterBefore(portalAuthenticationFilterBean(), BasicAuthenticationFilter.class);
85 * Resource paths that do not require authentication, especially including
86 * Swagger-generated documentation.
88 public static final String[] OPEN_PATHS = { //
90 "/swagger-resources/**", //
91 "/swagger-ui.html", //
93 PortalApiConstants.API_PREFIX + "/**", //
94 PolicyController.CONTROLLER_PATH + "/" + PolicyController.VERSION_METHOD, //
95 SimpleErrorController.ERROR_PATH };
98 public void configure(WebSecurity web) throws Exception {
99 // This disables Spring security, but not the app's filter.
100 web.ignoring().antMatchers(OPEN_PATHS);
104 public PortalAuthManager portalAuthManagerBean()
105 throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException,
106 IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
107 return new PortalAuthManager(appName, userName, password, decryptor, userCookie);
111 * If this is annotated with @Bean, it is created automatically AND REGISTERED,
112 * and Spring processes annotations in the source of the class. However, the
113 * filter is added in the chain apparently in the wrong order. Alternately, with
114 * no @Bean and added to the chain up in the configure() method in the desired
115 * order, the ignoring() matcher pattern configured above causes Spring to
116 * bypass this filter, which seems to me means the filter participates
119 public PortalAuthenticationFilter portalAuthenticationFilterBean()
120 throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException,
121 IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
122 PortalAuthenticationFilter portalAuthenticationFilter = new PortalAuthenticationFilter(portalapiSecurity,
123 portalAuthManagerBean(), this.userManager);
124 return portalAuthenticationFilter;