๐Ÿ“ ์ธ์ฆ(Authentication)

์ธ์ฆ์€ ์œ ์ €์˜ identification์„ ํ™•์ธํ•˜๋Š” ์ ˆ์ฐจ์ด๋‹ค. ํ•œ ๋งˆ๋””๋กœ, ์œ ์ €์˜ ์•„์ด๋””์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ํ™•์ธํ•˜๋Š” ์ ˆ์ฐจ๋‹ค.

์ธ์ฆ์„ ํ•˜๊ธฐ ์œ„ํ•ด์„  ๋จผ์ €, ์œ ์ €์˜ ์•„์ด๋””์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์ž‡๋Š” ๊ธฐ๋Šฅ๋„ ํ•„์š”ํ•˜๋‹ค.

์šฐ๋ฆฌ๊ฐ€ ์ƒ๊ฐํ•˜๋Š” ๋กœ๊ทธ์ธ ์ ˆ์ฐจ๋Š” ์ƒ๊ฐํ•ด๋ณด์ž.

1. ํšŒ์›๊ฐ€์ž… : ์•„์ด๋””์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

  • ์ด๋•Œ, ๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” ์•”ํ˜ธํ™” ์ €์žฅํ•œ๋‹ค.

2. ๋กœ๊ทธ์ธ : ์•„์ด๋””์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.

  • ์ž…๋ ฅํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์•”ํ˜ธํ™” ํ•œ ํ›„, DB์— ์ €์žฅ๋œ ์•”ํ˜ธํ™” ๋น„๋ฐ€๋ฒˆํ˜ธ์™€ ๋น„๊ตํ•œ๋‹ค.

3. ์ผ์น˜ํ•˜๋ฉด ๋กœ๊ทธ์ธ ์„ฑ๊ณต, ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

4. ๋กœ๊ทธ์ธ์ด ์„ฑ๊ณต๋˜๋ฉด access_tocken์„ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „์†กํ•œ๋‹ค.

5. ์œ ์ €๋Š” ๋กœ๊ทธ์ธ ์„ฑ๊ณ  ํ›„ ๋‹ค์Œ๋ถ€ํ„ฐ๋Š” access_tocken์„ ์ฒจ๋ถ€ํ•ด์„œ, request๋ฅผ ์„œ๋ฒ„์— ์ „์†กํ•œ๋‹ค.

 

๐Ÿ’ก ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™”

์œ ์ €์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” ์ ˆ๋Œ€ ๊ทธ๋Œ€๋กœ DB์— ์ €์žฅ๋˜์ง€ ์•Š๋Š”๋‹ค. ๋งŒ์ผ DB๊ฐ€ ํ•ดํ‚น ๋‹นํ•  ๊ฒฝ์šฐ, ๊ทธ๋Œ€๋กœ ์œ ์ถœ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
๋”ฐ๋ผ์„œ, ์œ ์ €์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” ๋ฐ˜๋“œ์‹œ ์•”ํ˜ธํ™”ํ•˜์—ฌ ์ €์žฅํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿด ๊ฒฝ์šฐ DB๊ฐ€ ํ•ดํ‚น ๋‹นํ•˜๋”๋ผ๋„ ๊ทธ๋Œ€๋กœ ๋…ธ์ถœ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๐Ÿ’ก ํ—ˆ๊ฐ€(Authorization)

ํ—ˆ๊ฐ€๋Š” ์œ ์ €๊ฐ€ ์š”์ฒญํ•˜๋Š” request๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์ด ์žˆ๋Š” ์œ ์ €์ธ๊ฐ€๋ฅผ ํ™•์ธํ•˜๋Š” ์ ˆ์ฐจ์ด๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด ํ•ด๋‹น ์œ ์ €๋Š” ๊ณ ๊ฐ ์ •๋ณด๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ˆ˜์ •ํ•  ๊ถŒํ•œ์€ ์—†๋Š” ๊ฒฝ์šฐ์ด๋‹ค.

 

