AuthorizationFailureEventListener.java

  1. /*
  2.  * Copyright 2010-2025 James Pether Sörling
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *
  16.  *  $Id$
  17.  *  $HeadURL$
  18. */
  19. package com.hack23.cia.web.impl.ui.application.web.listener;

  20. import java.text.MessageFormat;
  21. import java.util.Collection;

  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  24. import org.springframework.aop.framework.ReflectiveMethodInvocation;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.context.ApplicationListener;
  27. import org.springframework.security.access.ConfigAttribute;
  28. import org.springframework.security.access.event.AuthorizationFailureEvent;
  29. import org.springframework.security.core.GrantedAuthority;
  30. import org.springframework.stereotype.Service;
  31. import org.springframework.web.context.request.RequestContextHolder;

  32. import com.hack23.cia.model.internal.application.system.impl.ApplicationEventGroup;
  33. import com.hack23.cia.model.internal.application.system.impl.ApplicationOperationType;
  34. import com.hack23.cia.service.api.ApplicationManager;
  35. import com.hack23.cia.service.api.action.application.CreateApplicationEventRequest;
  36. import com.hack23.cia.web.impl.ui.application.util.UserContextUtil;
  37. import com.vaadin.server.Page;
  38. import com.vaadin.ui.UI;

  39. /**
  40.  * The Class AuthorizationFailureEventListener.
  41.  *
  42.  * @see AuthorizationFailureEventEvent
  43.  */
  44. @Service
  45. public final class AuthorizationFailureEventListener implements ApplicationListener<AuthorizationFailureEvent> {

  46.     /** The Constant ACCESS_DENIED. */
  47.     private static final String ACCESS_DENIED = "Access Denied";

  48.     /** The Constant AUTHORITIES. */
  49.     private static final String AUTHORITIES = "Authorities:";

  50.     /** The Constant CRLF. */
  51.     private static final String CRLF = "[\r\n]";

  52.     /** The Constant CRLF_REPLACEMENT. */
  53.     private static final String CRLF_REPLACEMENT = "";

  54.     /** The Constant ERROR_MESSAGE_FORMAT. */
  55.     private static final String ERROR_MESSAGE_FORMAT = "SECURITY:Url:{0} , Method{1} ,{2}{3}{4}{5} source:{6}";

  56.     /**
  57.      * The Constant
  58.      * LOG_MSG_AUTHORIZATION_FAILURE_SESSION_ID_AUTHORITIES_REQUIRED_AUTHORITIES.
  59.      */
  60.     private static final String LOG_MSG_AUTHORIZATION_FAILURE_SESSION_ID_AUTHORITIES_REQUIRED_AUTHORITIES = "SECURITY:Authorization Failure:: url : {} Method : {} SessionId :{} , Authorities : {} , RequiredAuthorities : {}";

  61.     /** The Constant LOGGER. */
  62.     private static final Logger LOGGER = LoggerFactory.getLogger(AuthorizationFailureEventListener.class);

  63.     /** The Constant REQUIRED_AUTHORITIES. */
  64.     private static final String REQUIRED_AUTHORITIES = " , RequiredAuthorities:";

  65.     /** The application manager. */
  66.     @Autowired
  67.     private ApplicationManager applicationManager;

  68.     /**
  69.      * Instantiates a new authorization failure event listener.
  70.      */
  71.     public AuthorizationFailureEventListener() {
  72.         super();
  73.     }

  74.     @Override
  75.     public void onApplicationEvent(final AuthorizationFailureEvent authorizationFailureEvent) {

  76.         final String sessionId = RequestContextHolder.currentRequestAttributes().getSessionId();

  77.         final CreateApplicationEventRequest serviceRequest = new CreateApplicationEventRequest();
  78.         serviceRequest.setSessionId(sessionId);

  79.         serviceRequest.setEventGroup(ApplicationEventGroup.APPLICATION);
  80.         serviceRequest.setApplicationOperation(ApplicationOperationType.AUTHORIZATION);

  81.         serviceRequest.setUserId(UserContextUtil.getUserIdFromSecurityContext());

  82.         final Page currentPageIfAny = Page.getCurrent();
  83.         final String requestUrl = UserContextUtil.getRequestUrl(currentPageIfAny);
  84.         final UI currentUiIfAny = UI.getCurrent();
  85.         String methodInfo = "";

  86.         if (currentPageIfAny != null && currentUiIfAny != null && currentUiIfAny.getNavigator() != null
  87.                 && currentUiIfAny.getNavigator().getCurrentView() != null) {
  88.             serviceRequest.setPage(currentUiIfAny.getNavigator().getCurrentView().getClass().getSimpleName());
  89.             serviceRequest.setPageMode(currentPageIfAny.getUriFragment());
  90.         }

  91.         if (authorizationFailureEvent.getSource() instanceof final ReflectiveMethodInvocation methodInvocation) {
  92.             if (methodInvocation.getThis() != null) {
  93.                 methodInfo = new StringBuilder().append(methodInvocation.getThis().getClass().getSimpleName())
  94.                         .append('.').append(methodInvocation.getMethod().getName()).toString();
  95.             }
  96.         }

  97.         final Collection<? extends GrantedAuthority> authorities = authorizationFailureEvent.getAuthentication()
  98.                 .getAuthorities();
  99.         final Collection<ConfigAttribute> configAttributes = authorizationFailureEvent.getConfigAttributes();

  100.         serviceRequest.setErrorMessage(MessageFormat.format(ERROR_MESSAGE_FORMAT, requestUrl, methodInfo, AUTHORITIES,
  101.                 authorities, REQUIRED_AUTHORITIES, configAttributes, authorizationFailureEvent.getSource()));
  102.         serviceRequest.setApplicationMessage(ACCESS_DENIED);

  103.         applicationManager.service(serviceRequest);

  104.         LOGGER.info(LOG_MSG_AUTHORIZATION_FAILURE_SESSION_ID_AUTHORITIES_REQUIRED_AUTHORITIES,
  105.                 requestUrl.replaceAll(CRLF, CRLF_REPLACEMENT), methodInfo.replaceAll(CRLF, CRLF_REPLACEMENT),
  106.                 sessionId.replaceAll(CRLF, CRLF_REPLACEMENT), authorities, configAttributes);
  107.     }

  108. }