2 * ============LICENSE_START=======================================================
3 * Copyright (c) 2021 Bell Canada.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * 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.temporal.controller.rest.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.WebSecurityConfigurerAdapter;
31 * Configuration class to implement application security. It enforces Basic Authentication access control.
34 public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
36 private static final String USER_ROLE = "USER";
38 private final String username;
39 private final String password;
40 private final String[] permitUris;
43 * Constructor. Accepts parameters from configuration.
45 * @param permitUris comma-separated list of uri patterns for endpoints permitted
46 * @param username username
47 * @param password password
49 public WebSecurityConfig(
50 @Autowired @Value("${security.permit-uri}") final String permitUris,
51 @Autowired @Value("${security.auth.username}") final String username,
52 @Autowired @Value("${security.auth.password}") final String password
56 permitUris.isEmpty() ? new String[]{"/swagger/openapi.yml"} : permitUris.split("\\s{0,9},\\s{0,9}");
57 this.username = username;
58 this.password = password;
62 // The team decided to disable default CSRF Spring protection and not implement CSRF tokens validation.
63 // CPS is a stateless REST API that is not as vulnerable to CSRF attacks as web applications running in
64 // web browsers are. CPS does not manage sessions, each request requires the authentication token in the header.
65 // See https://docs.spring.io/spring-security/site/docs/5.3.8.RELEASE/reference/html5/#csrf
66 @SuppressWarnings("squid:S4502")
67 protected void configure(final HttpSecurity http) throws Exception {
71 .antMatchers(permitUris).permitAll()
72 .anyRequest().authenticated()
77 protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
78 auth.inMemoryAuthentication().withUser(username).password("{noop}" + password).roles(USER_ROLE);