From 6aa83feb3bedd5d1418c17895af60fd9a6a7dfcf Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sat, 21 Jul 2018 19:01:22 +0300 Subject: [PATCH 1/2] update to spring 5 --- spring-security-rest-basic-auth/pom.xml | 13 +++++-------- .../filter/CustomWebSecurityConfigurerAdapter.java | 10 +++++++++- .../main/java/org/baeldung/spring/WebConfig.java | 4 ++-- .../src/main/resources/webSecurityConfig.xml | 10 ++++++---- .../src/main/webapp/WEB-INF/api-servlet.xml | 2 +- 5 files changed, 23 insertions(+), 16 deletions(-) diff --git a/spring-security-rest-basic-auth/pom.xml b/spring-security-rest-basic-auth/pom.xml index be351870cd..61a144db0c 100644 --- a/spring-security-rest-basic-auth/pom.xml +++ b/spring-security-rest-basic-auth/pom.xml @@ -10,9 +10,9 @@ com.baeldung - parent-spring-4 + parent-spring-5 0.0.1-SNAPSHOT - ../parent-spring-4 + ../parent-spring-5 @@ -22,12 +22,12 @@ org.springframework.security spring-security-web - ${org.springframework.security.version} + ${spring.version} org.springframework.security spring-security-config - ${org.springframework.security.version} + ${spring.version} @@ -96,7 +96,7 @@ com.fasterxml.jackson.core jackson-databind - ${jackson.version} + ${jackson-databind.version} @@ -270,8 +270,6 @@ - - 4.2.6.RELEASE 4.4.5 @@ -280,7 +278,6 @@ 1.2 3.1.0 - 2.8.5 19.0 diff --git a/spring-security-rest-basic-auth/src/main/java/org/baeldung/filter/CustomWebSecurityConfigurerAdapter.java b/spring-security-rest-basic-auth/src/main/java/org/baeldung/filter/CustomWebSecurityConfigurerAdapter.java index 2ff0e30f94..7ca2a80c52 100644 --- a/spring-security-rest-basic-auth/src/main/java/org/baeldung/filter/CustomWebSecurityConfigurerAdapter.java +++ b/spring-security-rest-basic-auth/src/main/java/org/baeldung/filter/CustomWebSecurityConfigurerAdapter.java @@ -2,11 +2,14 @@ package org.baeldung.filter; import org.baeldung.security.RestAuthenticationEntryPoint; import org.springframework.beans.factory.annotation.Autowired; +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.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; @Configuration @@ -20,7 +23,7 @@ public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAda auth .inMemoryAuthentication() .withUser("user1") - .password("user1Pass") + .password(passwordEncoder().encode("user1Pass")) .authorities("ROLE_USER"); } @@ -38,4 +41,9 @@ public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAda http.addFilterAfter(new CustomFilter(), BasicAuthenticationFilter.class); } + + @Bean + public PasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } } diff --git a/spring-security-rest-basic-auth/src/main/java/org/baeldung/spring/WebConfig.java b/spring-security-rest-basic-auth/src/main/java/org/baeldung/spring/WebConfig.java index 8c3d677af6..2305a7b6c2 100644 --- a/spring-security-rest-basic-auth/src/main/java/org/baeldung/spring/WebConfig.java +++ b/spring-security-rest-basic-auth/src/main/java/org/baeldung/spring/WebConfig.java @@ -7,12 +7,13 @@ import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration @EnableWebMvc @ComponentScan("org.baeldung.web") -public class WebConfig extends WebMvcConfigurerAdapter { +public class WebConfig implements WebMvcConfigurer { public WebConfig() { super(); @@ -22,7 +23,6 @@ public class WebConfig extends WebMvcConfigurerAdapter { @Override public void configureMessageConverters(final List> converters) { - super.configureMessageConverters(converters); converters.add(new MappingJackson2HttpMessageConverter()); } diff --git a/spring-security-rest-basic-auth/src/main/resources/webSecurityConfig.xml b/spring-security-rest-basic-auth/src/main/resources/webSecurityConfig.xml index 6dea965d8b..a93dc841b6 100644 --- a/spring-security-rest-basic-auth/src/main/resources/webSecurityConfig.xml +++ b/spring-security-rest-basic-auth/src/main/resources/webSecurityConfig.xml @@ -2,25 +2,27 @@ - + - + + + \ No newline at end of file diff --git a/spring-security-rest-basic-auth/src/main/webapp/WEB-INF/api-servlet.xml b/spring-security-rest-basic-auth/src/main/webapp/WEB-INF/api-servlet.xml index 4ba9642448..1dbff70b83 100644 --- a/spring-security-rest-basic-auth/src/main/webapp/WEB-INF/api-servlet.xml +++ b/spring-security-rest-basic-auth/src/main/webapp/WEB-INF/api-servlet.xml @@ -1,6 +1,6 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > \ No newline at end of file From 9bda433a5a708354870d1da1f1e61ba21b7f4cb4 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sun, 22 Jul 2018 11:38:26 +0300 Subject: [PATCH 2/2] update test --- .../java/org/baeldung/client/RestClientLiveManualTest.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spring-security-rest-basic-auth/src/test/java/org/baeldung/client/RestClientLiveManualTest.java b/spring-security-rest-basic-auth/src/test/java/org/baeldung/client/RestClientLiveManualTest.java index c27e306c08..104129b663 100644 --- a/spring-security-rest-basic-auth/src/test/java/org/baeldung/client/RestClientLiveManualTest.java +++ b/spring-security-rest-basic-auth/src/test/java/org/baeldung/client/RestClientLiveManualTest.java @@ -8,6 +8,9 @@ import java.io.IOException; import java.security.GeneralSecurityException; import java.security.cert.X509Certificate; +import javax.net.ssl.SSLException; +import javax.net.ssl.SSLPeerUnverifiedException; + import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; @@ -16,6 +19,7 @@ import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.conn.ssl.TrustStrategy; import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.HttpClients; import org.junit.Ignore; import org.junit.Test;