parent
6346f21f4e
commit
7cb53b3c45
@ -1,69 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2019 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure.session;
|
|
||||||
|
|
||||||
import org.springframework.boot.WebApplicationType;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
|
|
||||||
import org.springframework.boot.context.properties.bind.BindException;
|
|
||||||
import org.springframework.boot.context.properties.bind.Binder;
|
|
||||||
import org.springframework.context.annotation.ConditionContext;
|
|
||||||
import org.springframework.core.env.Environment;
|
|
||||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
|
||||||
import org.springframework.core.type.AnnotationMetadata;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Base class for Servlet and reactive session conditions.
|
|
||||||
*
|
|
||||||
* @author Tommy Ludwig
|
|
||||||
* @author Stephane Nicoll
|
|
||||||
* @author Madhura Bhave
|
|
||||||
* @author Andy Wilkinson
|
|
||||||
*/
|
|
||||||
abstract class AbstractSessionCondition extends SpringBootCondition {
|
|
||||||
|
|
||||||
private final WebApplicationType webApplicationType;
|
|
||||||
|
|
||||||
protected AbstractSessionCondition(WebApplicationType webApplicationType) {
|
|
||||||
this.webApplicationType = webApplicationType;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
|
||||||
ConditionMessage.Builder message = ConditionMessage.forCondition("Session Condition");
|
|
||||||
Environment environment = context.getEnvironment();
|
|
||||||
StoreType required = SessionStoreMappings.getType(this.webApplicationType,
|
|
||||||
((AnnotationMetadata) metadata).getClassName());
|
|
||||||
if (!environment.containsProperty("spring.session.store-type")) {
|
|
||||||
return ConditionOutcome.match(message.didNotFind("property", "properties")
|
|
||||||
.items(ConditionMessage.Style.QUOTE, "spring.session.store-type"));
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
Binder binder = Binder.get(environment);
|
|
||||||
return binder.bind("spring.session.store-type", StoreType.class)
|
|
||||||
.map((t) -> new ConditionOutcome(t == required,
|
|
||||||
message.found("spring.session.store-type property").items(t)))
|
|
||||||
.orElseGet(() -> ConditionOutcome
|
|
||||||
.noMatch(message.didNotFind("spring.session.store-type property").atAll()));
|
|
||||||
}
|
|
||||||
catch (BindException ex) {
|
|
||||||
return ConditionOutcome.noMatch(message.found("invalid spring.session.store-type property").atAll());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,35 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2019 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure.session;
|
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
||||||
import org.springframework.context.annotation.Conditional;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.session.ReactiveSessionRepository;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* No-op session configuration used to disable Spring Session using the environment.
|
|
||||||
*
|
|
||||||
* @author Tommy Ludwig
|
|
||||||
* @author Andy Wilkinson
|
|
||||||
*/
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
|
||||||
@ConditionalOnMissingBean(ReactiveSessionRepository.class)
|
|
||||||
@Conditional(ReactiveSessionCondition.class)
|
|
||||||
class NoOpReactiveSessionConfiguration {
|
|
||||||
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2019 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure.session;
|
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
||||||
import org.springframework.context.annotation.Conditional;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.session.SessionRepository;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* No-op session configuration used to disable Spring Session using the environment.
|
|
||||||
*
|
|
||||||
* @author Tommy Ludwig
|
|
||||||
*/
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
|
||||||
@ConditionalOnMissingBean(SessionRepository.class)
|
|
||||||
@Conditional(ServletSessionCondition.class)
|
|
||||||
class NoOpSessionConfiguration {
|
|
||||||
|
|
||||||
}
|
|
@ -1,47 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2019 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure.session;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.springframework.session.SessionRepository;
|
|
||||||
import org.springframework.util.ObjectUtils;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Exception thrown when multiple {@link SessionRepository} implementations are available
|
|
||||||
* with no way to know which implementation should be used.
|
|
||||||
*
|
|
||||||
* @author Stephane Nicoll
|
|
||||||
* @since 2.0.0
|
|
||||||
*/
|
|
||||||
public class NonUniqueSessionRepositoryException extends RuntimeException {
|
|
||||||
|
|
||||||
private final List<Class<?>> availableCandidates;
|
|
||||||
|
|
||||||
public NonUniqueSessionRepositoryException(List<Class<?>> availableCandidates) {
|
|
||||||
super("Multiple session repository candidates are available, set the "
|
|
||||||
+ "'spring.session.store-type' property accordingly");
|
|
||||||
this.availableCandidates = (!ObjectUtils.isEmpty(availableCandidates) ? availableCandidates
|
|
||||||
: Collections.emptyList());
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Class<?>> getAvailableCandidates() {
|
|
||||||
return this.availableCandidates;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,45 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2021 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure.session;
|
|
||||||
|
|
||||||
import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
|
|
||||||
import org.springframework.boot.diagnostics.FailureAnalysis;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An {@link AbstractFailureAnalyzer} for {@link NonUniqueSessionRepositoryException}.
|
|
||||||
*
|
|
||||||
* @author Stephane Nicoll
|
|
||||||
*/
|
|
||||||
class NonUniqueSessionRepositoryFailureAnalyzer extends AbstractFailureAnalyzer<NonUniqueSessionRepositoryException> {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected FailureAnalysis analyze(Throwable rootFailure, NonUniqueSessionRepositoryException cause) {
|
|
||||||
StringBuilder message = new StringBuilder();
|
|
||||||
message.append(
|
|
||||||
String.format("Multiple Spring Session store implementations are available on the classpath:%n"));
|
|
||||||
for (Class<?> candidate : cause.getAvailableCandidates()) {
|
|
||||||
message.append(String.format(" - %s%n", candidate.getName()));
|
|
||||||
}
|
|
||||||
StringBuilder action = new StringBuilder();
|
|
||||||
action.append(String.format("Consider any of the following:%n"));
|
|
||||||
action.append(
|
|
||||||
String.format(" - Define the 'spring.session.store-type' property to the store you want to use%n"));
|
|
||||||
action.append(String.format(" - Review your classpath and remove the unwanted store implementation(s)%n"));
|
|
||||||
return new FailureAnalysis(message.toString(), action.toString(), cause);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,32 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2019 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure.session;
|
|
||||||
|
|
||||||
import org.springframework.boot.WebApplicationType;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* General condition used with all reactive session configuration classes.
|
|
||||||
*
|
|
||||||
* @author Andy Wilkinson
|
|
||||||
*/
|
|
||||||
class ReactiveSessionCondition extends AbstractSessionCondition {
|
|
||||||
|
|
||||||
ReactiveSessionCondition() {
|
|
||||||
super(WebApplicationType.REACTIVE);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,35 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2019 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure.session;
|
|
||||||
|
|
||||||
import org.springframework.boot.WebApplicationType;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* General condition used with all servlet session configuration classes.
|
|
||||||
*
|
|
||||||
* @author Tommy Ludwig
|
|
||||||
* @author Stephane Nicoll
|
|
||||||
* @author Madhura Bhave
|
|
||||||
* @author Andy Wilkinson
|
|
||||||
*/
|
|
||||||
class ServletSessionCondition extends AbstractSessionCondition {
|
|
||||||
|
|
||||||
ServletSessionCondition() {
|
|
||||||
super(WebApplicationType.SERVLET);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,40 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2019 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure.session;
|
|
||||||
|
|
||||||
import org.springframework.session.SessionRepository;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Exception thrown when no {@link SessionRepository} is available.
|
|
||||||
*
|
|
||||||
* @author Stephane Nicoll
|
|
||||||
* @since 2.0.0
|
|
||||||
*/
|
|
||||||
public class SessionRepositoryUnavailableException extends RuntimeException {
|
|
||||||
|
|
||||||
private final StoreType storeType;
|
|
||||||
|
|
||||||
public SessionRepositoryUnavailableException(String message, StoreType storeType) {
|
|
||||||
super(message);
|
|
||||||
this.storeType = storeType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public StoreType getStoreType() {
|
|
||||||
return this.storeType;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,94 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2019 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure.session;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.EnumMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.springframework.boot.WebApplicationType;
|
|
||||||
import org.springframework.util.Assert;
|
|
||||||
import org.springframework.util.ObjectUtils;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Mappings between {@link StoreType} and {@code @Configuration}.
|
|
||||||
*
|
|
||||||
* @author Tommy Ludwig
|
|
||||||
* @author Eddú Meléndez
|
|
||||||
*/
|
|
||||||
final class SessionStoreMappings {
|
|
||||||
|
|
||||||
private static final Map<StoreType, Configurations> MAPPINGS;
|
|
||||||
|
|
||||||
static {
|
|
||||||
Map<StoreType, Configurations> mappings = new EnumMap<>(StoreType.class);
|
|
||||||
mappings.put(StoreType.REDIS,
|
|
||||||
new Configurations(RedisSessionConfiguration.class, RedisReactiveSessionConfiguration.class));
|
|
||||||
mappings.put(StoreType.MONGODB,
|
|
||||||
new Configurations(MongoSessionConfiguration.class, MongoReactiveSessionConfiguration.class));
|
|
||||||
mappings.put(StoreType.JDBC, new Configurations(JdbcSessionConfiguration.class, null));
|
|
||||||
mappings.put(StoreType.HAZELCAST, new Configurations(HazelcastSessionConfiguration.class, null));
|
|
||||||
mappings.put(StoreType.NONE,
|
|
||||||
new Configurations(NoOpSessionConfiguration.class, NoOpReactiveSessionConfiguration.class));
|
|
||||||
MAPPINGS = Collections.unmodifiableMap(mappings);
|
|
||||||
}
|
|
||||||
|
|
||||||
private SessionStoreMappings() {
|
|
||||||
}
|
|
||||||
|
|
||||||
static String getConfigurationClass(WebApplicationType webApplicationType, StoreType sessionStoreType) {
|
|
||||||
Configurations configurations = MAPPINGS.get(sessionStoreType);
|
|
||||||
Assert.state(configurations != null, () -> "Unknown session store type " + sessionStoreType);
|
|
||||||
return configurations.getConfiguration(webApplicationType);
|
|
||||||
}
|
|
||||||
|
|
||||||
static StoreType getType(WebApplicationType webApplicationType, String configurationClass) {
|
|
||||||
return MAPPINGS.entrySet().stream()
|
|
||||||
.filter((entry) -> ObjectUtils.nullSafeEquals(configurationClass,
|
|
||||||
entry.getValue().getConfiguration(webApplicationType)))
|
|
||||||
.map(Map.Entry::getKey).findFirst()
|
|
||||||
.orElseThrow(() -> new IllegalStateException("Unknown configuration class " + configurationClass));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class Configurations {
|
|
||||||
|
|
||||||
private final Class<?> servletConfiguration;
|
|
||||||
|
|
||||||
private final Class<?> reactiveConfiguration;
|
|
||||||
|
|
||||||
Configurations(Class<?> servletConfiguration, Class<?> reactiveConfiguration) {
|
|
||||||
this.servletConfiguration = servletConfiguration;
|
|
||||||
this.reactiveConfiguration = reactiveConfiguration;
|
|
||||||
}
|
|
||||||
|
|
||||||
String getConfiguration(WebApplicationType webApplicationType) {
|
|
||||||
switch (webApplicationType) {
|
|
||||||
case SERVLET:
|
|
||||||
return getName(this.servletConfiguration);
|
|
||||||
case REACTIVE:
|
|
||||||
return getName(this.reactiveConfiguration);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
String getName(Class<?> configuration) {
|
|
||||||
return (configuration != null) ? configuration.getName() : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,54 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2019 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure.session;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Supported Spring Session data store types.
|
|
||||||
*
|
|
||||||
* @author Tommy Ludwig
|
|
||||||
* @author Eddú Meléndez
|
|
||||||
* @author Vedran Pavic
|
|
||||||
* @since 1.4.0
|
|
||||||
*/
|
|
||||||
public enum StoreType {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Redis backed sessions.
|
|
||||||
*/
|
|
||||||
REDIS,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* MongoDB backed sessions.
|
|
||||||
*/
|
|
||||||
MONGODB,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* JDBC backed sessions.
|
|
||||||
*/
|
|
||||||
JDBC,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Hazelcast backed sessions.
|
|
||||||
*/
|
|
||||||
HAZELCAST,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* No session data-store.
|
|
||||||
*/
|
|
||||||
NONE
|
|
||||||
|
|
||||||
}
|
|
@ -1,65 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2020 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure.session;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import org.springframework.boot.diagnostics.FailureAnalysis;
|
|
||||||
import org.springframework.boot.diagnostics.FailureAnalyzer;
|
|
||||||
import org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter;
|
|
||||||
import org.springframework.session.SessionRepository;
|
|
||||||
import org.springframework.session.hazelcast.HazelcastIndexedSessionRepository;
|
|
||||||
import org.springframework.session.jdbc.JdbcIndexedSessionRepository;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Tests for {@link NonUniqueSessionRepositoryFailureAnalyzer}.
|
|
||||||
*
|
|
||||||
* @author Stephane Nicoll
|
|
||||||
*/
|
|
||||||
class NonUniqueSessionRepositoryFailureAnalyzerTests {
|
|
||||||
|
|
||||||
private final FailureAnalyzer analyzer = new NonUniqueSessionRepositoryFailureAnalyzer();
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void failureAnalysisWithMultipleCandidates() {
|
|
||||||
FailureAnalysis analysis = analyzeFailure(
|
|
||||||
createFailure(JdbcIndexedSessionRepository.class, HazelcastIndexedSessionRepository.class));
|
|
||||||
assertThat(analysis).isNotNull();
|
|
||||||
assertThat(analysis.getDescription()).contains(JdbcIndexedSessionRepository.class.getName(),
|
|
||||||
HazelcastIndexedSessionRepository.class.getName());
|
|
||||||
assertThat(analysis.getAction()).contains("spring.session.store-type");
|
|
||||||
}
|
|
||||||
|
|
||||||
@SafeVarargs
|
|
||||||
@SuppressWarnings("varargs")
|
|
||||||
private final Exception createFailure(Class<? extends SessionRepository<?>>... candidates) {
|
|
||||||
return new NonUniqueSessionRepositoryException(Arrays.asList(candidates));
|
|
||||||
}
|
|
||||||
|
|
||||||
private FailureAnalysis analyzeFailure(Exception failure) {
|
|
||||||
FailureAnalysis analysis = this.analyzer.analyze(failure);
|
|
||||||
if (analysis != null) {
|
|
||||||
new LoggingFailureAnalysisReporter().report(analysis);
|
|
||||||
}
|
|
||||||
return analysis;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,91 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2022 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure.session;
|
|
||||||
|
|
||||||
import com.hazelcast.core.HazelcastInstance;
|
|
||||||
import com.hazelcast.map.IMap;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
|
||||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
|
||||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
|
|
||||||
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.session.jdbc.JdbcIndexedSessionRepository;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
import static org.mockito.BDDMockito.given;
|
|
||||||
import static org.mockito.Mockito.mock;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Integration tests for {@link SessionAutoConfiguration}.
|
|
||||||
*
|
|
||||||
* @author Stephane Nicoll
|
|
||||||
*/
|
|
||||||
class SessionAutoConfigurationIntegrationTests extends AbstractSessionAutoConfigurationTests {
|
|
||||||
|
|
||||||
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
|
|
||||||
.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class,
|
|
||||||
DataSourceTransactionManagerAutoConfiguration.class, SessionAutoConfiguration.class))
|
|
||||||
.withPropertyValues("spring.datasource.generate-unique-name=true");
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void severalCandidatesWithNoSessionStore() {
|
|
||||||
this.contextRunner.withUserConfiguration(HazelcastConfiguration.class).run((context) -> {
|
|
||||||
assertThat(context).hasFailed();
|
|
||||||
assertThat(context).getFailure().rootCause().isInstanceOf(NonUniqueSessionRepositoryException.class)
|
|
||||||
.hasMessageContaining("Multiple session repository candidates are available")
|
|
||||||
.hasMessageContaining("set the 'spring.session.store-type' property accordingly");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void severalCandidatesWithWrongSessionStore() {
|
|
||||||
this.contextRunner.withUserConfiguration(HazelcastConfiguration.class)
|
|
||||||
.withPropertyValues("spring.session.store-type=redis").run((context) -> {
|
|
||||||
assertThat(context).hasFailed();
|
|
||||||
assertThat(context).getFailure().hasCauseInstanceOf(SessionRepositoryUnavailableException.class);
|
|
||||||
assertThat(context).getFailure()
|
|
||||||
.hasMessageContaining("No session repository could be auto-configured");
|
|
||||||
assertThat(context).getFailure().hasMessageContaining("session store type is 'redis'");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void severalCandidatesWithValidSessionStore() {
|
|
||||||
this.contextRunner.withUserConfiguration(HazelcastConfiguration.class)
|
|
||||||
.withPropertyValues("spring.session.store-type=jdbc")
|
|
||||||
.run((context) -> validateSessionRepository(context, JdbcIndexedSessionRepository.class));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
|
||||||
static class HazelcastConfiguration {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
HazelcastInstance hazelcastInstance() {
|
|
||||||
IMap<Object, Object> map = mock(IMap.class);
|
|
||||||
HazelcastInstance mock = mock(HazelcastInstance.class);
|
|
||||||
given(mock.getMap("spring:session:sessions")).willReturn(map);
|
|
||||||
given(mock.getMap("foo:bar:biz")).willReturn(map);
|
|
||||||
return mock;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue