본 튜토리얼에서는 대부분의 사이트에 기본적으로 필요한 기능을 익히는데 목적이 있습니다. 예제에서는 관리자로 로그인/로그아웃하고, 관리자를 등록/수정/삭제 등을 할 수 있는 기본적인 기능을 포함하는 예제를 만들어봅니다. 그리고, 비밀번호의 단방향 암호화(SHA-256)를 해보는 기능도 포함됩니다. DB사용시, Laravel에서 제공하는 Model 메소드(ORM)를 사용하 않고, 직접 쿼리(스토어드 프로시저)를 사용하여 처리할 것입니다.
2. xyz/app/Http/Controllers/Mgmt/Main.php 를 생성합니다.
<?php
namespace App\Http\Controllers\Mgmt;
use App\Http\Controllers\Controller;
class Main extends Controller
{
public function index()
{
return view('mgmt.index');
}
}
3. xyz/routes/web.php 다음을 추가합니다.
...
use App\Http\Controllers\Mgmt\Main as MgmtMain;
...
Route::get('/', [MgmtMain::class, 'index']);
...
$ sudo mysql -u root
mysql> SET GLOBAL log_bin_trust_function_creators = 1;
mysql> exit
$
6. MySQL에서 isMember Function 생성
$ mysql -u xyz -pxyz123 xyz
DELIMITER $$
CREATE FUNCTION isMember (
i_userid VARCHAR(255),
i_password VARCHAR(255)
) RETURNS INT
BEGIN
DECLARE CNT INT;
SELECT COUNT(*) INTO CNT FROM admins
WHERE userid = i_userid AND password = SHA2(i_password, 256);
RETURN CNT;
END $$
DELIMITER ;
정보통신망법, 개인정보보호법 등에 의하면 비밀번호는 단방향암호화(SHA256 등), 개인정보는 양방향암호화(AES256 등)를 하도록 되어 있습니다. 지금까지 작성한 예제는 이런 법에 맞지 않아서 서비스할 수 없겠네요. 여기에서는 비밀번호의 단방향 암호화를 구현해보도록 하겠습니다. SHA256을 적용하겠습니다. PHP(Laravel)에서는 아무런 작업을 하지 않고, DB(MySQL)에서만 작업을 하겠습니다.
1. 일단 DBMS에 접속해봅니다.
$ mysql -u xyz -pxyz123 xyz
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 282
Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 271
Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
DELIMITER $$
CREATE PROCEDURE deleteAdmin (i_sno INT)
BEGIN
DELETE FROM admins WHERE sno = i_sno;
END $$
DELIMITER ;