2 * ============LICENSE_START=======================================================
3 * Copyright (c) 2021 Bell Canada.
4 * Modification Copyright (C) 2021 Pantheon.tech
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.config;
23 import org.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.beans.factory.annotation.Value;
25 import org.springframework.context.annotation.Configuration;
26 import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
27 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
28 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
29 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
32 * Configuration class to implement application security.
33 * It enforces Basic Authentication access control.
37 public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
39 private static final String USER_ROLE = "USER";
41 private final String username;
42 private final String password;
43 private final String[] permitUris;
46 * Constructor. Accepts parameters from configuration.
48 * @param permitUris comma-separated list of uri patterns for endpoints permitted
49 * @param username username
50 * @param password password
52 public WebSecurityConfig(
53 @Autowired @Value("${security.permit-uri}") final String permitUris,
54 @Autowired @Value("${security.auth.username}") final String username,
55 @Autowired @Value("${security.auth.password}") final String password
58 this.permitUris = permitUris.isEmpty() ? new String[] {"/v3/api-docs"} : permitUris.split("\\s{0,9},\\s{0,9}");
59 this.username = username;
60 this.password = password;
64 // The team decided to disable default CSRF Spring protection and not implement CSRF tokens validation.
65 // CPS is a stateless REST API that is not as vulnerable to CSRF attacks as web applications running in
66 // web browsers are. CPS does not manage sessions, each request requires the authentication token in the header.
67 // See https://docs.spring.io/spring-security/site/docs/5.3.8.RELEASE/reference/html5/#csrf
68 @SuppressWarnings("squid:S4502")
69 protected void configure(final HttpSecurity http) throws Exception {
73 .antMatchers(permitUris).permitAll()
74 .anyRequest().authenticated()
79 protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
80 auth.inMemoryAuthentication().withUser(username).password("{noop}" + password).roles(USER_ROLE);