Skip to content

Commit c533462

Browse files
sandy03934maibin
authored andcommitted
Bael 2262 : Removed the basic authentication from HTTPS Enabled Application (eugenp#5516)
* 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 * BAEL-2262 Removed the Basic Authentication from the HttpsEnabledApplication.
1 parent f83798f commit c533462

File tree

2 files changed

+7
-38
lines changed

2 files changed

+7
-38
lines changed
Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,16 @@
11
package org.baeldung.ssl;
22

3-
import org.springframework.context.annotation.Bean;
4-
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
53
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
64
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
75
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;
106

117
@EnableWebSecurity
128
public class SecurityConfig extends WebSecurityConfigurerAdapter {
139

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-
2310
@Override
2411
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();
12+
http.authorizeRequests()
13+
.antMatchers("/**")
14+
.permitAll();
3515
}
3616
}

spring-security-mvc-boot/src/test/java/org/baeldung/web/HttpsApplicationIntegrationTest.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
import org.springframework.beans.factory.annotation.Value;
1111
import org.springframework.boot.test.context.SpringBootTest;
1212
import org.springframework.core.io.Resource;
13-
import org.springframework.http.*;
13+
import org.springframework.http.HttpStatus;
14+
import org.springframework.http.ResponseEntity;
1415
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
1516
import org.springframework.test.context.ActiveProfiles;
1617
import org.springframework.test.context.junit4.SpringRunner;
1718
import org.springframework.web.client.RestTemplate;
1819

1920
import javax.net.ssl.SSLContext;
20-
import java.util.Base64;
21+
import java.util.Collections;
2122

2223
import static org.junit.Assert.assertEquals;
2324

@@ -36,7 +37,7 @@ public class HttpsApplicationIntegrationTest {
3637

3738
@Test
3839
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+
ResponseEntity<String> response = restTemplate().getForEntity(WELCOME_URL, String.class, Collections.emptyMap());
4041

4142
assertEquals("<h1>Welcome to Secured Site</h1>", response.getBody());
4243
assertEquals(HttpStatus.OK, response.getStatusCode());
@@ -52,16 +53,4 @@ RestTemplate restTemplate() throws Exception {
5253
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
5354
return new RestTemplate(factory);
5455
}
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-
6756
}

0 commit comments

Comments
 (0)