Polishing
1. fix deprecations 2. fix generics 3. use lambda instead of anonymous class 4. mark some fields as final
This commit is contained in:
committed by
Marcus Hert Da Coregio
parent
831d65a97c
commit
11ce4b4561
@@ -1,9 +1,7 @@
|
||||
package org.springframework.gradle.maven;
|
||||
|
||||
import org.gradle.api.Action;
|
||||
import org.gradle.api.Plugin;
|
||||
import org.gradle.api.Project;
|
||||
import org.gradle.api.artifacts.repositories.MavenArtifactRepository;
|
||||
import org.gradle.api.publish.PublishingExtension;
|
||||
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin;
|
||||
|
||||
@@ -12,18 +10,12 @@ import java.io.File;
|
||||
public class PublishLocalPlugin implements Plugin<Project> {
|
||||
@Override
|
||||
public void apply(Project project) {
|
||||
project.getPlugins().withType(MavenPublishPlugin.class).all(new Action<MavenPublishPlugin>() {
|
||||
@Override
|
||||
public void execute(MavenPublishPlugin mavenPublish) {
|
||||
PublishingExtension publishing = project.getExtensions().getByType(PublishingExtension.class);
|
||||
publishing.getRepositories().maven(new Action<MavenArtifactRepository>() {
|
||||
@Override
|
||||
public void execute(MavenArtifactRepository maven) {
|
||||
maven.setName("local");
|
||||
maven.setUrl(new File(project.getRootProject().getBuildDir(), "publications/repos"));
|
||||
}
|
||||
});
|
||||
}
|
||||
project.getPlugins().withType(MavenPublishPlugin.class).all(mavenPublish -> {
|
||||
PublishingExtension publishing = project.getExtensions().getByType(PublishingExtension.class);
|
||||
publishing.getRepositories().maven(maven -> {
|
||||
maven.setName("local");
|
||||
maven.setUrl(new File(project.getRootProject().getBuildDir(), "publications/repos"));
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -30,7 +31,7 @@ public class JavadocApiPluginITest {
|
||||
File allClasses = new File(testKit.getRootDir(), "build/api/allclasses-noframe.html");
|
||||
File index = new File(testKit.getRootDir(), "build/api/allclasses-index.html");
|
||||
File listing = allClasses.exists() ? allClasses : index;
|
||||
String listingText = FileUtils.readFileToString(listing);
|
||||
String listingText = FileUtils.readFileToString(listing, Charset.defaultCharset());
|
||||
assertThat(listingText).contains("sample/Api.html");
|
||||
assertThat(listingText).contains("sample/Impl.html");
|
||||
assertThat(listingText).doesNotContain("sample/Sample.html");
|
||||
|
||||
@@ -9,6 +9,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -56,6 +57,6 @@ public class SpringMavenPluginITest {
|
||||
}
|
||||
|
||||
public String getSigningKey() throws Exception {
|
||||
return IOUtils.toString(getClass().getResource("/test-private.pgp"));
|
||||
return IOUtils.toString(getClass().getResource("/test-private.pgp"), Charset.defaultCharset());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.mockito.quality.Strictness;
|
||||
|
||||
import org.springframework.security.core.AuthenticatedPrincipal;
|
||||
import org.springframework.security.core.Authentication;
|
||||
@@ -154,7 +155,7 @@ class SpringSessionBackedSessionRegistryTest {
|
||||
private Session createSession(String sessionId, String userName, Instant lastAccessed) {
|
||||
MapSession session = new MapSession(sessionId);
|
||||
session.setLastAccessedTime(lastAccessed);
|
||||
Authentication authentication = mock(Authentication.class, withSettings().lenient());
|
||||
Authentication authentication = mock(Authentication.class, withSettings().strictness(Strictness.LENIENT));
|
||||
given(authentication.getName()).willReturn(userName);
|
||||
SecurityContextImpl securityContext = new SecurityContextImpl();
|
||||
securityContext.setAuthentication(authentication);
|
||||
|
||||
@@ -39,7 +39,7 @@ class OnCommittedResponseWrapperTests {
|
||||
|
||||
private static final String NL = "\r\n";
|
||||
|
||||
@Mock(lenient = true)
|
||||
@Mock(strictness = Mock.Strictness.LENIENT)
|
||||
HttpServletResponse delegate;
|
||||
|
||||
@Mock
|
||||
|
||||
@@ -47,7 +47,7 @@ import static org.mockito.Mockito.verify;
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class SpringSessionWebSessionStoreTests<S extends Session> {
|
||||
|
||||
@Mock(lenient = true)
|
||||
@Mock(strictness = Mock.Strictness.LENIENT)
|
||||
private ReactiveSessionRepository<S> sessionRepository;
|
||||
|
||||
@Mock
|
||||
|
||||
@@ -48,13 +48,13 @@ import static org.mockito.Mockito.verify;
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class WebSocketRegistryListenerTests {
|
||||
|
||||
@Mock(lenient = true)
|
||||
@Mock(strictness = Mock.Strictness.LENIENT)
|
||||
private WebSocketSession wsSession;
|
||||
|
||||
@Mock(lenient = true)
|
||||
@Mock(strictness = Mock.Strictness.LENIENT)
|
||||
private WebSocketSession wsSession2;
|
||||
|
||||
@Mock(lenient = true)
|
||||
@Mock(strictness = Mock.Strictness.LENIENT)
|
||||
private Message<byte[]> message;
|
||||
|
||||
@Mock
|
||||
|
||||
@@ -55,7 +55,7 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class SessionRepositoryMessageInterceptorTests {
|
||||
|
||||
@Mock(lenient = true)
|
||||
@Mock(strictness = Mock.Strictness.LENIENT)
|
||||
SessionRepository<Session> sessionRepository;
|
||||
|
||||
@Mock
|
||||
|
||||
@@ -36,6 +36,7 @@ import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.config.Customizer;
|
||||
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
|
||||
import org.springframework.security.config.web.server.ServerHttpSecurity;
|
||||
import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;
|
||||
@@ -72,57 +73,62 @@ public class MongoDbDeleteJacksonSessionVerificationTest {
|
||||
void logoutShouldDeleteOldSessionFromMongoDB() {
|
||||
|
||||
// 1. Login and capture the SESSION cookie value.
|
||||
|
||||
// @formatter:off
|
||||
FluxExchangeResult<String> loginResult = this.client.post().uri("/login")
|
||||
.contentType(MediaType.APPLICATION_FORM_URLENCODED) //
|
||||
.body(BodyInserters //
|
||||
.fromFormData("username", "admin") //
|
||||
.with("password", "password")) //
|
||||
.exchange() //
|
||||
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
||||
.body(BodyInserters
|
||||
.fromFormData("username", "admin")
|
||||
.with("password", "password"))
|
||||
.exchange()
|
||||
.returnResult(String.class);
|
||||
// @formatter:on
|
||||
|
||||
AssertionsForClassTypes.assertThat(loginResult.getResponseHeaders().getLocation()).isEqualTo(URI.create("/"));
|
||||
|
||||
String originalSessionId = loginResult.getResponseCookies().getFirst("SESSION").getValue();
|
||||
|
||||
// 2. Fetch a protected resource using the SESSION cookie.
|
||||
|
||||
this.client.get().uri("/hello") //
|
||||
.cookie("SESSION", originalSessionId) //
|
||||
.exchange() //
|
||||
.expectStatus().isOk() //
|
||||
.returnResult(String.class).getResponseBody() //
|
||||
.as(StepVerifier::create) //
|
||||
.expectNext("HelloWorld") //
|
||||
// @formatter:off
|
||||
this.client.get().uri("/hello")
|
||||
.cookie("SESSION", originalSessionId)
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.returnResult(String.class).getResponseBody()
|
||||
.as(StepVerifier::create)
|
||||
.expectNext("HelloWorld")
|
||||
.verifyComplete();
|
||||
// @formatter:on
|
||||
|
||||
// 3. Logout using the SESSION cookie, and capture the new SESSION cookie.
|
||||
|
||||
String newSessionId = this.client.post().uri("/logout") //
|
||||
.cookie("SESSION", originalSessionId) //
|
||||
.exchange() //
|
||||
.expectStatus().isFound() //
|
||||
// @formatter:off
|
||||
String newSessionId = this.client.post().uri("/logout")
|
||||
.cookie("SESSION", originalSessionId)
|
||||
.exchange()
|
||||
.expectStatus().isFound()
|
||||
.returnResult(String.class).getResponseCookies().getFirst("SESSION").getValue();
|
||||
// @formatter:on
|
||||
|
||||
AssertionsForClassTypes.assertThat(newSessionId).isNotEqualTo(originalSessionId);
|
||||
|
||||
// 4. Verify the new SESSION cookie is not yet authorized.
|
||||
|
||||
this.client.get().uri("/hello") //
|
||||
.cookie("SESSION", newSessionId) //
|
||||
.exchange() //
|
||||
.expectStatus().isFound() //
|
||||
// @formatter:off
|
||||
this.client.get().uri("/hello")
|
||||
.cookie("SESSION", newSessionId)
|
||||
.exchange()
|
||||
.expectStatus().isFound()
|
||||
.expectHeader()
|
||||
.value(HttpHeaders.LOCATION, (value) -> AssertionsForClassTypes.assertThat(value).isEqualTo("/login"));
|
||||
// @formatter:on
|
||||
|
||||
// 5. Verify the original SESSION cookie no longer works.
|
||||
|
||||
this.client.get().uri("/hello") //
|
||||
.cookie("SESSION", originalSessionId) //
|
||||
.exchange() //
|
||||
.expectStatus().isFound() //
|
||||
// @formatter:off
|
||||
this.client.get().uri("/hello")
|
||||
.cookie("SESSION", originalSessionId)
|
||||
.exchange()
|
||||
.expectStatus().isFound()
|
||||
.expectHeader()
|
||||
.value(HttpHeaders.LOCATION, (value) -> AssertionsForClassTypes.assertThat(value).isEqualTo("/login"));
|
||||
// @formatter:on
|
||||
}
|
||||
|
||||
@RestController
|
||||
@@ -141,24 +147,24 @@ public class MongoDbDeleteJacksonSessionVerificationTest {
|
||||
|
||||
@Bean
|
||||
SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
|
||||
return http //
|
||||
.logout()//
|
||||
/**/.and() //
|
||||
.formLogin() //
|
||||
/**/.and() //
|
||||
.csrf().disable() //
|
||||
.authorizeExchange() //
|
||||
.anyExchange().authenticated() //
|
||||
/**/.and() //
|
||||
// @formatter:off
|
||||
return http
|
||||
.logout(Customizer.withDefaults())
|
||||
.formLogin(Customizer.withDefaults())
|
||||
.csrf((csrf) -> csrf.disable())
|
||||
.authorizeExchange((ae) -> ae.anyExchange().authenticated())
|
||||
.build();
|
||||
// @formatter:on
|
||||
}
|
||||
|
||||
@Bean
|
||||
MapReactiveUserDetailsService userDetailsService() {
|
||||
return new MapReactiveUserDetailsService(User.withUsername("admin") //
|
||||
.password("{noop}password") //
|
||||
.roles("USER,ADMIN") //
|
||||
// @formatter:off
|
||||
return new MapReactiveUserDetailsService(User.withUsername("admin")
|
||||
.password("{noop}password")
|
||||
.roles("USER,ADMIN")
|
||||
.build());
|
||||
// @formatter:on
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -36,6 +36,7 @@ import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.config.Customizer;
|
||||
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
|
||||
import org.springframework.security.config.web.server.ServerHttpSecurity;
|
||||
import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;
|
||||
@@ -141,26 +142,24 @@ public class MongoDbLogoutVerificationTest {
|
||||
|
||||
@Bean
|
||||
SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
|
||||
|
||||
return http //
|
||||
.logout()//
|
||||
/**/.and() //
|
||||
.formLogin() //
|
||||
/**/.and() //
|
||||
.csrf().disable() //
|
||||
.authorizeExchange() //
|
||||
.anyExchange().authenticated() //
|
||||
/**/.and() //
|
||||
// @formatter:off
|
||||
return http
|
||||
.logout(Customizer.withDefaults())
|
||||
.formLogin(Customizer.withDefaults())
|
||||
.csrf((csrf) -> csrf.disable())
|
||||
.authorizeExchange((ae) -> ae.anyExchange().authenticated())
|
||||
.build();
|
||||
// @formatter:on
|
||||
}
|
||||
|
||||
@Bean
|
||||
MapReactiveUserDetailsService userDetailsService() {
|
||||
|
||||
return new MapReactiveUserDetailsService(User.withUsername("admin") //
|
||||
.password("{noop}password") //
|
||||
.roles("USER,ADMIN") //
|
||||
// @formatter:off
|
||||
return new MapReactiveUserDetailsService(User.withUsername("admin")
|
||||
.password("{noop}password")
|
||||
.roles("USER,ADMIN")
|
||||
.build());
|
||||
// @formatter:on
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -48,15 +48,14 @@ class MongoSession implements Session {
|
||||
* NOTE: This was originally stored in unicode format. Delomboking the code caused it
|
||||
* to get converted to another encoding, which isn't supported on all systems, so we
|
||||
* migrated back to unicode. The same character is being represented ensuring binary
|
||||
* compatibility.
|
||||
*
|
||||
* See https://www.compart.com/en/unicode/U+F607
|
||||
* compatibility. See <a href=
|
||||
* "https://www.compart.com/en/unicode/U+F607">https://www.compart.com/en/unicode/U+F607</a>
|
||||
*/
|
||||
private static final char DOT_COVER_CHAR = '\uF607';
|
||||
|
||||
private String id;
|
||||
|
||||
private String originalSessionId;
|
||||
private final String originalSessionId;
|
||||
|
||||
private long createdMillis = System.currentTimeMillis();
|
||||
|
||||
@@ -66,7 +65,7 @@ class MongoSession implements Session {
|
||||
|
||||
private Date expireAt;
|
||||
|
||||
private Map<String, Object> attrs = new HashMap<>();
|
||||
private final Map<String, Object> attrs = new HashMap<>();
|
||||
|
||||
private transient SessionIdGenerator sessionIdGenerator = UuidSessionIdGenerator.getInstance();
|
||||
|
||||
@@ -135,6 +134,7 @@ class MongoSession implements Session {
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T getAttribute(String attributeName) {
|
||||
return (T) this.attrs.get(coverDot(attributeName));
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ class RedisSessionExpirationPolicyTests {
|
||||
// Wed Apr 15 10:27:32 CDT 2015
|
||||
private static final Long ONE_MINUTE_AGO = 1429111652346L;
|
||||
|
||||
@Mock(lenient = true)
|
||||
@Mock(strictness = Mock.Strictness.LENIENT)
|
||||
RedisOperations<String, Object> sessionRedisOperations;
|
||||
|
||||
@Mock
|
||||
|
||||
@@ -59,7 +59,7 @@ class RedisSessionRepositoryTests {
|
||||
|
||||
private static final String TEST_SESSION_KEY = getSessionKey(TEST_SESSION_ID);
|
||||
|
||||
@Mock(lenient = true)
|
||||
@Mock(strictness = Mock.Strictness.LENIENT)
|
||||
private RedisOperations<String, Object> sessionRedisOperations;
|
||||
|
||||
@Mock
|
||||
@@ -311,7 +311,6 @@ class RedisSessionRepositoryTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void findById_SessionExists_ShouldReturnSession() {
|
||||
Instant now = Instant.now().truncatedTo(ChronoUnit.MILLIS);
|
||||
given(this.sessionHashOperations.entries(eq(TEST_SESSION_KEY)))
|
||||
@@ -334,7 +333,6 @@ class RedisSessionRepositoryTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void findById_SessionExistsAndIsExpired_ShouldReturnNull() {
|
||||
given(this.sessionHashOperations.entries(eq(TEST_SESSION_KEY)))
|
||||
.willReturn(mapOf(RedisSessionMapper.CREATION_TIME_KEY, Instant.EPOCH.toEpochMilli(),
|
||||
@@ -410,7 +408,7 @@ class RedisSessionRepositoryTests {
|
||||
.plusSeconds(session.getMaxInactiveInterval().getSeconds());
|
||||
}
|
||||
|
||||
private static Map mapOf(Object... objects) {
|
||||
private static Map<String, Object> mapOf(Object... objects) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
if (objects != null) {
|
||||
for (int i = 0; i < objects.length; i += 2) {
|
||||
|
||||
@@ -37,7 +37,7 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class RedisIndexedHttpSessionConfigurationMockTests {
|
||||
|
||||
@Mock(lenient = true)
|
||||
@Mock(strictness = Mock.Strictness.LENIENT)
|
||||
RedisConnectionFactory factory;
|
||||
|
||||
@Mock
|
||||
|
||||
@@ -61,14 +61,15 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
* @author Vedran Pavic
|
||||
* @author Aleksandar Stojsavljevic
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
class HazelcastIndexedSessionRepositoryTests {
|
||||
|
||||
private static final String SPRING_SECURITY_CONTEXT = "SPRING_SECURITY_CONTEXT";
|
||||
|
||||
private HazelcastInstance hazelcastInstance = mock(HazelcastInstance.class);
|
||||
private final HazelcastInstance hazelcastInstance = mock(HazelcastInstance.class);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private IMap<String, MapSession> sessions = mock(IMap.class);
|
||||
private final IMap<String, MapSession> sessions = mock(IMap.class);
|
||||
|
||||
private HazelcastIndexedSessionRepository repository;
|
||||
|
||||
|
||||
@@ -790,6 +790,7 @@ class JdbcIndexedSessionRepositoryTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void findByIdWhenChangeSessionIdThenUsesSessionIdGenerator() {
|
||||
this.repository.setSessionIdGenerator(() -> "test");
|
||||
Session saved = this.repository.new JdbcSession(new MapSession(), "primaryKey", false);
|
||||
|
||||
@@ -72,13 +72,11 @@ class JdbcHttpSessionConfigurationTests {
|
||||
|
||||
private static final String CLEANUP_CRON_EXPRESSION = "0 0 * * * *";
|
||||
|
||||
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
|
||||
@AfterEach
|
||||
void closeContext() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
this.context.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -407,7 +405,7 @@ class JdbcHttpSessionConfigurationTests {
|
||||
static class CustomMaxInactiveIntervalInSecondsSetterConfiguration extends JdbcHttpSessionConfiguration {
|
||||
|
||||
CustomMaxInactiveIntervalInSecondsSetterConfiguration() {
|
||||
setMaxInactiveIntervalInSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS);
|
||||
setMaxInactiveInterval(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
@@ -29,7 +29,7 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
@Configuration
|
||||
@EnableGlobalMethodSecurity(prePostEnabled = true)
|
||||
@EnableMethodSecurity
|
||||
public class WebSecurityConfig {
|
||||
|
||||
// @formatter:off
|
||||
|
||||
Reference in New Issue
Block a user