21 changed files with 1 additions and 688 deletions
@ -1,48 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2012-present 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. |
|
||||||
*/ |
|
||||||
|
|
||||||
plugins { |
|
||||||
id "java-library" |
|
||||||
id "org.springframework.boot.auto-configuration" |
|
||||||
id "org.springframework.boot.configuration-properties" |
|
||||||
id "org.springframework.boot.deployed" |
|
||||||
id "org.springframework.boot.optional-dependencies" |
|
||||||
} |
|
||||||
|
|
||||||
description = "Spring Boot Session Hazelcast" |
|
||||||
|
|
||||||
dependencies { |
|
||||||
api(project(":core:spring-boot")) |
|
||||||
api(project(":module:spring-boot-session")) |
|
||||||
api("org.springframework.session:spring-session-hazelcast") |
|
||||||
|
|
||||||
implementation(project(":module:spring-boot-hazelcast")) |
|
||||||
implementation(project(":module:spring-boot-web-server")) |
|
||||||
|
|
||||||
optional(project(":core:spring-boot-autoconfigure")) |
|
||||||
|
|
||||||
testImplementation(project(":core:spring-boot-test")) |
|
||||||
testImplementation(project(":test-support:spring-boot-test-support")) |
|
||||||
testImplementation(testFixtures(project(":module:spring-boot-session"))) |
|
||||||
testImplementation("jakarta.servlet:jakarta.servlet-api") |
|
||||||
testImplementation("org.springframework:spring-web") |
|
||||||
|
|
||||||
testRuntimeOnly("ch.qos.logback:logback-classic") |
|
||||||
} |
|
||||||
|
|
||||||
tasks.named("compileTestJava") { |
|
||||||
options.nullability.checking = "tests" |
|
||||||
} |
|
||||||
@ -1,77 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2012-present 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.session.hazelcast.autoconfigure; |
|
||||||
|
|
||||||
import com.hazelcast.core.HazelcastInstance; |
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.AutoConfiguration; |
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; |
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; |
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties; |
|
||||||
import org.springframework.boot.context.properties.PropertyMapper; |
|
||||||
import org.springframework.boot.hazelcast.autoconfigure.HazelcastAutoConfiguration; |
|
||||||
import org.springframework.boot.session.autoconfigure.SessionAutoConfiguration; |
|
||||||
import org.springframework.boot.session.autoconfigure.SessionProperties; |
|
||||||
import org.springframework.boot.web.server.autoconfigure.ServerProperties; |
|
||||||
import org.springframework.context.annotation.Bean; |
|
||||||
import org.springframework.context.annotation.Import; |
|
||||||
import org.springframework.core.Ordered; |
|
||||||
import org.springframework.core.annotation.Order; |
|
||||||
import org.springframework.session.Session; |
|
||||||
import org.springframework.session.SessionRepository; |
|
||||||
import org.springframework.session.config.SessionRepositoryCustomizer; |
|
||||||
import org.springframework.session.hazelcast.HazelcastIndexedSessionRepository; |
|
||||||
import org.springframework.session.hazelcast.config.annotation.web.http.HazelcastHttpSessionConfiguration; |
|
||||||
|
|
||||||
/** |
|
||||||
* {@link EnableAutoConfiguration Auto-configuration} for Spring Session Hazelcast. |
|
||||||
* |
|
||||||
* @author Tommy Ludwig |
|
||||||
* @author Eddú Meléndez |
|
||||||
* @author Stephane Nicoll |
|
||||||
* @author Vedran Pavic |
|
||||||
* @since 4.0.0 |
|
||||||
*/ |
|
||||||
@AutoConfiguration(before = SessionAutoConfiguration.class, after = HazelcastAutoConfiguration.class) |
|
||||||
@ConditionalOnWebApplication(type = Type.SERVLET) |
|
||||||
@ConditionalOnClass({ Session.class, HazelcastIndexedSessionRepository.class }) |
|
||||||
@ConditionalOnMissingBean(SessionRepository.class) |
|
||||||
@ConditionalOnBean(HazelcastInstance.class) |
|
||||||
@EnableConfigurationProperties({ HazelcastSessionProperties.class, ServerProperties.class, SessionProperties.class }) |
|
||||||
@Import(HazelcastHttpSessionConfiguration.class) |
|
||||||
public final class HazelcastSessionAutoConfiguration { |
|
||||||
|
|
||||||
@Bean |
|
||||||
@Order(Ordered.HIGHEST_PRECEDENCE) |
|
||||||
SessionRepositoryCustomizer<HazelcastIndexedSessionRepository> springBootSessionRepositoryCustomizer( |
|
||||||
SessionProperties sessionProperties, HazelcastSessionProperties hazelcastSessionProperties, |
|
||||||
ServerProperties serverProperties) { |
|
||||||
return (sessionRepository) -> { |
|
||||||
PropertyMapper map = PropertyMapper.get(); |
|
||||||
map.from(sessionProperties.determineTimeout(() -> serverProperties.getServlet().getSession().getTimeout())) |
|
||||||
.to(sessionRepository::setDefaultMaxInactiveInterval); |
|
||||||
map.from(hazelcastSessionProperties::getMapName).to(sessionRepository::setSessionMapName); |
|
||||||
map.from(hazelcastSessionProperties::getFlushMode).to(sessionRepository::setFlushMode); |
|
||||||
map.from(hazelcastSessionProperties::getSaveMode).to(sessionRepository::setSaveMode); |
|
||||||
}; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,73 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2012-present 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.session.hazelcast.autoconfigure; |
|
||||||
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties; |
|
||||||
import org.springframework.session.FlushMode; |
|
||||||
import org.springframework.session.SaveMode; |
|
||||||
|
|
||||||
/** |
|
||||||
* Configuration properties for Hazelcast backed Spring Session. |
|
||||||
* |
|
||||||
* @author Vedran Pavic |
|
||||||
* @since 4.0.0 |
|
||||||
*/ |
|
||||||
@ConfigurationProperties("spring.session.hazelcast") |
|
||||||
public class HazelcastSessionProperties { |
|
||||||
|
|
||||||
/** |
|
||||||
* Name of the map used to store sessions. |
|
||||||
*/ |
|
||||||
private String mapName = "spring:session:sessions"; |
|
||||||
|
|
||||||
/** |
|
||||||
* Sessions flush mode. Determines when session changes are written to the session |
|
||||||
* store. |
|
||||||
*/ |
|
||||||
private FlushMode flushMode = FlushMode.ON_SAVE; |
|
||||||
|
|
||||||
/** |
|
||||||
* Sessions save mode. Determines how session changes are tracked and saved to the |
|
||||||
* session store. |
|
||||||
*/ |
|
||||||
private SaveMode saveMode = SaveMode.ON_SET_ATTRIBUTE; |
|
||||||
|
|
||||||
public String getMapName() { |
|
||||||
return this.mapName; |
|
||||||
} |
|
||||||
|
|
||||||
public void setMapName(String mapName) { |
|
||||||
this.mapName = mapName; |
|
||||||
} |
|
||||||
|
|
||||||
public FlushMode getFlushMode() { |
|
||||||
return this.flushMode; |
|
||||||
} |
|
||||||
|
|
||||||
public void setFlushMode(FlushMode flushMode) { |
|
||||||
this.flushMode = flushMode; |
|
||||||
} |
|
||||||
|
|
||||||
public SaveMode getSaveMode() { |
|
||||||
return this.saveMode; |
|
||||||
} |
|
||||||
|
|
||||||
public void setSaveMode(SaveMode saveMode) { |
|
||||||
this.saveMode = saveMode; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,23 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2012-present 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. |
|
||||||
*/ |
|
||||||
|
|
||||||
/** |
|
||||||
* Auto-configuration for Spring Session Hazelcast. |
|
||||||
*/ |
|
||||||
@NullMarked |
|
||||||
package org.springframework.boot.session.hazelcast.autoconfigure; |
|
||||||
|
|
||||||
import org.jspecify.annotations.NullMarked; |
|
||||||
@ -1,4 +0,0 @@ |
|||||||
{ |
|
||||||
"groups": [], |
|
||||||
"properties": [] |
|
||||||
} |
|
||||||
@ -1 +0,0 @@ |
|||||||
org.springframework.boot.session.hazelcast.autoconfigure.HazelcastSessionAutoConfiguration |
|
||||||
@ -1,142 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2012-present 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.session.hazelcast.autoconfigure; |
|
||||||
|
|
||||||
import java.time.Duration; |
|
||||||
|
|
||||||
import com.hazelcast.core.HazelcastInstance; |
|
||||||
import com.hazelcast.map.IMap; |
|
||||||
import org.junit.jupiter.api.BeforeEach; |
|
||||||
import org.junit.jupiter.api.Test; |
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigurations; |
|
||||||
import org.springframework.boot.session.autoconfigure.AbstractSessionAutoConfigurationTests; |
|
||||||
import org.springframework.boot.session.autoconfigure.SessionAutoConfiguration; |
|
||||||
import org.springframework.boot.test.context.runner.WebApplicationContextRunner; |
|
||||||
import org.springframework.boot.web.server.autoconfigure.ServerProperties; |
|
||||||
import org.springframework.context.annotation.Bean; |
|
||||||
import org.springframework.context.annotation.Configuration; |
|
||||||
import org.springframework.session.FlushMode; |
|
||||||
import org.springframework.session.SaveMode; |
|
||||||
import org.springframework.session.config.SessionRepositoryCustomizer; |
|
||||||
import org.springframework.session.hazelcast.HazelcastIndexedSessionRepository; |
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat; |
|
||||||
import static org.mockito.BDDMockito.given; |
|
||||||
import static org.mockito.BDDMockito.then; |
|
||||||
import static org.mockito.Mockito.mock; |
|
||||||
|
|
||||||
/** |
|
||||||
* Tests for {@link HazelcastSessionAutoConfiguration}. |
|
||||||
* |
|
||||||
* @author Vedran Pavic |
|
||||||
*/ |
|
||||||
class HazelcastSessionAutoConfigurationTests extends AbstractSessionAutoConfigurationTests { |
|
||||||
|
|
||||||
@BeforeEach |
|
||||||
void prepareContextRunner() { |
|
||||||
this.contextRunner = new WebApplicationContextRunner() |
|
||||||
.withConfiguration( |
|
||||||
AutoConfigurations.of(SessionAutoConfiguration.class, HazelcastSessionAutoConfiguration.class)) |
|
||||||
.withUserConfiguration(HazelcastConfiguration.class); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
void defaultConfig() { |
|
||||||
this.contextRunner.run((context) -> { |
|
||||||
HazelcastIndexedSessionRepository repository = validateSessionRepository(context, |
|
||||||
HazelcastIndexedSessionRepository.class); |
|
||||||
assertThat(repository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval", |
|
||||||
new ServerProperties().getServlet().getSession().getTimeout()); |
|
||||||
HazelcastInstance hazelcastInstance = context.getBean(HazelcastInstance.class); |
|
||||||
then(hazelcastInstance).should().getMap("spring:session:sessions"); |
|
||||||
}); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
void defaultConfigWithCustomTimeout() { |
|
||||||
this.contextRunner.withPropertyValues("spring.session.timeout=1m").run((context) -> { |
|
||||||
HazelcastIndexedSessionRepository repository = validateSessionRepository(context, |
|
||||||
HazelcastIndexedSessionRepository.class); |
|
||||||
assertThat(repository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval", Duration.ofMinutes(1)); |
|
||||||
}); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
void customMapName() { |
|
||||||
this.contextRunner.withPropertyValues("spring.session.hazelcast.map-name=foo:bar:biz").run((context) -> { |
|
||||||
validateSessionRepository(context, HazelcastIndexedSessionRepository.class); |
|
||||||
HazelcastInstance hazelcastInstance = context.getBean(HazelcastInstance.class); |
|
||||||
then(hazelcastInstance).should().getMap("foo:bar:biz"); |
|
||||||
}); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
void customFlushMode() { |
|
||||||
this.contextRunner.withPropertyValues("spring.session.hazelcast.flush-mode=immediate").run((context) -> { |
|
||||||
HazelcastIndexedSessionRepository repository = validateSessionRepository(context, |
|
||||||
HazelcastIndexedSessionRepository.class); |
|
||||||
assertThat(repository).hasFieldOrPropertyWithValue("flushMode", FlushMode.IMMEDIATE); |
|
||||||
}); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
void customSaveMode() { |
|
||||||
this.contextRunner.withPropertyValues("spring.session.hazelcast.save-mode=on-get-attribute").run((context) -> { |
|
||||||
HazelcastIndexedSessionRepository repository = validateSessionRepository(context, |
|
||||||
HazelcastIndexedSessionRepository.class); |
|
||||||
assertThat(repository).hasFieldOrPropertyWithValue("saveMode", SaveMode.ON_GET_ATTRIBUTE); |
|
||||||
}); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
void whenTheUserDefinesTheirOwnSessionRepositoryCustomizerThenDefaultConfigurationIsOverwritten() { |
|
||||||
this.contextRunner.withUserConfiguration(CustomizerConfiguration.class) |
|
||||||
.withPropertyValues("spring.session.hazelcast.save-mode=on-get-attribute") |
|
||||||
.run((context) -> { |
|
||||||
HazelcastIndexedSessionRepository repository = validateSessionRepository(context, |
|
||||||
HazelcastIndexedSessionRepository.class); |
|
||||||
assertThat(repository).hasFieldOrPropertyWithValue("saveMode", SaveMode.ALWAYS); |
|
||||||
}); |
|
||||||
} |
|
||||||
|
|
||||||
@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; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
@Configuration(proxyBeanMethods = false) |
|
||||||
static class CustomizerConfiguration { |
|
||||||
|
|
||||||
@Bean |
|
||||||
SessionRepositoryCustomizer<HazelcastIndexedSessionRepository> sessionRepositoryCustomizer() { |
|
||||||
return (repository) -> repository.setSaveMode(SaveMode.ALWAYS); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,35 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2012-present 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. |
|
||||||
*/ |
|
||||||
|
|
||||||
plugins { |
|
||||||
id "java" |
|
||||||
} |
|
||||||
|
|
||||||
description = "Spring Boot Session smoke test" |
|
||||||
|
|
||||||
dependencies { |
|
||||||
implementation(project(":starter:spring-boot-starter-actuator")) |
|
||||||
implementation(project(":starter:spring-boot-starter-security")) |
|
||||||
implementation(project(":starter:spring-boot-starter-session-hazelcast")) |
|
||||||
implementation(project(":starter:spring-boot-starter-webmvc")) |
|
||||||
|
|
||||||
testImplementation(project(":starter:spring-boot-starter-webmvc-test")) |
|
||||||
testImplementation(project(":starter:spring-boot-starter-test")) |
|
||||||
} |
|
||||||
|
|
||||||
tasks.named("compileTestJava") { |
|
||||||
options.nullability.checking = "tests" |
|
||||||
} |
|
||||||
@ -1,29 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2012-present 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 smoketest.session.hazelcast; |
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication; |
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication; |
|
||||||
|
|
||||||
@SpringBootApplication |
|
||||||
public class SampleSessionHazelcastApplication { |
|
||||||
|
|
||||||
public static void main(String[] args) { |
|
||||||
SpringApplication.run(SampleSessionHazelcastApplication.class); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,49 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2012-present 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 smoketest.session.hazelcast; |
|
||||||
|
|
||||||
import org.springframework.boot.health.actuate.endpoint.HealthEndpoint; |
|
||||||
import org.springframework.boot.security.autoconfigure.actuate.servlet.EndpointRequest; |
|
||||||
import org.springframework.context.annotation.Bean; |
|
||||||
import org.springframework.context.annotation.Configuration; |
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
|
||||||
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer; |
|
||||||
import org.springframework.security.web.SecurityFilterChain; |
|
||||||
|
|
||||||
import static org.springframework.security.config.Customizer.withDefaults; |
|
||||||
|
|
||||||
/** |
|
||||||
* Security configuration. |
|
||||||
* |
|
||||||
* @author Madhura Bhave |
|
||||||
*/ |
|
||||||
@Configuration(proxyBeanMethods = false) |
|
||||||
class SecurityConfiguration { |
|
||||||
|
|
||||||
@Bean |
|
||||||
SecurityFilterChain managementSecurityFilterChain(HttpSecurity http) { |
|
||||||
http.authorizeHttpRequests((requests) -> { |
|
||||||
requests.requestMatchers(EndpointRequest.to(HealthEndpoint.class)).permitAll(); |
|
||||||
requests.anyRequest().authenticated(); |
|
||||||
}); |
|
||||||
http.formLogin(withDefaults()); |
|
||||||
http.httpBasic(withDefaults()); |
|
||||||
http.csrf(CsrfConfigurer::disable); |
|
||||||
return http.build(); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,20 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2012-present 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. |
|
||||||
*/ |
|
||||||
|
|
||||||
@NullMarked |
|
||||||
package smoketest.session.hazelcast; |
|
||||||
|
|
||||||
import org.jspecify.annotations.NullMarked; |
|
||||||
@ -1,4 +0,0 @@ |
|||||||
spring.security.user.name=user |
|
||||||
spring.security.user.password=password |
|
||||||
|
|
||||||
management.endpoints.web.exposure.include=* |
|
||||||
@ -1,19 +0,0 @@ |
|||||||
<hazelcast xmlns="http://www.hazelcast.com/schema/config" |
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
|
||||||
xsi:schemaLocation="http://www.hazelcast.com/schema/config |
|
||||||
http://www.hazelcast.com/schema/config/hazelcast-config-4.2.xsd"> |
|
||||||
|
|
||||||
<map name="spring:session:sessions"> |
|
||||||
<attributes> |
|
||||||
<attribute extractor-class-name="org.springframework.session.hazelcast.PrincipalNameExtractor">principalName</attribute> |
|
||||||
</attributes> |
|
||||||
</map> |
|
||||||
|
|
||||||
<network> |
|
||||||
<join> |
|
||||||
<auto-detection enabled="false"/> |
|
||||||
<multicast enabled="false"/> |
|
||||||
</join> |
|
||||||
</network> |
|
||||||
|
|
||||||
</hazelcast> |
|
||||||
@ -1,94 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2012-present 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 smoketest.session.hazelcast; |
|
||||||
|
|
||||||
import java.net.URI; |
|
||||||
import java.util.Base64; |
|
||||||
import java.util.Collections; |
|
||||||
import java.util.List; |
|
||||||
import java.util.Map; |
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test; |
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||||
import org.springframework.boot.resttestclient.TestRestTemplate; |
|
||||||
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate; |
|
||||||
import org.springframework.boot.test.context.SpringBootTest; |
|
||||||
import org.springframework.core.ParameterizedTypeReference; |
|
||||||
import org.springframework.http.HttpEntity; |
|
||||||
import org.springframework.http.HttpHeaders; |
|
||||||
import org.springframework.http.HttpMethod; |
|
||||||
import org.springframework.http.HttpStatus; |
|
||||||
import org.springframework.http.MediaType; |
|
||||||
import org.springframework.http.RequestEntity; |
|
||||||
import org.springframework.http.ResponseEntity; |
|
||||||
import org.springframework.util.LinkedMultiValueMap; |
|
||||||
import org.springframework.util.MultiValueMap; |
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat; |
|
||||||
|
|
||||||
/** |
|
||||||
* Tests for {@link SampleSessionHazelcastApplication}. |
|
||||||
* |
|
||||||
* @author Susmitha Kandula |
|
||||||
* @author Madhura Bhave |
|
||||||
*/ |
|
||||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
|
||||||
@AutoConfigureTestRestTemplate |
|
||||||
class SampleSessionHazelcastApplicationTests { |
|
||||||
|
|
||||||
@Autowired |
|
||||||
private TestRestTemplate restTemplate; |
|
||||||
|
|
||||||
@Test |
|
||||||
@SuppressWarnings("unchecked") |
|
||||||
void sessionsEndpointShouldReturnUserSession() { |
|
||||||
performLogin(); |
|
||||||
ResponseEntity<Map<String, Object>> entity = getSessions(); |
|
||||||
assertThat(entity).isNotNull(); |
|
||||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); |
|
||||||
Map<String, Object> body = entity.getBody(); |
|
||||||
assertThat(body).isNotNull(); |
|
||||||
List<Map<String, Object>> sessions = (List<Map<String, Object>>) body.get("sessions"); |
|
||||||
assertThat(sessions).hasSize(1); |
|
||||||
} |
|
||||||
|
|
||||||
private void performLogin() { |
|
||||||
HttpHeaders headers = new HttpHeaders(); |
|
||||||
headers.setAccept(Collections.singletonList(MediaType.TEXT_HTML)); |
|
||||||
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
|
||||||
MultiValueMap<String, String> form = new LinkedMultiValueMap<>(); |
|
||||||
form.set("username", "user"); |
|
||||||
form.set("password", "password"); |
|
||||||
this.restTemplate.exchange("/login", HttpMethod.POST, new HttpEntity<>(form, headers), String.class); |
|
||||||
} |
|
||||||
|
|
||||||
private ResponseEntity<Map<String, Object>> getSessions() { |
|
||||||
HttpHeaders headers = new HttpHeaders(); |
|
||||||
headers.set("Authorization", getBasicAuth()); |
|
||||||
RequestEntity<Object> request = new RequestEntity<>(headers, HttpMethod.GET, |
|
||||||
URI.create("/actuator/sessions?username=user")); |
|
||||||
ParameterizedTypeReference<Map<String, Object>> stringObjectMap = new ParameterizedTypeReference<>() { |
|
||||||
}; |
|
||||||
return this.restTemplate.exchange(request, stringObjectMap); |
|
||||||
} |
|
||||||
|
|
||||||
private String getBasicAuth() { |
|
||||||
return "Basic " + Base64.getEncoder().encodeToString("user:password".getBytes()); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,27 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2012-present 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. |
|
||||||
*/ |
|
||||||
|
|
||||||
plugins { |
|
||||||
id "org.springframework.boot.starter" |
|
||||||
} |
|
||||||
|
|
||||||
description = "Starter for testing Spring Session with Hazelcast" |
|
||||||
|
|
||||||
dependencies { |
|
||||||
api(project(":starter:spring-boot-starter-session-hazelcast")) |
|
||||||
api(project(":starter:spring-boot-starter-test")) |
|
||||||
api(project(":starter:spring-boot-starter-hazelcast-test")) |
|
||||||
} |
|
||||||
@ -1,29 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2012-present 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. |
|
||||||
*/ |
|
||||||
|
|
||||||
plugins { |
|
||||||
id "org.springframework.boot.starter" |
|
||||||
} |
|
||||||
|
|
||||||
description = "Starter for using Spring Session with Hazelcast" |
|
||||||
|
|
||||||
dependencies { |
|
||||||
api(project(":starter:spring-boot-starter")) |
|
||||||
api(project(":starter:spring-boot-starter-hazelcast")) |
|
||||||
|
|
||||||
api(project(":module:spring-boot-session-hazelcast")) |
|
||||||
api(project(":module:spring-boot-hazelcast")) |
|
||||||
} |
|
||||||
Loading…
Reference in new issue