22
33import java .io .IOException ;
44
5+ import javax .annotation .Resource ;
56import javax .servlet .ServletException ;
67import javax .servlet .http .HttpServletRequest ;
78import javax .servlet .http .HttpServletResponse ;
89
10+ import org .orcid .core .manager .impl .OrcidUrlManager ;
911import org .slf4j .Logger ;
1012import org .slf4j .LoggerFactory ;
1113import org .springframework .security .access .AccessDeniedException ;
14+ import org .springframework .security .core .Authentication ;
1215import org .springframework .security .web .access .AccessDeniedHandlerImpl ;
1316import org .springframework .security .web .csrf .CsrfException ;
1417import org .springframework .web .context .request .RequestContextHolder ;
18+ import org .springframework .security .core .context .SecurityContextHolder ;
1519
1620public class OrcidAccessDeniedHandler extends AccessDeniedHandlerImpl {
1721
22+ @ Resource
23+ private OrcidUrlManager orcidUrlManager ;
24+
1825 private static final Logger LOGGER = LoggerFactory .getLogger (OrcidAccessDeniedHandler .class );
1926
2027 public void handle (HttpServletRequest request , HttpServletResponse response , AccessDeniedException accessDeniedException ) throws IOException , ServletException {
@@ -36,6 +43,13 @@ public void handle(HttpServletRequest request, HttpServletResponse response, Acc
3643 }
3744 }
3845
46+ // Check if the current user is authenticated
47+ Authentication auth = SecurityContextHolder .getContext ().getAuthentication ();
48+ if (auth != null && auth .isAuthenticated ()) {
49+ response .sendRedirect (orcidUrlManager .getBaseUrl () + "/404" );
50+ return ;
51+ }
52+
3953 super .handle (request , response , accessDeniedException );
4054 }
4155}
0 commit comments