Search in sources :

Example 1 with CurrentUserNotFoundException

use of org.summerb.security.api.CurrentUserNotFoundException in project summerb by skarpushin.

the class RestExceptionTranslator method determineFailureResult.

protected DtoBase determineFailureResult(Exception ex, HttpServletRequest request, HttpServletResponse response) {
    // first see if it is FVE
    FieldValidationException fve = ExceptionUtils.findExceptionOfType(ex, FieldValidationException.class);
    if (fve != null) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        return fve.getErrorDescriptionObject();
    }
    boolean translateAuthErrors = Boolean.TRUE.equals(Boolean.valueOf(request.getHeader(X_TRANSLATE_AUTHORIZATION_ERRORS)));
    GenericServerErrorResult ret = null;
    if (translateAuthErrors) {
        ret = new GenericServerErrorResult(buildUserMessage(ex, request), new ExceptionInfo(ex));
    }
    NotAuthorizedException naex = ExceptionUtils.findExceptionOfType(ex, NotAuthorizedException.class);
    if (naex != null) {
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        return ret != null ? ret : naex.getResult();
    }
    AuthenticationException ae = ExceptionUtils.findExceptionOfType(ex, AuthenticationException.class);
    if (ae != null) {
        // NOTE: See how we did that in AuthenticationFailureHandlerImpl...
        // Looks like we need to augment our custom RestLoginFilter so it
        // will put username to request
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        return ret != null ? ret : new NotAuthorizedResult("(username not resolved)", SecurityMessageCodes.AUTH_FATAL);
    }
    AccessDeniedException ade = ExceptionUtils.findExceptionOfType(ex, AccessDeniedException.class);
    if (ade != null) {
        if (authenticationTrustResolver.isAnonymous(SecurityContextHolder.getContext().getAuthentication())) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return ret != null ? ret : new NotAuthorizedResult(getCurrentUser(null), SecurityMessageCodes.LOGIN_REQUIRED);
        }
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        return ret != null ? ret : new NotAuthorizedResult(getCurrentUser(null), SecurityMessageCodes.ACCESS_DENIED);
    }
    CurrentUserNotFoundException cunfe = ExceptionUtils.findExceptionOfType(ex, CurrentUserNotFoundException.class);
    if (cunfe != null) {
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        return ret != null ? ret : new NotAuthorizedResult(getCurrentUser(null), SecurityMessageCodes.LOGIN_REQUIRED);
    }
    // TBD: Do we really need to send whole stack trace to client ??? I think we
    // should do it only during development
    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    return new GenericServerErrorResult(buildUserMessage(ex, request), new ExceptionInfo(ex));
}
Also used : FieldValidationException(org.summerb.validation.FieldValidationException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) AuthenticationException(org.springframework.security.core.AuthenticationException) NotAuthorizedResult(org.summerb.security.api.dto.NotAuthorizedResult) CurrentUserNotFoundException(org.summerb.security.api.CurrentUserNotFoundException) NotAuthorizedException(org.summerb.security.api.exceptions.NotAuthorizedException) GenericServerErrorResult(org.summerb.utils.exceptions.dto.GenericServerErrorResult) ExceptionInfo(org.summerb.utils.exceptions.dto.ExceptionInfo)

Example 2 with CurrentUserNotFoundException

use of org.summerb.security.api.CurrentUserNotFoundException in project summerb by skarpushin.

the class SecurityContextResolverAbstract method getUser.

@Override
public TUser getUser() throws CurrentUserNotFoundException {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        throw new CurrentUserNotFoundException();
    }
    Object principal = authentication.getPrincipal();
    if (principal instanceof UserDetails) {
        return getUserFromUserDetails((UserDetails) principal);
    }
    throw new CurrentUserNotFoundException();
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) Authentication(org.springframework.security.core.Authentication) CurrentUserNotFoundException(org.summerb.security.api.CurrentUserNotFoundException)

Aggregations

CurrentUserNotFoundException (org.summerb.security.api.CurrentUserNotFoundException)2 AccessDeniedException (org.springframework.security.access.AccessDeniedException)1 Authentication (org.springframework.security.core.Authentication)1 AuthenticationException (org.springframework.security.core.AuthenticationException)1 UserDetails (org.springframework.security.core.userdetails.UserDetails)1 NotAuthorizedResult (org.summerb.security.api.dto.NotAuthorizedResult)1 NotAuthorizedException (org.summerb.security.api.exceptions.NotAuthorizedException)1 ExceptionInfo (org.summerb.utils.exceptions.dto.ExceptionInfo)1 GenericServerErrorResult (org.summerb.utils.exceptions.dto.GenericServerErrorResult)1 FieldValidationException (org.summerb.validation.FieldValidationException)1