如何创建 FindByIndexNameSessionRepository 的 bean
How to create the bean of FindByIndexNameSessionRepository
我正在尝试创建 FindByIndexNameSessionRepository 的 Bean。我需要让所有用户会话都使用它,但即使我已经定义了它,我也会收到 bean 错误。我正在使用 Spring Boot Starter 1.5.7
Error: Field sessionRepository required a bean of type 'org.springframework.session.FindByIndexNameSessionRepository' that could not be found.
Consider defining a bean of type
'org.springframework.session.FindByIndexNameSessionRepository' in your
configuration.
我正在尝试创建 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 class 如下:
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>
非常感谢任何帮助。
假设您已正确配置 Spring 会话(使用 Spring 启动 1.5.x
将 spring.session.store-type
配置 属性 设置为 redis
,或者明确地使用 @EnableRedisHttpSession
),你应该能够使用 FindByIndexNameSessionRepository<? extends ExpiringSession>
。例如:
@Autowired
FindByIndexNameSessionRepository<? extends ExpiringSession> sessionRepository;
我正在尝试创建 FindByIndexNameSessionRepository 的 Bean。我需要让所有用户会话都使用它,但即使我已经定义了它,我也会收到 bean 错误。我正在使用 Spring Boot Starter 1.5.7
Error: Field sessionRepository required a bean of type 'org.springframework.session.FindByIndexNameSessionRepository' that could not be found.
Consider defining a bean of type 'org.springframework.session.FindByIndexNameSessionRepository' in your configuration.
我正在尝试创建 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 class 如下:
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>
非常感谢任何帮助。
假设您已正确配置 Spring 会话(使用 Spring 启动 1.5.x
将 spring.session.store-type
配置 属性 设置为 redis
,或者明确地使用 @EnableRedisHttpSession
),你应该能够使用 FindByIndexNameSessionRepository<? extends ExpiringSession>
。例如:
@Autowired
FindByIndexNameSessionRepository<? extends ExpiringSession> sessionRepository;