spring-projects / spring-projects/spring-session

Introduce SessionAttributeCompressor

Open
#2,008 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage type: enhancement
Dominant language
Java
Stars
1.9k
Forks
1.2k
Avg merge
4h 27m
Merged PRs (30d)
55

Description

public interface SessionAttributeCompressor<T> {

	boolean accepts(String attributeName);

	T compress(Object attributeValue);

	Object uncompress(T serializedAttributeValue);

}

then we can compress spring security SecurityContext to username

import lombok.RequiredArgsConstructor;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
import org.springframework.util.StringUtils;

@RequiredArgsConstructor
public class SecurityContextSessionAttributeCompressor implements SessionAttributeCompressor<String> {

	private final UserDetailsService userDetailsService;

	@Override
	public boolean accepts(String attributeName) {
		return HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY.equals(attributeName);
	}

	@Override
	public String compress(Object securityContext) {
		SecurityContext sc = (SecurityContext) securityContext;
		if (sc == null || sc.getAuthentication() == null) {
			return null;
		}
		return sc.getAuthentication().getName();
	}

	@Override
	public Object uncompress(String compressedSecurityContext) {
		SecurityContext sc = SecurityContextHolder.getContext();
		if (StringUtils.hasLength(compressedSecurityContext)) {
			UserDetails ud = this.userDetailsService.loadUserByUsername(compressedSecurityContext);
			Authentication auth = new UsernamePasswordAuthenticationToken(ud, ud.getPassword(), ud.getAuthorities());
			sc.setAuthentication(auth);
		}
		return sc;
	}

}

Two advantages:

  1. Minified serialized session
  2. Avoid stale state of UserDetails, currently UserDetails in session is not synchronized with UserDetailsService, session is valid even if user is deleted.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the proposed SessionAttributeCompressor interface and the SecurityContextSessionAttributeCompressor example in the issue. Check how Spring Session currently serializes session attributes and how Spring Security restores UserDetails. Done should include an agreed design for compressed security-context storage and refreshed user details, with tests covering the proposed behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
authentication, backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.