๐Ÿ“ Flask ๋กœ๊ทธ์ธ ๊ตฌํ˜„ํ•˜๊ธฐ

# 1๋ฒˆ์„ ํ•ด๋ณด์„ธ์š”!
from flask import Flask, request, render_template, session, url_for, redirect

app = Flask(__name__)
app.secret_key = 'super secret key'
app.config['SESSION_TYPE'] = 'filesystem'
userinfo = {'Elice': '1q2w3e4r!!'}


@app.route("/")
def home():
    if session.get('logged_in'):
        return render_template('loggedin.html')
    else:
        return render_template('index.html')


@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        name = request.form['username']
        password = request.form['password']
        try:
            if (name in userinfo):
                #2๋ฒˆ์„ ํ•ด๋ณด์„ธ์š”!
                session["logged_in"] = True
                    #3๋ฒˆ์„ ํ•ด๋ณด์„ธ์š”!
                return render_template('loggedin.html')
                    #4๋ฒˆ์„ ํ•ด๋ณด์„ธ์š”!
            else:
                return '๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ํ‹€๋ฆฝ๋‹ˆ๋‹ค.'
            return '์•„์ด๋””๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.'
        except:
            return 'Dont login'
    else:
        return render_template('login.html')


@app.route('/register', methods=['GET', 'POST'])
def register():
    if request.method == 'POST':
        #4๋ฒˆ์„ ํ•ด๋ณด์„ธ์š”!
        name = request.form['username'] 
        password = request.form['password']
        userinfo[name] = password
        
        return redirect(url_for('login'))
    else:
        return render_template('register.html')


@app.route("/logout")
def logout():
    session['logged_in'] = False
    return render_template('index.html')
    
if __name__ == '__main__':
    app.run()
<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>HTML for python flask</title>
</head>

<body>
        <p>๋กœ๊ทธ์ธ์ด ํ•„์š”ํ•œ ์„œ๋น„์Šค์ž…๋‹ˆ๋‹ค.</p>
        <a href= "login">๋กœ๊ทธ์ธ์ฐฝ์œผ๋กœ</a><br>
        <a href= "register">ํšŒ์›๊ฐ€์ž…</a>
</body>
</html>


<!-- loggedin.html -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>HTML for python flask</title>
</head>

<body>
        <form action = "" method="post">
        <p>๋กœ๊ทธ์ธ ์„ฑ๊ณต ํŽ˜์ด์ง€</p>
        <a href= "/logout">๋กœ๊ทธ์•„์›ƒ</a>
        </form>
</body>
</html>


<!-- login.html -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>HTML for python flask</title>
</head>

<body>
    <form action = "" method="post">
        <p>name : <input type="text" name = "username"></p>
        <p>password : <input type="password"  name = "password"></p>
        <p>์ด๋ฆ„๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ๋กœ๊ทธ์ธ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด์„ธ์š”.<br><input type = "submit" value = "๋กœ๊ทธ์ธ"/> </p>
    </form>
</body>
</html>


<!-- register.html -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>HTML for python flask</title>
</head>

<body>
    <form action = "" method="post">
        <p>name : <input type="text" name = "username"></p>
        <p>password : <input type="password" name = "password"></p>
        <p>์ด๋ฆ„๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์ƒ์„ฑ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด์„ธ์š”.<br> <input type = "submit" value = "์ƒ์„ฑ"/> </p>
    </form>
</body>
</html>

[ ์ถœ์ฒ˜ : elice ]

 

๐Ÿ“ ๋กœ๊น…

๋กœ๊น…์€ ํ”„๋กœ๊ทธ๋žจ์ด ์ž‘๋™ํ•  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ๋ฅผ ์ถ”์ ํ•˜๋Š” ํ–‰์œ„์ด๋‹ค.
ํ”„๋กœ๊ทธ๋žจ์˜ ๋ฌธ์ œ๋“ค์„ ํŒŒ์•…ํ•˜๊ณ  ์œ ์ง€๋ณด์ˆ˜ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๊ณ , ๋กœ๊น…์„ ํ†ตํ•ด ๋ฐœ์ƒํ•œ ์—๋Ÿฌ๋ฅผ ์ถ”์  ๊ฐ€๋Šฅํ•˜๋‹ค.

