2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
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=========================================================
21 package org.onap.aai.aaf.filters;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.onap.aaf.cadi.PropAccess;
26 import org.onap.aaf.cadi.filter.CadiFilter;
27 import org.onap.aai.aaf.auth.ResponseFormatter;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.boot.web.filter.OrderedRequestContextFilter;
30 import org.springframework.context.annotation.Profile;
31 import org.springframework.stereotype.Component;
33 import javax.servlet.FilterChain;
34 import javax.servlet.ServletException;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
37 import java.io.IOException;
41 * AAF authentication filter
45 @Profile(AafProfiles.AAF_AUTHENTICATION)
46 public class AafFilter extends OrderedRequestContextFilter {
48 private static final Logger LOGGER = LoggerFactory.getLogger(AafCertFilter.class);
50 private final CadiFilter cadiFilter;
53 public AafFilter(CadiProps cadiProps) throws IOException, ServletException {
54 cadiFilter = new CadiFilter(new PropAccess((level,element)->{
57 LOGGER.debug(buildMsg(element));
61 LOGGER.info(buildMsg(element));
64 LOGGER.warn(buildMsg(element));
67 LOGGER.error(buildMsg(element));
70 LOGGER.info(buildMsg(element));
73 LOGGER.trace(buildMsg(element));
78 }, new String[]{"cadi_prop_files=" + cadiProps.getCadiFileName()} ));
79 this.setOrder(FilterPriority.AAF_AUTHENTICATION.getPriority());
83 protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
84 if (!request.getRequestURI().matches("^.*/util/echo$")) {
85 cadiFilter.doFilter(request, response, filterChain);
86 if (response.getStatus() == 401 || response.getStatus() == 403) {
87 ResponseFormatter.errorResponse(request, response);
90 filterChain.doFilter(request, response);
94 private String buildMsg(Object[] objects) {
95 StringBuilder sb = new StringBuilder();
97 for ( Object o: objects ) {
104 sb.append(o.toString());
106 return (sb.toString());