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.portal.nonrtric.controlpanel.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.portal.nonrtric.controlpanel.ControlPanelUserManager;
29 import org.oransc.portal.nonrtric.controlpanel.controller.PolicyController;
30 import org.oransc.portal.nonrtric.controlpanel.controller.SimpleErrorController;
31 import org.oransc.portal.nonrtric.controlpanel.portalapi.PortalAuthManager;
32 import org.oransc.portal.nonrtric.controlpanel.portalapi.PortalAuthenticationFilter;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.beans.factory.annotation.Value;
37 import org.springframework.context.annotation.Bean;
38 import org.springframework.context.annotation.Configuration;
39 import org.springframework.context.annotation.Profile;
40 import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
41 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
42 import org.springframework.security.config.annotation.web.builders.WebSecurity;
43 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
44 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
45 import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
46 import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
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 ControlPanelUserManager 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 @SuppressWarnings("squid:S1075") // URIs should not be hardcoded
89 public static final String[] OPEN_PATHS = { //
91 "/swagger-resources/**", //
92 "/swagger-ui.html", //
94 PortalApiConstants.API_PREFIX + "/**", //
95 PolicyController.CONTROLLER_PATH + "/" + PolicyController.VERSION_METHOD, //
96 SimpleErrorController.ERROR_PATH};
99 public void configure(WebSecurity web) throws Exception {
100 // This disables Spring security, but not the app's filter.
101 web.ignoring().antMatchers(OPEN_PATHS);
105 public PortalAuthManager portalAuthManagerBean() throws ClassNotFoundException, InstantiationException,
106 IllegalAccessException, InvocationTargetException, NoSuchMethodException {
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() throws ClassNotFoundException,
120 InstantiationException, IllegalAccessException, IOException, InvocationTargetException, NoSuchMethodException {
121 return new PortalAuthenticationFilter(portalapiSecurity, portalAuthManagerBean(), this.userManager);