์šด์˜ ์ค‘์ธ ์›น ์„œ๋น„์Šค์—์„œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๊ฒฝ์šฐ, ํ•ด๋‹น ๋ฌธ์ œ์˜ ์›์ธ์„ ํŒŒ์•…ํ•˜๋ ค๋ฉด ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ์˜ ์ •๋ณด๊ฐ€ ํ•„์š”ํ•˜๋‹ค.
์ด๋Ÿฐ ์ •๋ณด๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด์„œ ์ค‘์š”ํ•œ ๊ธฐ๋Šฅ์˜ ์‹คํ–‰๋˜๋Š” ๋ถ€๋ถ„์— ์ ์ ˆํ•œ ๋กœ๊ทธ(log)๋ฅผ ๋‚จ๊ฒจ์•ผ ํ•œ๋‹ค.
์ผ๋ฐ˜์ ์ธ ๋กœ๊ทธ ๊ธฐ๋ก์˜ ์ด์ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • ๋กœ๊ทธ๋Š” ์„ฑ๋Šฅ์— ๊ด€ํ•œ ํ†ต๊ณ„์™€ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋กœ๊ทธ๋Š” ์žฌํ˜„ํ•˜๊ธฐ ํž˜๋“  ๋ฒ„๊ทธ์— ๋Œ€ํ•œ ์œ ์šฉํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์„ค์ •์ด ๊ฐ€๋Šฅํ•  ๋•Œ, ๋กœ๊ทธ๋Š” ์˜ˆ๊ธฐ์น˜ ๋ชปํ•œ ํŠน์ • ๋ฌธ์ œ๋“ค์„ ๋””๋ฒ„๊ทธํ•˜๊ธฐ ์œ„ํ•ด ๊ทธ ๋ฌธ์ œ๋“ค์„ ์ฒ˜๋ฆฌํ•˜๋„๋ก  ์ฝ”๋“œ๋ฅผ  ์ˆ˜์ •ํ•˜์—ฌ ๋‹ค์‹œ ์ ์šฉํ•˜์ง€ ์•Š์•„๋„, ์ผ๋ฐ˜์ ์ธ ์ •๋ณด๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ’ก ๋กœ๊น… ๋ ˆ๋ฒจ(Loggin Level)

๋‹ค์Œ ์ˆœ์„œ๋กœ ๋กœ๊น…์ด ๋œ๋‹ค.

DEBUG < INFO < WARNING < ERROR < CRITICAL

  • DEBUG : ์ƒ์„ธํ•œ ์ •๋ณด
  • INFO : ์ผ๋ฐ˜์ ์ธ ์ •๋ณด
  • WARNING : ์˜ˆ์ƒ์น˜ ๋ชปํ•˜๊ฑฐ๋‚˜, ๊ฐ€๊นŒ์šด ๋ฏธ๋ž˜์— ๋ฐœ์ƒํ•  ๋ฌธ์ œ
  • ERROR : ์—๋Ÿฌ ๋กœ๊ทธ, ์‹ฌ๊ฐํ•œ ๋ฌธ์ œ
  • CRITICAL : ํ”„๋กœ๊ทธ๋žจ ์ž์ฒด๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ

ํŒŒ์ด์ฌ ๋กœ๊ฑฐ ๋ ˆ๋ฒจ ์„ค์ •์— ๋”ฐ๋ผ, ํ•˜์œ„ ๋ ˆ๋ฒจ์€ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค.
๊ธฐ๋ณธ ๋กœ๊ฑฐ ๋ ˆ๋ฒจ ์„ธํŒ…์€ WARNING ์ด๋‹ค. ๋•Œ๋ฌธ์—  ์„ค์ • ์—†์ด๋Š” INFO, DEBUG๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜ ์—†๋‹ค.

๐Ÿ’ก python logger

