Skip to content

Commit 8c8b2b1

Browse files
committed
Align master with spring-projectsgh-2567-rewrite-docs
1 parent 12f2239 commit 8c8b2b1

File tree

25 files changed

+309
-296
lines changed

25 files changed

+309
-296
lines changed

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/userinfo/DefaultReactiveOAuth2UserServiceTests.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@
1616

1717
package org.springframework.security.oauth2.client.userinfo;
1818

19-
import okhttp3.mockwebserver.MockResponse;
20-
import okhttp3.mockwebserver.MockWebServer;
21-
import org.junit.After;
22-
import org.junit.Before;
23-
import org.junit.Test;
19+
import java.time.Duration;
20+
import java.time.Instant;
21+
2422
import org.springframework.http.HttpHeaders;
2523
import org.springframework.http.HttpMethod;
2624
import org.springframework.http.MediaType;
27-
import org.springframework.security.authentication.AuthenticationServiceException;
2825
import org.springframework.security.oauth2.client.registration.ClientRegistration;
2926
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
3027
import org.springframework.security.oauth2.core.AuthenticationMethod;
@@ -33,13 +30,16 @@
3330
import org.springframework.security.oauth2.core.user.OAuth2User;
3431
import org.springframework.security.oauth2.core.user.OAuth2UserAuthority;
3532

33+
import okhttp3.mockwebserver.MockResponse;
34+
import okhttp3.mockwebserver.MockWebServer;
3635
import okhttp3.mockwebserver.RecordedRequest;
36+
import org.junit.After;
37+
import org.junit.Before;
38+
import org.junit.Test;
3739
import reactor.test.StepVerifier;
3840

39-
import java.time.Duration;
40-
import java.time.Instant;
41-
42-
import static org.assertj.core.api.Assertions.*;
41+
import static org.assertj.core.api.Assertions.assertThat;
42+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4343

4444
/**
4545
* @author Rob Winch
@@ -208,7 +208,7 @@ public void loadUserWhenUserInfoErrorResponseThenThrowOAuth2AuthenticationExcept
208208
public void loadUserWhenUserInfoUriInvalidThenThrowAuthenticationServiceException() throws Exception {
209209
this.clientRegistration.userInfoUri("http://invalid-provider.com/user");
210210
assertThatThrownBy(() -> this.userService.loadUser(oauth2UserRequest()).block())
211-
.isInstanceOf(AuthenticationServiceException.class);
211+
.isInstanceOf(OAuth2AuthenticationException.class);
212212
}
213213

214214
private OAuth2UserRequest oauth2UserRequest() {

samples/boot/helloworld/spring-security-samples-boot-helloworld.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ dependencies {
99

1010
testCompile project(':spring-security-test')
1111
testCompile 'org.springframework.boot:spring-boot-starter-test'
12-
testCompile seleniumDependencies
1312
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2012-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.samples;
17+
18+
import org.junit.Test;
19+
import org.junit.runner.RunWith;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
23+
import org.springframework.boot.test.context.SpringBootTest;
24+
import org.springframework.mock.web.MockHttpSession;
25+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
26+
import org.springframework.test.web.servlet.MockMvc;
27+
import org.springframework.test.web.servlet.MvcResult;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
31+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
32+
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
33+
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.unauthenticated;
34+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
35+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
36+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
37+
38+
/**
39+
*
40+
* @author Joe Grandja
41+
*/
42+
@RunWith(SpringJUnit4ClassRunner.class)
43+
@SpringBootTest
44+
@AutoConfigureMockMvc
45+
public class HelloWorldApplicationTests {
46+
47+
@Autowired
48+
private MockMvc mockMvc;
49+
50+
@Test
51+
public void accessUnprotected() throws Exception {
52+
// @formatter:off
53+
this.mockMvc.perform(get("/index"))
54+
.andExpect(status().isOk());
55+
// @formatter:on
56+
}
57+
58+
@Test
59+
public void accessProtectedRedirectsToLogin() throws Exception {
60+
// @formatter:off
61+
MvcResult mvcResult = this.mockMvc.perform(get("/user/index"))
62+
.andExpect(status().is3xxRedirection())
63+
.andReturn();
64+
// @formatter:on
65+
66+
assertThat(mvcResult.getResponse().getRedirectedUrl()).endsWith("/login");
67+
}
68+
69+
@Test
70+
public void loginUser() throws Exception {
71+
// @formatter:off
72+
this.mockMvc.perform(formLogin().user("user").password("password"))
73+
.andExpect(authenticated());
74+
// @formatter:on
75+
}
76+
77+
@Test
78+
public void loginInvalidUser() throws Exception {
79+
// @formatter:off
80+
this.mockMvc.perform(formLogin().user("invalid").password("invalid"))
81+
.andExpect(unauthenticated())
82+
.andExpect(status().is3xxRedirection());
83+
// @formatter:on
84+
}
85+
86+
@Test
87+
public void loginUserAccessProtected() throws Exception {
88+
// @formatter:off
89+
MvcResult mvcResult = this.mockMvc.perform(formLogin().user("user").password("password"))
90+
.andExpect(authenticated()).andReturn();
91+
// @formatter:on
92+
93+
MockHttpSession httpSession = (MockHttpSession) mvcResult.getRequest().getSession(false);
94+
95+
// @formatter:off
96+
this.mockMvc.perform(get("/user/index").session(httpSession))
97+
.andExpect(status().isOk());
98+
// @formatter:on
99+
}
100+
101+
@Test
102+
public void loginUserValidateLogout() throws Exception {
103+
// @formatter:off
104+
MvcResult mvcResult = this.mockMvc.perform(formLogin().user("user").password("password"))
105+
.andExpect(authenticated()).andReturn();
106+
// @formatter:on
107+
108+
MockHttpSession httpSession = (MockHttpSession) mvcResult.getRequest().getSession(false);
109+
110+
// @formatter:off
111+
this.mockMvc.perform(post("/logout").with(csrf()).session(httpSession))
112+
.andExpect(unauthenticated());
113+
this.mockMvc.perform(get("/user/index").session(httpSession))
114+
.andExpect(unauthenticated())
115+
.andExpect(status().is3xxRedirection());
116+
// @formatter:on
117+
}
118+
}

