2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property
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;
24 import java.lang.reflect.InvocationTargetException;
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.A1Controller;
28 import org.oransc.ric.portal.dashboard.controller.SimpleErrorController;
29 import org.oransc.ric.portal.dashboard.portalapi.PortalAuthManager;
30 import org.oransc.ric.portal.dashboard.portalapi.PortalAuthenticationFilter;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.beans.factory.annotation.Value;
35 import org.springframework.context.annotation.Bean;
36 import org.springframework.context.annotation.Configuration;
37 import org.springframework.context.annotation.Profile;
38 import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
39 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
40 import org.springframework.security.config.annotation.web.builders.WebSecurity;
41 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
42 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
43 import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
44 import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
48 @EnableGlobalMethodSecurity(securedEnabled = true)
50 public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
52 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
54 // Although constructor arguments are recommended over field injection,
55 // this results in fewer lines of code.
56 @Value("${portalapi.security}")
57 private Boolean portalapiSecurity;
58 @Value("${portalapi.appname}")
59 private String appName;
60 @Value("${portalapi.username}")
61 private String userName;
62 @Value("${portalapi.password}")
63 private String password;
64 @Value("${portalapi.decryptor}")
65 private String decryptor;
66 @Value("${portalapi.usercookie}")
67 private String userCookie;
70 DashboardUserManager userManager;
73 protected void configure(HttpSecurity http) throws Exception {
74 logger.debug("configure: portalapi.username {}", userName);
75 // A chain of ".and()" always baffles me
76 http.authorizeRequests().anyRequest().authenticated();
77 http.headers().frameOptions().disable();
78 http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
79 http.addFilterBefore(portalAuthenticationFilterBean(), BasicAuthenticationFilter.class);
83 * Resource paths that do not require authentication, especially including
84 * Swagger-generated documentation.
86 public static final String[] OPEN_PATHS = { //
88 "/swagger-resources/**", //
89 "/swagger-ui.html", //
91 PortalApiConstants.API_PREFIX + "/**", //
92 A1Controller.CONTROLLER_PATH + "/" + A1Controller.VERSION_METHOD, //
93 SimpleErrorController.ERROR_PATH };
96 public void configure(WebSecurity web) throws Exception {
97 // This disables Spring security, but not the app's filter.
98 web.ignoring().antMatchers(OPEN_PATHS);
102 public PortalAuthManager portalAuthManagerBean()
103 throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException,
104 IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
105 return new PortalAuthManager(appName, userName, password, decryptor, userCookie);
109 * If this is annotated with @Bean, it is created automatically AND REGISTERED,
110 * and Spring processes annotations in the source of the class. However, the
111 * filter is added in the chain apparently in the wrong order. Alternately, with
112 * no @Bean and added to the chain up in the configure() method in the desired
113 * order, the ignoring() matcher pattern configured above causes Spring to
114 * bypass this filter, which seems to me means the filter participates
117 public PortalAuthenticationFilter portalAuthenticationFilterBean()
118 throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException,
119 IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
120 PortalAuthenticationFilter portalAuthenticationFilter = new PortalAuthenticationFilter(portalapiSecurity,
121 portalAuthManagerBean(), this.userManager);
122 return portalAuthenticationFilter;