Skip to content

Commit

Permalink
jdbc sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
david-blasby committed Dec 4, 2024
1 parent b62e6c2 commit c2d3d94
Show file tree
Hide file tree
Showing 15 changed files with 254 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public void logout(HttpServletRequest request,
UserSession userSession = (UserSession) tmp;
userSession.clear();
}
httpSession.invalidate();
}

}
Expand Down
35 changes: 17 additions & 18 deletions core/src/main/java/jeeves/server/UserSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

package jeeves.server;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.fao.geonet.domain.LDAPUser;
import org.fao.geonet.domain.Profile;
import org.fao.geonet.domain.User;
Expand All @@ -37,17 +40,19 @@

import javax.servlet.http.HttpSession;

import java.io.Serializable;
import java.util.Hashtable;

//=============================================================================

/**
* Abstraction layer from the user session.
*/
public class UserSession {
public class UserSession implements Serializable {

@JsonProperty
private Hashtable<String, Object> htProperties = new Hashtable<String, Object>(10, .75f);

private HttpSession sHttpSession;

//--------------------------------------------------------------------------
//---
Expand All @@ -64,19 +69,6 @@ public UserSession() {
//---
//--------------------------------------------------------------------------

/**
* @return the sHttpSession
*/
public HttpSession getsHttpSession() {
return sHttpSession;
}

/**
* @param sHttpSession the sHttpSession to set
*/
public void setsHttpSession(HttpSession sHttpSession) {
this.sHttpSession = sHttpSession;
}

/**
* Sets a generic property.
Expand Down Expand Up @@ -109,9 +101,6 @@ public void removeProperty(String name) {
public void clear() {
htProperties.clear();
SecurityContextHolder.clearContext();
if (sHttpSession != null) {
sHttpSession.invalidate();
}
}

//--------------------------------------------------------------------------
Expand All @@ -124,12 +113,14 @@ public void loginAs(User user) {
SecurityContextHolder.setContext(secContext);
}

@JsonIgnore
public boolean isAuthenticated() {
return !(auth() instanceof AnonymousAuthenticationToken);
}

//--------------------------------------------------------------------------

@JsonIgnore
public String getUserId() {
User userDetails = getPrincipal();
if (userDetails == null) {
Expand All @@ -139,6 +130,7 @@ public String getUserId() {
}
}

@JsonIgnore
public String getUsername() {
User userDetails = getPrincipal();
if (userDetails == null) {
Expand All @@ -148,6 +140,7 @@ public String getUsername() {
}
}

@JsonIgnore
public String getName() {
User userDetails = getPrincipal();
if (userDetails == null) {
Expand All @@ -157,6 +150,7 @@ public String getName() {
}
}

@JsonIgnore
public String getSurname() {
User userDetails = getPrincipal();
if (userDetails == null) {
Expand All @@ -166,6 +160,7 @@ public String getSurname() {
}
}

@JsonIgnore
public Profile getProfile() {
User userDetails = getPrincipal();
if (userDetails == null) {
Expand All @@ -175,6 +170,7 @@ public Profile getProfile() {
}
}

@JsonIgnore
public String getEmailAddr() {
User userDetails = getPrincipal();
if (userDetails == null) {
Expand All @@ -184,6 +180,7 @@ public String getEmailAddr() {
}
}

@JsonIgnore
public String getOrganisation() {
User userDetails = getPrincipal();
if (userDetails == null) {
Expand All @@ -193,6 +190,7 @@ public String getOrganisation() {
}
}

@JsonIgnore
public int getUserIdAsInt() {
String id = getUserId();
return id == null ? -1 : Integer.parseInt(getUserId());
Expand All @@ -212,6 +210,7 @@ private Authentication auth() {
}
}

@JsonIgnore
public User getPrincipal() {
Authentication auth = auth();
if (auth != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private void execute(HttpServletRequest req, HttpServletResponse res) throws IOE
session = new UserSession();

httpSession.setAttribute(USER_SESSION_ATTRIBUTE_KEY, session);
session.setsHttpSession(httpSession);
// session.setsHttpSession(httpSession);

if (Log.isDebugEnabled(Log.REQUEST)) {
Log.debug(Log.REQUEST, "Session created for client : " + ip);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import jeeves.server.UserSession;
Expand All @@ -40,6 +41,7 @@
import org.fao.geonet.utils.Log;
import org.jdom.Element;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

Expand All @@ -50,7 +52,7 @@
/**
* Manage objects selection for a user session.
*/
public class SelectionManager {
public class SelectionManager implements Serializable {

public static final String SELECTION_METADATA = "metadata";
public static final String SELECTION_BUCKET = "bucket";
Expand All @@ -72,6 +74,7 @@ private SelectionManager() {
}


@JsonIgnore
public Map<String, Integer> getSelectionsAndSize() {
return selections.entrySet().stream().collect(Collectors.toMap(
e -> e.getKey(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public MockHttpSession loginAs(User user) {
UserSession userSession = new UserSession();
userSession.loginAs(user);
session.setAttribute(Jeeves.Elem.SESSION, userSession);
userSession.setsHttpSession(session);
//userSession.setsHttpSession(session);

return session;
}
Expand All @@ -303,8 +303,8 @@ public MockHttpSession loginAsAnonymous() {

UserSession userSession = new UserSession();
session.setAttribute(Jeeves.Elem.SESSION, userSession);
userSession.setsHttpSession(session);

// userSession.setsHttpSession(session);
//
return session;
}

Expand Down
21 changes: 21 additions & 0 deletions domain/src/main/java/org/fao/geonet/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.fasterxml.jackson.annotation.JsonIgnore;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.lang.StringUtils;
import org.fao.geonet.entitylistener.UserEntityListenerManager;
import org.fao.geonet.domain.converter.BooleanToYNConverter;
Expand Down Expand Up @@ -59,17 +60,31 @@ public class User extends GeonetEntity implements UserDetails {
public static final String NODE_APPLICATION_CONTEXT_KEY = "jeevesNodeApplicationContext_";
static final String ID_SEQ_NAME = "user_id_seq";
private static final long serialVersionUID = 2589607276443866650L;

@JsonProperty("id")
private int _id;

@JsonProperty("username")
private String _username;
@JsonProperty("surname")
private String _surname;
@JsonProperty("name")
private String _name;
@JsonProperty("email")
private Set<String> _email = new HashSet<>();
@JsonProperty("addresss")
private Set<Address> _addresses = new LinkedHashSet<>();
@JsonProperty("organisation")
private String _organisation;
@JsonProperty("kind")
private String _kind;
@JsonProperty("profile")
private Profile _profile = Profile.RegisteredUser;
@JsonProperty("security")
private UserSecurity _security = new UserSecurity();
@JsonProperty("lastLoginDate")
private String _lastLoginDate;
@JsonProperty("isEnabled")
private Boolean _isEnabled;

public static String getRandomPassword() {
Expand Down Expand Up @@ -187,6 +202,7 @@ public User setName(@Nullable String name) {
* @return the main email address of the user.
*/
@Transient
@JsonIgnore
public String getEmail() {
if (_email != null) {
for (String email : _email) {
Expand Down Expand Up @@ -249,6 +265,7 @@ protected User setAddresses(Set<Address> addresses) {
* @return the first address in the list of the addresses.
*/
@Transient
@JsonIgnore
public
@Nonnull
Address getPrimaryAddress() {
Expand Down Expand Up @@ -375,6 +392,7 @@ public User setLastLoginDate(@Nullable String lastLoginDate) {
return this;
}

@JsonIgnore
@Transient
@Override
public Collection<GrantedAuthority> getAuthorities() {
Expand All @@ -393,18 +411,21 @@ public Collection<GrantedAuthority> getAuthorities() {
}

@Transient
@JsonIgnore
@Override
public boolean isAccountNonExpired() {
return true;
}

@Transient
@JsonIgnore
@Override
public boolean isAccountNonLocked() {
return true;
}

@Override
@JsonIgnore
@Transient
public boolean isCredentialsNonExpired() {
return true;
Expand Down
2 changes: 2 additions & 0 deletions domain/src/main/java/org/fao/geonet/domain/UserSecurity.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
@Embeddable
public class UserSecurity extends GeonetEntity implements Serializable {
private char[] _password;
@JsonIgnore
private Set<UserSecurityNotification> _securityNotifications = new HashSet<UserSecurityNotification>();
private String _authType;
private String _nodeId;
Expand Down Expand Up @@ -121,6 +122,7 @@ protected UserSecurity setSecurityNotificationsString(final String securityNotif
* @return the mutable set if security notifications.
*/
@Transient
@JsonIgnore
public Set<UserSecurityNotification> getSecurityNotifications() {
return _securityNotifications;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private void createSessionForAllButNotCrawlers(HttpServletRequest request) {
session = new UserSession();

httpSession.setAttribute(Jeeves.Elem.SESSION, session);
session.setsHttpSession(httpSession);
// session.setsHttpSession(httpSession);

if (Log.isDebugEnabled(Log.REQUEST)) {
Log.debug(Log.REQUEST, "Session created for client : " + request.getRemoteAddr());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void dispatch(@PathVariable String portal,
session = new UserSession();

httpSession.setAttribute(USER_SESSION_ATTRIBUTE_KEY, session);
session.setsHttpSession(httpSession);
// session.setsHttpSession(httpSession);

if (Log.isDebugEnabled(Log.REQUEST))
Log.debug(Log.REQUEST, "Session created for client : " + ip);
Expand Down
7 changes: 7 additions & 0 deletions web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@

<!-- FIXME set common dependencies to the root pom.xml -->
<dependencies>

<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-jdbc</artifactId>
<version>2.7.4</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
Expand Down
Loading

0 comments on commit c2d3d94

Please sign in to comment.