samples/boot/helloworld/src/main/java/sample/hello/HelloWorldApplication.java renamed to samples/boot/helloworld/src/main/java/org/springframework/security/samples/HelloWorldApplication.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package sample.hello;
16+
package org.springframework.security.samples;
1717

1818
import org.springframework.boot.SpringApplication;
1919
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -27,4 +27,6 @@ public class HelloWorldApplication {
2727
public static void main(String[] args) {
2828
SpringApplication.run(HelloWorldApplication.class, args);
2929
}
30-
}
30+
31+
32+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.samples.config;
17+
18+
import org.springframework.beans.factory.annotation.Autowired;
19+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
20+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
21+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
22+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
23+
import org.springframework.security.core.userdetails.User;
24+
25+
/**
26+
* @author Joe Grandja
27+
*/
28+
@EnableWebSecurity
29+
public class SecurityConfig extends WebSecurityConfigurerAdapter {
30+
31+
// @formatter:off
32+
@Override
33+
protected void configure(HttpSecurity http) throws Exception {
34+
http
35+
.authorizeRequests()
36+
.antMatchers("/css/**", "/index").permitAll()
37+
.antMatchers("/user/**").hasRole("USER")
38+
.and()
39+
.formLogin().loginPage("/login").failureUrl("/login-error");
40+
}
41+
// @formatter:on
42+
43+
// @formatter:off
44+
@Autowired
45+
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
46+
auth
47+
.inMemoryAuthentication()
48+
.withUser(User.withDefaultPasswordEncoder().username("user").password("password").roles("USER"));
49+
}
50+
// @formatter:on
51+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.samples.web;
17+
18+
import org.springframework.stereotype.Controller;
19+
import org.springframework.ui.Model;
20+
import org.springframework.web.bind.annotation.RequestMapping;
21+
22+
/**
23+
* @author Joe Grandja
24+
*/
25+
@Controller
26+
public class MainController {
27+
28+
@RequestMapping("/")
29+
public String root() {
30+
return "redirect:/index";
31+
}
32+
33+
@RequestMapping("/index")
34+
public String index() {
35+
return "index";
36+
}
37+
38+
@RequestMapping("/user/index")
39+
public String userIndex() {
40+
return "user/index";
41+
}
42+
43+
@RequestMapping("/login")
44+
public String login() {
45+
return "login";
46+
}
47+
48+
@RequestMapping("/login-error")
49+
public String loginError(Model model) {
50+
model.addAttribute("loginError", true);
51+
return "login";
52+
}
53+
54+
}
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1+
server:
2+
port: 8080
3+
4+
logging:
5+
level:
6+
root: WARN
7+
org.springframework.web: INFO
8+
org.springframework.security: INFO
9+
110
spring:
2-
security:
3-
user:
4-
password: password
11+
thymeleaf:
12+
cache: false

samples/boot/helloworld/src/main/resources/templates/index.html

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
1-
<!--
2-
~ Copyright 2002-2018 the original author or authors.
3-
~
4-
~ Licensed under the Apache License, Version 2.0 (the "License");
5-
~ you may not use this file except in compliance with the License.
6-
~ You may obtain a copy of the License at
7-
~
8-
~ http://www.apache.org/licenses/LICENSE-2.0
9-
~
10-
~ Unless required by applicable law or agreed to in writing, software
11-
~ distributed under the License is distributed on an "AS IS" BASIS,
12-
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
~ See the License for the specific language governing permissions and
14-
~ limitations under the License.
15-
-->
161
<!DOCTYPE html>
172
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
183
<head>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<title>Login page</title>
5+
<meta charset="utf-8" />
6+
<link rel="stylesheet" href="/css/main.css" th:href="@{/css/main.css}" />
7+
</head>
8+
<body>
9+
<h1>Login page</h1>
10+
<p>Example user: user / password</p>
11+
<p th:if="${loginError}" class="error">Wrong user or password</p>
12+
<form th:action="@{/login}" method="post">
13+
<label for="username">Username</label>:
14+
<input type="text" id="username" name="username" autofocus="autofocus" /> <br />
15+
<label for="password">Password</label>:
16+
<input type="password" id="password" name="password" /> <br />
17+
<input type="submit" value="Log in" />
18+
</form>
19+
<p><a href="/index" th:href="@{/index}">Back to home page</a></p>
20+
</body>
21+
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<title>Hello Spring Security</title>
5+
<meta charset="utf-8" />
6+
<link rel="stylesheet" href="/css/main.css" th:href="@{/css/main.css}" />
7+
</head>
8+
<body>
9+
<div th:substituteby="index::logout"></div>
10+
<h1>This is a secured page!</h1>
11+
<p><a href="/index" th:href="@{/index}">Back to home page</a></p>
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)