- Spring Auth ๊ธฐ๋ฅ์ ํ์ตํ๊ธฐ ์ํ ํ ์คํธ ํ๋ก์ ํธ
- sessionLogin: session ๊ธฐ๋ฐ์ ๋ก๊ทธ์ธ ํ์ต ํ ์คํธ
- tokenLogin: token ๊ธฐ๋ฐ์ ๋ก๊ทธ์ธ ํ์ต ํ ์คํธ
AuthController
์์ ํ์ฌAuthControllerTest
ํ ์คํธ ์ฑ๊ณต ์ํค๊ธฐ
- ํด๋ก ๋ฐ๊ธฐ
git clone https://github.com/next-step/spring-learning-test.git
- ์ฒดํฌ์์ ๋ธ๋์น
git checkout auth
- ๋ผ๋ ์ฝ๋๋ก ์ ๊ณต๋ ํ๋ก์ ํธ์์ ํ ์คํธ๋ฅผ ์ฑ๊ณต ์ํค๊ธฐ ์ํด ํ ์คํธ ์ฝ๋์ ์ฃผ์์ ์ฐธ๊ณ ํ์ฌ ํ๋ก๋์ ์ฝ๋๋ฅผ ๊ตฌํํ๊ธฐ
- ํ ์คํธ๋ auth ๋๋ ํ ๋ฆฌ ํ์์ ์์น
- ํ์ต ํ ์คํธ ํ์ฉ ๋ฐฉ๋ฒ๋ฅผ ์ฐธ๊ณ ํ์ฌ ํ์ต ๋ฐ ๊ธฐ๋ฅ ๊ตฌํ
- ์์ฑ ๋ธ๋์น auth-sample๋ฅผ ์ฐธ๊ณ ํด์ ์งํํด๋ ์ข์
git checkout auth-sample
- ํ ์คํธ ๋ฉ์๋์ ์ฃผ์์ ํ์ธํ์ฌ ํ๋ก๋์ ์ฝ๋์ ์ถ๊ฐํ ๋ด์ฉ์ ์ธ์งํ๊ธฐ
@Test
void sessionLogin() {
MemberResponse member = RestAssured
.given().log().all()
.auth().form(EMAIL, PASSWORD, new FormAuthConfig("/login/session", USERNAME_FIELD, PASSWORD_FIELD))
.accept(MediaType.APPLICATION_JSON_VALUE)
.when().get("/members/me")
.then().log().all()
.statusCode(HttpStatus.OK.value()).extract().as(MemberResponse.class);
assertThat(member.getEmail()).isEqualTo(EMAIL);
}
- request ์ฐธ๊ณ ํ์ฌ
AuthController
์์ ์์ฒญ์ ์ฒ๋ฆฌ ํ ๋ฉ์๋๋ฅผ ํ์ธ
request
POST /login/session HTTP/1.1
accept: */*
content-type: application/x-www-form-urlencoded; charset=ISO-8859-1
content-length: 37
host: localhost:62888
connection: Keep-Alive
user-agent: Apache-HttpClient/4.5.11 (Java/1.8.0_252)
accept-encoding: gzip,deflate
response
HTTP/1.1 401 Unauthorized
Transfer-Encoding: chunked
Keep-Alive: timeout=60
Connection: keep-alive
Date: Sun, 17 Jan 2021 14:00:41 GMT
Content-Type: application/json
- ์ฃผ์์ ํ์ธํ์ฌ ๋์ํ๋ ์ฝ๋๋ฅผ ๊ตฌํ
/**
* ex) request sample
* <p>
* POST /login/session HTTP/1.1
* content-type: application/x-www-form-urlencoded; charset=ISO-8859-1
* host: localhost:55477
* <p>
* [email protected]&password=1234
*/
@PostMapping("/login/session")
public ResponseEntity sessionLogin() {
// TODO: email๊ณผ password ๊ฐ ์ถ์ถํ๊ธฐ
String email = "";
String password = "";
if (authService.checkInvalidLogin(email, password)) {
throw new AuthorizationException();
}
// TODO: Session์ ์ธ์ฆ ์ ๋ณด ์ ์ฅ (key: SESSION_KEY, value: email๊ฐ)
return ResponseEntity.ok().build();
}
- ๊ธฐ๋ฅ์ด ์ ์์ ์ผ๋ก ๋์ํ๋์ง ๊ฒ์ฆ