Skip to content

Commit 590cb07

Browse files
Doha2012Eugen
authored andcommitted
fix live test (eugenp#2160)
* minor logging fix * spring security sso * use basic auth * use form login * cleanup * cleanup * final cleanup * second client app for sso * spring boot bootstrap * add logic * cleanup * add simple controller * add thymeleaf and security * minor fix * minor fix * add more boot properties * fix live test * fix live test
1 parent 52663a6 commit 590cb07

3 files changed

Lines changed: 17 additions & 14 deletions

File tree

‎spring-security-mvc-boot/src/main/java/org/baeldung/Application.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5+
import org.springframework.boot.web.support.SpringBootServletInitializer;
56
import org.springframework.context.annotation.ComponentScan;
67
import org.springframework.context.annotation.Configuration;
78
import org.springframework.context.annotation.FilterType;
89

910
@Configuration
1011
@EnableAutoConfiguration
11-
@ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.voter.*"), @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.multiplelogin.*"),
12-
@ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.multipleentrypoints.*") })
13-
public class Application {
12+
@ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.voter.*"), @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.multipleauthproviders.*"),
13+
@ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.multiplelogin.*"), @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.multipleentrypoints.*") })
14+
public class Application extends SpringBootServletInitializer {
1415
public static void main(String[] args) {
1516
SpringApplication.run(Application.class, args);
1617
}

‎spring-security-mvc-boot/src/main/java/org/baeldung/persistence/SetupData.java‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.baeldung.persistence.model.Privilege;
1313
import org.baeldung.persistence.model.User;
1414
import org.springframework.beans.factory.annotation.Autowired;
15+
import org.springframework.security.crypto.password.PasswordEncoder;
1516
import org.springframework.stereotype.Component;
1617

1718
@Component
@@ -25,6 +26,9 @@ public class SetupData {
2526
@Autowired
2627
private OrganizationRepository organizationRepository;
2728

29+
@Autowired
30+
private PasswordEncoder encoder;
31+
2832
@PostConstruct
2933
public void init() {
3034
initOrganizations();
@@ -38,14 +42,14 @@ private void initUsers() {
3842
//
3943
final User user1 = new User();
4044
user1.setUsername("john");
41-
user1.setPassword("123");
45+
user1.setPassword(encoder.encode("123"));
4246
user1.setPrivileges(new HashSet<Privilege>(Arrays.asList(privilege1)));
4347
user1.setOrganization(organizationRepository.findByName("FirstOrg"));
4448
userRepository.save(user1);
4549
//
4650
final User user2 = new User();
4751
user2.setUsername("tom");
48-
user2.setPassword("111");
52+
user2.setPassword(encoder.encode("111"));
4953
user2.setPrivileges(new HashSet<Privilege>(Arrays.asList(privilege1, privilege2)));
5054
user2.setOrganization(organizationRepository.findByName("SecondOrg"));
5155
userRepository.save(user2);

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@
22

33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertTrue;
5-
6-
import org.baeldung.persistence.model.Foo;
7-
import org.junit.Test;
8-
import org.springframework.http.MediaType;
9-
105
import io.restassured.RestAssured;
116
import io.restassured.authentication.FormAuthConfig;
127
import io.restassured.response.Response;
138
import io.restassured.specification.RequestSpecification;
149

15-
public class ApplicationLiveTest {
10+
import org.baeldung.persistence.model.Foo;
11+
import org.junit.Test;
12+
import org.springframework.http.MediaType;
1613

17-
private final FormAuthConfig formAuthConfig = new FormAuthConfig("http://localhost:8082/spring-security-mvc-boot/login", "username", "password");
1814

15+
public class ApplicationLiveTest {
16+
1917
@Test
2018
public void givenUserWithReadPrivilegeAndHasPermission_whenGetFooById_thenOK() {
2119
final Response response = givenAuth("john", "123").get("http://localhost:8082/spring-security-mvc-boot/foos/1");
@@ -31,7 +29,7 @@ public void givenUserWithNoWritePrivilegeAndHasPermission_whenPostFoo_thenForbid
3129

3230
@Test
3331
public void givenUserWithWritePrivilegeAndHasPermission_whenPostFoo_thenOk() {
34-
final Response response = givenAuth("tom", "111").contentType(MediaType.APPLICATION_JSON_VALUE).body(new Foo("sample")).post("http://localhost:8082/spring-security-mvc-boot/foos");
32+
final Response response = givenAuth("tom", "111").and().body(new Foo("sample")).and().contentType(MediaType.APPLICATION_JSON_VALUE).post("http://localhost:8082/spring-security-mvc-boot/foos");
3533
assertEquals(201, response.getStatusCode());
3634
assertTrue(response.asString().contains("id"));
3735
}
@@ -62,6 +60,6 @@ public void givenDisabledSecurityExpression_whenGetFooByName_thenError() {
6260

6361
//
6462
private RequestSpecification givenAuth(String username, String password) {
65-
return RestAssured.given().auth().form(username, password, formAuthConfig);
63+
return RestAssured.given().log().uri().auth().form(username, password, new FormAuthConfig("/spring-security-mvc-boot/login","username","password"));
6664
}
6765
}

0 commit comments

Comments
 (0)