From 68461377f6835be3a9a2bfa0b09bbade336729d5 Mon Sep 17 00:00:00 2001 From: amontenegro Date: Mon, 10 Feb 2025 18:51:26 -0600 Subject: [PATCH] Session events are ignored for public api page calls --- .../OrcidRedisIndexedSessionRepository.java | 37 ++-- .../resources/orcid-frontend-security.xml | 166 +++++++----------- 2 files changed, 82 insertions(+), 121 deletions(-) diff --git a/orcid-web/src/main/java/org/orcid/frontend/spring/session/redis/OrcidRedisIndexedSessionRepository.java b/orcid-web/src/main/java/org/orcid/frontend/spring/session/redis/OrcidRedisIndexedSessionRepository.java index 4f156b96b0..57fcba0b08 100644 --- a/orcid-web/src/main/java/org/orcid/frontend/spring/session/redis/OrcidRedisIndexedSessionRepository.java +++ b/orcid-web/src/main/java/org/orcid/frontend/spring/session/redis/OrcidRedisIndexedSessionRepository.java @@ -2,6 +2,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.orcid.api.common.analytics.APIEndpointParser; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEventPublisher; @@ -52,15 +53,15 @@ public class OrcidRedisIndexedSessionRepository implements FindByIndexNameSessio private RedisSerializer defaultSerializer = new JdkSerializationRedisSerializer(); private FlushMode flushMode; private SaveMode saveMode; - private final List urisToSkip = List.of("/account/biographyForm.json", "/account/countryForm.json", "/account/emails.json", - "/account/nameForm.json", "/account/preferences.json", "/affiliations/affiliationGroups.json", "/affiliations/affiliationDetails.json", - "/assets/vectors/orcid.logo.icon.svg", "/config.json", "/delegators/delegators-and-me.json", "/fundings/fundingDetails.json", - "/fundings/fundingGroups.json", "/inbox/unreadCount.json", "/my-orcid/externalIdentifiers.json", "/my-orcid/keywordsForms.json", - "/my-orcid/otherNamesForms.json", "/my-orcid/websitesForms.json", "/peer-reviews/peer-reviews-minimized.json", - "/research-resources/researchResourcePage.json", "/research-resources/researchResource.json", "/works/worksExtendedPage.json", - "/works/groupingSuggestions.json", "/works/getWorkInfo.json", "/works/work.json", "/works/idTypes.json", "/orgs/disambiguated/ROR", - "/orgs/disambiguated/GRID", "/orgs/disambiguated/FUNDREF", "/ng-cli-ws", "/not-found", "/peer-reviews/peer-review.json", - "/peer-reviews/peer-reviews-by-group-id.json"); + private final String PUBLIC_ORCID_PAGE_REGEX = "/(\\d{4}-){3,}\\d{3}[\\dX](/.+)"; + private final List urisToSkip = List.of("/2FA/status.json", "/account/", "/account/biographyForm.json", "/account/countryForm.json", "/account/delegates.json", "/account/emails.json", + "/account/get-trusted-orgs.json", "/account/nameForm.json", "/account/preferences.json", "/account/socialAccounts.json", "/affiliations/affiliationDetails.json", "/affiliations/affiliationGroups.json", + "/assets/vectors/orcid.logo.icon.svg", "/config.json", "/delegators/delegators-and-me.json", "/fundings/fundingDetails.json", "/fundings/fundingGroups.json", "/inbox/notifications.json", + "/inbox/totalCount.json", "/inbox/unreadCount.json", "/my-orcid/externalIdentifiers.json", "/my-orcid/keywordsForms.json", "/my-orcid/otherNamesForms.json", "/my-orcid/websitesForms.json", + "/ng-cli-ws", "/not-found", "/notifications/frequencies/view", "/orgs/disambiguated/FUNDREF", "/orgs/disambiguated/GRID", "/orgs/disambiguated/LEI", "/orgs/disambiguated/RINGGOLD", + "/orgs/disambiguated/ROR", "/peer-reviews/peer-review.json", "/peer-reviews/peer-reviews-by-group-id.json", "/peer-reviews/peer-reviews-minimized.json", "/qr-code.png", + "/research-resources/researchResource.json", "/research-resources/researchResourcePage.json", "/works/getWorkInfo.json", "/works/groupingSuggestions.json", "/works/idTypes.json", "/works/work.json", + "/works/worksExtendedPage.json"); private final Set SKIP_SAVE_SESSION = new HashSet<>(urisToSkip); public OrcidRedisIndexedSessionRepository(RedisOperations sessionRedisOperations) { @@ -129,7 +130,7 @@ public void save(OrcidRedisIndexedSessionRepository.RedisSession session) { if(updateSession()) { //TODO: REMOVE THIS LOG ENTRY BEFORE GOING LIVE!!!! - logger.info("Saving session for " + request.getRequestURI()); + logger.info("Saving session for " + request.getRequestURI() + " - " + request.getMethod()); session.save(); if (session.isNew) { String sessionCreatedKey = this.getSessionCreatedChannel(session.getId()); @@ -138,7 +139,7 @@ public void save(OrcidRedisIndexedSessionRepository.RedisSession session) { } } else { //TODO: REMOVE THIS LOG ENTRY BEFORE GOING LIVE!!!! - logger.info("Skip save session id " + request.getRequestURI()); + logger.info("Skip save session id " + request.getRequestURI() + " - " + request.getMethod()); } } @@ -205,11 +206,11 @@ private MapSession loadSession(String id, Map entries) { /////////////////////////////////////////////// if(updateSession()) { // TODO: REMOVE THIS LOG ENTRY BEFORE GOING LIVE!!! - logger.info("Updating last accessed time for " + request.getRequestURI()); + logger.info("Updating last accessed time for " + request.getRequestURI() + " - " + request.getMethod()); loaded.setLastAccessedTime(Instant.ofEpochMilli((Long) entry.getValue())); } else { // TODO: REMOVE THIS LOG ENTRY BEFORE GOING LIVE!!! - logger.info("Ignoring last accessed time for " + request.getRequestURI()); + logger.info("Ignoring last accessed time for " + request.getRequestURI() + " - " + request.getMethod()); } } else if (key.startsWith("sessionAttr:")) { loaded.setAttribute(key.substring("sessionAttr:".length()), entry.getValue()); @@ -362,7 +363,13 @@ private BoundHashOperations getSessionBoundHashOperation private boolean updateSession() { ServletRequestAttributes att = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); HttpServletRequest request = att.getRequest(); - return (!SKIP_SAVE_SESSION.contains(request.getRequestURI().substring(request.getContextPath().length()))); + if(request.getMethod().equals("GET")) { + String url = request.getRequestURI().substring(request.getContextPath().length()); + if(SKIP_SAVE_SESSION.contains(url) || url.matches(PUBLIC_ORCID_PAGE_REGEX)) { + return false; + } + } + return true; } static String getSessionAttrNameKey(String attributeName) { @@ -402,7 +409,7 @@ public void setLastAccessedTime(Instant lastAccessedTime) { // TODO: REMOVE THIS BEFORE GOING LIVE!!!! ServletRequestAttributes att = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); HttpServletRequest request = att.getRequest(); - System.out.println("REDIS_SESSION: setLastAccessedTime: " + request.getRequestURI().toString()); + System.out.println("REDIS_SESSION: setLastAccessedTime: " + request.getRequestURI().toString() + " - " + request.getMethod()); this.cached.setLastAccessedTime(lastAccessedTime); this.delta.put("lastAccessedTime", this.getLastAccessedTime().toEpochMilli()); diff --git a/orcid-web/src/main/resources/orcid-frontend-security.xml b/orcid-web/src/main/resources/orcid-frontend-security.xml index 647bfe735c..1b99b724eb 100644 --- a/orcid-web/src/main/resources/orcid-frontend-security.xml +++ b/orcid-web/src/main/resources/orcid-frontend-security.xml @@ -102,62 +102,81 @@ - - - + + + - + - - - - - - - - - - - - + + + + - + - - - + + - - + + - - - + + + - + - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -282,8 +301,8 @@ username-parameter="userId" password-parameter="password" default-target-url="/social/access" /> - - + + @@ -294,57 +313,13 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + @@ -436,11 +394,7 @@ - - + access="ROLE_USER"/>