๊ธฐ๋ณธ์ ์œผ๋กœ ๋‹ค์Œ ๋กœ๊น… ์ด๋ ฅ์€ ๋‚จ๊ธฐ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

  • ์„œ๋ฒ„ ์‹œ์ž‘ ๋กœ๊ทธ
  • ์„œ๋ฒ„ ํฌํŠธ ๋ฒˆํ˜ธ
  • ํ•จ์ˆ˜ ํ˜ธ์ถœ
  • ๋ฐ์ดํ„ฐ์˜ ์ž…์ถœ๋ ฅ
#์˜ˆ์‹œ ์ฝ”๋“œ
import logging 

if __name__ : '__main__': 
    logging.info("hello elice!")

์œ„ ์ฝ”๋“œ๋กœ ์‹คํ–‰ํ•˜๋ฉด, "hello elice!" ๊ฐ€ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. ์œ„์—์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด, ๋กœ๊น…์˜ ๊ธฐ๋ณธ ์„ธํŒ…์€ WARNING์ด๊ธฐ ๋•Œ๋ฌธ
๊ทธ๋ ‡๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•˜๋ฉด "hello elice!" ๊ฐ€ ์ถœ๋ ฅ์ด ๋ ๊นŒ ?  ์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด ์„ค์ •ํ•˜๋ฉด ์ถœ๋ ฅ๋œ๋‹ค.

import logging 

if __name__ : '__main__': 
    logger = logging.getLogger() 
    logger.setlevel(logging.DEBUG) 	# ๋กœ๊น… ๊ธฐ๋ณธ์„ธํŒ… WARNING -> DEBUG๋กœ ๋ณ€๊ฒฝ
    logger.info("hello elice!")

 

๐Ÿ“ Flask logger

ํ”Œ๋ผ์Šคํฌ๋Š” 0.3 ๋ฒ„์ „๋ถ€ํ„ฐ logger๋ฅผ ํ”Œ๋ผ์Šคํฌ ๋‚ด๋ถ€์—์„œ ์ œ๊ณตํ•˜๊ธฐ ์‹œ์ž‘ํ–ˆ๋‹ค. ๐Ÿ˜ฎ
(ํ”Œ๋ผ์Šคํฌ์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ๋กœ๊น…์„ ์ œ์™ธํ•˜๊ณ , ์ผ๋ฐ˜ python logging์„ ์‚ฌ์šฉํ•ด๋„ ๋ฌด๋ฐฉํ•˜๋‹ค. )

# ๋ ˆ๋ฒจ์— ๋”ฐ๋ฅธ ํ•จ์ˆ˜๊ฐ€ ์ด๋ฏธ ์žˆ์–ด์„œ, ๋”ฐ๋กœ ๋กœ๊น…๋ ˆ๋ฒจ ์„ค์ •์„ ์•ˆํ•ด๋„ ๋œ๋‹ค.

from flask import Flask 
app = Flask(__name__) 
if __name__ == '__main__': 

    app.logger.info("test") 
    app.logger.debug("debug test") 
    app.logger.error("error test") 
    app.run()

 

๐Ÿ’ก Flask logger ๊ตฌํ˜„

from flask import Flask, render_template

app = Flask(__name__)

# .errorhandler(์—๋Ÿฌ์ฝ”๋“œ) : flask ๋‚ด๋ถ€์— ๊ธฐ๋ณธ์ ์œผ๋กœ ์žˆ๋Š” ์—๋Ÿฌ ํ•ธ๋“ค๋Ÿฌ
# ํŠน์ • ์—๋Ÿฌ์— ๋Œ€ํ•˜์—ฌ errorhandler๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ํ•ด๋‹น ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ๋งค์นญ๋œ๋‹ค.
@app.errorhandler(404)
def page_not_found(error):
    app.logger.error(error)
    return "ํŽ˜์ด์ง€๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."

@app.route("/")
def hello_elice():
    return "Hello world!"

if __name__ == '__main__':
    app.run()

[ ์ถœ์ฒ˜ : elice ]

 

728x90
๋ฐ˜์‘ํ˜•

+ Recent posts