Coder Social home page Coder Social logo

spring-mvc-test's Introduction

๐Ÿ“– Spring Auth ํ•™์Šต ํ…Œ์ŠคํŠธ

  • 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

ํ•™์Šต ํ…Œ์ŠคํŠธ ํ™œ์šฉ ๋ฐฉ๋ฒ•


1. ํ…Œ์ŠคํŠธ ํ™•์ธํ•˜๊ธฐ

  • ํ…Œ์ŠคํŠธ ๋ฉ”์„œ๋“œ์˜ ์ฃผ์„์„ ํ™•์ธํ•˜์—ฌ ํ”„๋กœ๋•์…˜ ์ฝ”๋“œ์— ์ถ”๊ฐ€ํ•  ๋‚ด์šฉ์„ ์ธ์ง€ํ•˜๊ธฐ
@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);
}

2. ๋กœ๊ทธ ํ™•์ธํ•˜๊ธฐ

  • 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

3. ํ”„๋กœ๋•์…˜ ์ฝ”๋“œ์— ๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•˜๊ธฐ

  • ์ฃผ์„์„ ํ™•์ธํ•™์—ฌ ๋™์ž‘ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„
/**
 * 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();
}

4. ํ…Œ์ŠคํŠธ ๋‹ค์‹œ ์ˆ˜ํ–‰

  • ๊ธฐ๋Šฅ์ด ์ •์ƒ์ ์œผ๋กœ ๋™์ž‘ํ•˜๋Š”์ง€ ๊ฒ€์ฆ

์ฐธ๊ณ  ๋ ˆํผ๋Ÿฐ์Šค ๋ชจ์Œ

spring-mvc-test's People

Contributors

boorownie avatar woowabrie avatar haileykim2014 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.