본문 바로가기

study/flask

flask

flask document의 tutorial을 보고 그때그때 모르는거 정리

 

flask.palletsprojects.com/en/1.1.x/tutorial/#tutorial

 

Tutorial — Flask Documentation (1.1.x)

Tutorial This tutorial will walk you through creating a basic blog application called Flaskr. Users will be able to register, log in, create posts, and edit or delete their own posts. You will be able to package and install the application on other compute

flask.palletsprojects.com

 

1. session cookie

http 는  stateless 해서 인증할때 좀 중요한데 

인증방식이 크게 두가지임

계정정보를 header에 넣어서 각 요청마다 인증하거나 session과 cookie 둘다사용해야됨.

 

먼저 session/cookie이용 방식을 보자

 

이렇게 진행되는데

@bp.route('/login', methods=('GET', 'POST'))
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        db = get_db()
        error = None
        user = db.execute(
            'SELECT * FROM user WHERE username = ?', (username,)
        ).fetchone()

        if user is None:
            error = 'Incorrect username.'
        elif not check_password_hash(user['password'], password):
            error = 'Incorrect password.'

        if error is None:
            session.clear()  # session 사용
            session['user_id'] = user['id']
            return redirect(url_for('index'))

        flash(error)

    return render_template('auth/login.html')

로그인이 되면 session에 user_id를 넣는거다.

 

근데 그럼 수많은 요청중에서 알맞는 session을 어떻게 찾아낼까?

 

request를 찍어보면 cookie에 session에대한 정보가 담긴걸 볼 수 있다. 이걸로 session 구분하는거임 ㅇㅇ

 

 

2. JWT 

json web token은 사실 1번이랑 별다를거 없음 cookie의 token으로 session을 여나 아님 자체로 복호화하여 검증하냐 차이임. 

 

서버입장에서는 session memory를 줄인다는 장점이있지만 만약 탈취당할 경우 session을 사용할 경우 모든 session다 삭제하면 되는데 jwt인 경우에는 그렇지못하다 따라서 refresh token같은걸로 추가 보안성 가능