Skip to content

Commit d67ad21

Browse files
sandy03934maibin
authored andcommitted
BAEL-2262 Demo Spring Boot App for HTTPS enabled. (eugenp#5513)
* BAEL-1979 Added examples for SnakeYAML Library * BAEL-1979 Moved the snakeyaml related code to libraries module * BAEL-1979 Removed the System.out.println() statements and converted the assertTrue to assertEquals wherever possible. * BAEL-1979 Removed println statements, small formatting fix in pom.xml * BAEL-1466 Added a new module for apache-geode * BAEL-1466 Updated the Integration Tests. * BAEL-1466 Updated the Integration Tests. * BAEL-1466 Updated the Integration Tests. * BAEL-1466 Removed the Unnecessary code. * BAEL-2262 Added code for demonstration of HTTPS enabled Spring Boot Application
1 parent c013266 commit d67ad21

8 files changed

Lines changed: 158 additions & 1 deletion

File tree

‎spring-security-mvc-boot/pom.xml‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,16 @@
229229
<!--<start-class>org.baeldung.multiplelogin.MultipleLoginApplication</start-class> -->
230230
<!--If you want to run the example with the multiple http elements,
231231
comment the tag above and uncomment the one below -->
232-
<!--<start-class>org.baeldung.multipleentrypoints.MultipleEntryPointsApplication</start-class> -->
232+
<!--<start-class>org.baeldung.multipleentrypoints.MultipleEntryPointsApplication</start-class>-->
233+
<!--If you want to run the example with the Https enabled endpoints,
234+
comment the tag above and uncomment the one below -->
235+
<!-- <start-class>org.baeldung.ssl.HttpsEnabledApplication</start-class> -->
233236

234237
<taglibs-standard.version>1.1.2</taglibs-standard.version>
235238
<jstl.version>1.2</jstl.version>
236239
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
237240
<ehcache-core.version>2.6.11</ehcache-core.version>
241+
<java.version>1.8</java.version>
238242
</properties>
239243

240244
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.baeldung.ssl;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class HttpsEnabledApplication {
8+
9+
public static void main(String... args) {
10+
SpringApplication application = new SpringApplication(HttpsEnabledApplication.class);
11+
application.setAdditionalProfiles("ssl");
12+
application.run(args);
13+
}
14+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.baeldung.ssl;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
5+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
6+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
7+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
8+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
9+
import org.springframework.security.crypto.password.PasswordEncoder;
10+
11+
@EnableWebSecurity
12+
public class SecurityConfig extends WebSecurityConfigurerAdapter {
13+
14+
@Override
15+
public void configure(AuthenticationManagerBuilder auth) throws Exception {
16+
17+
auth.inMemoryAuthentication()
18+
.withUser("memuser")
19+
.password(passwordEncoder().encode("pass"))
20+
.roles("USER");
21+
}
22+
23+
@Override
24+
protected void configure(HttpSecurity http) throws Exception {
25+
http.httpBasic()
26+
.and()
27+
.authorizeRequests()
28+
.antMatchers("/**")
29+
.authenticated();
30+
}
31+
32+
@Bean
33+
public PasswordEncoder passwordEncoder() {
34+
return new BCryptPasswordEncoder();
35+
}
36+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.baeldung.ssl;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.ResponseBody;
6+
7+
@Controller
8+
public class WelcomeController {
9+
10+
@GetMapping("/welcome")
11+
public String welcome() {
12+
return "ssl/welcome";
13+
}
14+
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
http.port=8080
3+
4+
server.port=8443
5+
6+
security.require-ssl=true
7+
8+
# The format used for the keystore
9+
server.ssl.key-store-type=PKCS12
10+
# The path to the keystore containing the certificate
11+
server.ssl.key-store=classpath:keystore/baeldung.p12
12+
# The password used to generate the certificate
13+
server.ssl.key-store-password=password
14+
# The alias mapped to the certificate
15+
server.ssl.key-alias=baeldung
16+
17+
#trust store location
18+
trust.store=classpath:keystore/baeldung.p12
19+
#trust store password
20+
trust.store.password=password
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>Welcome to Secured Site</h1>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.baeldung.web;
2+
3+
import org.apache.http.client.HttpClient;
4+
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
5+
import org.apache.http.impl.client.HttpClients;
6+
import org.apache.http.ssl.SSLContextBuilder;
7+
import org.baeldung.ssl.HttpsEnabledApplication;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.springframework.beans.factory.annotation.Value;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.core.io.Resource;
13+
import org.springframework.http.*;
14+
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
15+
import org.springframework.test.context.ActiveProfiles;
16+
import org.springframework.test.context.junit4.SpringRunner;
17+
import org.springframework.web.client.RestTemplate;
18+
19+
import javax.net.ssl.SSLContext;
20+
import java.util.Base64;
21+
22+
import static org.junit.Assert.assertEquals;
23+
24+
@RunWith(SpringRunner.class)
25+
@SpringBootTest(classes = HttpsEnabledApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
26+
@ActiveProfiles("ssl")
27+
public class HttpsApplicationIntegrationTest {
28+
29+
private static final String WELCOME_URL = "https://localhost:8443/welcome";
30+
31+
@Value("${trust.store}")
32+
private Resource trustStore;
33+
34+
@Value("${trust.store.password}")
35+
private String trustStorePassword;
36+
37+
@Test
38+
public void whenGETanHTTPSResource_thenCorrectResponse() throws Exception {
39+
ResponseEntity<String> response = restTemplate().exchange(WELCOME_URL, HttpMethod.GET, new HttpEntity<String>(withAuthorization("memuser", "pass")), String.class);
40+
41+
assertEquals("<h1>Welcome to Secured Site</h1>", response.getBody());
42+
assertEquals(HttpStatus.OK, response.getStatusCode());
43+
}
44+
45+
RestTemplate restTemplate() throws Exception {
46+
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray())
47+
.build();
48+
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext);
49+
HttpClient httpClient = HttpClients.custom()
50+
.setSSLSocketFactory(socketFactory)
51+
.build();
52+
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
53+
return new RestTemplate(factory);
54+
}
55+
56+
HttpHeaders withAuthorization(String userName, String password) {
57+
return new HttpHeaders() {
58+
{
59+
String auth = userName + ":" + password;
60+
String authHeader = "Basic " + new String(Base64.getEncoder()
61+
.encode(auth.getBytes()));
62+
set("Authorization", authHeader);
63+
}
64+
};
65+
}
66+
67+
}

0 commit comments

Comments
 (0)