如何创建FindByIndexNameSessionRepository的bean

时间:2018-07-15 09:23:51

标签: java spring-boot redis spring-session

我正在尝试创建FindByIndexNameSessionRepository的Bean。我需要使用它来进行所有用户会话,但是即使我已经定义了它,也遇到了bean错误。我正在使用Spring Boot Starter 1.5.7

  

错误:字段sessionRepository需要一个类型为'org.springframework.session.FindByIndexNameSessionRepository'的bean。

     

考虑定义一个类型的bean   您的“ org.springframework.session.FindByIndexNameSessionRepository”   配置。

我正在尝试创建bean并在我的配置中使用它,诸如此类:

import com.x.security.SpringSessionBackedSessionRegistry;
@Bean
SpringSessionBackedSessionRegistry sessionRegistry() {
    return new SpringSessionBackedSessionRegistry<ExpiringSession>(
            this.sessionRepository);
}
@Autowired
private FindByIndexNameSessionRepository<ExpiringSession> sessionRepository;

我的配置在下面

http<...>
   .maximumSessions(2)
   .sessionRegistry(sessionRegistry())
    .maxSessionsPreventsLogin(false)
    .<other settings>           

我的SpringSessionBackedSessionRegistry类如下:

public class SpringSessionBackedSessionRegistry<S extends ExpiringSession>
    implements SessionRegistry {
  private final FindByIndexNameSessionRepository<S> sessionRepository;


public SpringSessionBackedSessionRegistry(
        FindByIndexNameSessionRepository<S> sessionRepository) {
    Assert.notNull(sessionRepository, "sessionRepository cannot be null");
    this.sessionRepository = sessionRepository;
     }
     @Override
public List<Object> getAllPrincipals() {
    throw new UnsupportedOperationException("SpringSessionBackedSessionRegistry does "
            + "not support retrieving all principals, since Spring Session provides "
            + "no way to obtain that information");
}
@Override
public List<SessionInformation> getAllSessions(Object principal,
        boolean includeExpiredSessions) {
    Collection<S> sessions = this.sessionRepository.findByIndexNameAndIndexValue(
            FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME,
            name(principal)).values();
    List<SessionInformation> infos = new ArrayList<>();
    for (S session : sessions) {
        if (includeExpiredSessions || !Boolean.TRUE.equals(session
                .getAttribute(SpringSessionBackedSessionInformation.EXPIRED_ATTR))) {
            infos.add(new SpringSessionBackedSessionInformation<S>(session,
                    this.sessionRepository));
        }
    }
    return infos;
}

@Override
public SessionInformation getSessionInformation(String sessionId) {
    S session = this.sessionRepository.getSession(sessionId);
    if (session != null) {
        return new SpringSessionBackedSessionInformation<S>(session,
                this.sessionRepository);
    }
    return null;
}

/*
 * This is a no-op, as we don't administer sessions ourselves.
 */
 @Override
public void refreshLastRequest(String sessionId) {
}

/*
 * This is a no-op, as we don't administer sessions ourselves.
 */
 @Override
public void registerNewSession(String sessionId, Object principal) {
}

/*
 * This is a no-op, as we don't administer sessions ourselves.
 */
 @Override
public void removeSessionInformation(String sessionId) {
}

/**
 * Derives a String name for the given principal.
 *
 * @param principal as provided by Spring Security
 * @return name of the principal, or its {@code toString()} representation if no name
 * could be derived
 */

protected String name(Object principal) {
    if (principal instanceof UserDetails) {
        return ((UserDetails) principal).getUsername();
    }
    if (principal instanceof Principal) {
        return ((Principal) principal).getName();
    }
    return principal.toString();
}

}

我的pom片段如下

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.7.RELEASE</version>
</parent>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session</artifactId>
    </dependency>   
</dependencies>

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

假设您已经正确配置了Spring Session(使用Spring Boot 1.5.x,可以通过将spring.session.store-type配置属性设置为redis,或者使用@EnableRedisHttpSession来显式配置) ,您应该可以使用FindByIndexNameSessionRepository<? extends ExpiringSession>。例如:

@Autowired
FindByIndexNameSessionRepository<? extends ExpiringSession> sessionRepository;