본문 바로가기
Programming/PHP

[코드이그나이터] 2. codeigniter shield 프레임워크 설치하기

by Berasix 2022. 10. 24.
반응형

Codeigniter용 인증 권한 부여 프레임워크 CodeIgniter Shield

1. codeigniter 기본 설정

.env 파일을 아직 손대지 않은 가정하에 설정을 설명한다.

.env 파일에서 아래 APP, DATABASE, SECURITY 부분 주석을 해제하고,

DATABASE 부분은 본인 상황에 맞게 작성한다.

#--------------------------------------------------------------------
# ENVIRONMENT
#--------------------------------------------------------------------

CI_ENVIRONMENT = development

#--------------------------------------------------------------------
# APP
#--------------------------------------------------------------------

app.baseURL = ''
app_baseURL = ''
app.forceGlobalSecureRequests = false

app.sessionDriver = 'CodeIgniter\Session\Handlers\FileHandler'
app.sessionCookieName = 'ci_session'
app.sessionExpiration = 7200
app.sessionSavePath = null
app.sessionMatchIP = false
app.sessionTimeToUpdate = 300
app.sessionRegenerateDestroy = false

# app.CSPEnabled = false

#--------------------------------------------------------------------
# DATABASE
#--------------------------------------------------------------------

database.default.hostname = localhost
database.default.database = ci4
database.default.username = root
database.default.password = 
database.default.DBDriver = MySQLi
database.default.DBPrefix =
database.default.port = 3306

=========== 중략 ==============

#--------------------------------------------------------------------
# SECURITY
#--------------------------------------------------------------------

security.csrfProtection = 'session'
security.tokenRandomize = false
security.tokenName = 'csrf_token_name'
security.headerName = 'X-CSRF-TOKEN'
security.cookieName = 'csrf_cookie_name'
security.expires = 7200
security.regenerate = true
security.redirect = true
security.samesite = 'Lax'

2. shield 설치

아래와 같이 설치하면, 최신 코드이그나이터에서 다음과 같은 오류가 날 수 있다

> composer require codeigniter4/shield
  [InvalidArgumentException]
  Could not find a version of package codeigniter4/shield matching your minimum-stability (stable). Require it with an explicit version constraint allowing its desired stability.

 

그럴 땐, 아래처럼 설치한다.

> composer require codeigniter4/shield:dev-develop
> php spark shield:setup
> php spark serve

Database에 테이블들이 생긴 것을 확인할 수 있다.

 

3. 확인하기

/register 로 들어가면 다음과 같은 화면을 볼 수 있다.

가입화면

/index.php/login을 통해 로그인해 볼 수 있다. (아직 index.php 제거 전)

로그인화면

/index.php/logout을 통해 로그아웃하면 아래와 같은 화면이 나온다.

로그아웃한 후 화면

4. 간단하게 로그인화면으로 연결하기

설치 후 app\Config\Filters.php 파일의 아래 부분과 같이 넣어주면, 세션이 없으면 로그인 화면으로 연결된다.

    /**
     * List of filter aliases that are always
     * applied before and after every request.
     *
     * @var array
     */
    public $globals = [
        'before' => [
            // 'honeypot',
            // 'csrf',
            // 'invalidchars',
            'session' => ['except' => ['login*', 'register', 'auth/a/*']],            
        ],
        'after' => [
            'toolbar',
            // 'honeypot',
            // 'secureheaders',
        ],
    ];

 

5. 다시 설치한 경우 문제 발생시

- 여차저차해서 다시 설치했는데, 자꾸 index.php 로 가버리고 뭔가 이상한 느낌이라면

/index.php/logout 하여 세션을 정리해 보자.

 

- username에 한글입력이 안된다면?

vendor\codeigniter4\shield\src\Config\AuthSession.php 의 $usernameValidationRules 를 확인하여 수정한다.

    public array $usernameValidationRules = [
        'required',
        'max_length[30]',
        'min_length[3]',
        'regex_match[/\A[a-zA-Z0-9ㄱ-ㅎ가-힣\.]+\z/]',
    ];

6. 참고

https://github.com/codeigniter4/shield/blob/develop/docs/index.md

 

GitHub - codeigniter4/shield: Authentication and Authorization for CodeIgniter 4

Authentication and Authorization for CodeIgniter 4 - GitHub - codeigniter4/shield: Authentication and Authorization for CodeIgniter 4

github.com

 

728x90

댓글