---
title: Cấu hình WSO2 Identity server 5.11 sử dụng PostgreSQL
tags: WSO2IS
description: Cấu hình WSO2 Identity server 5.11 sử dụng CSDL PostgreSQL
---
<style>
html, body, .ui-content {
background-color: #333;
color: #ddd;
}
.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
color: #ddd;
}
.markdown-body h1,
.markdown-body h2 {
border-bottom-color: #ffffff69;
}
.markdown-body h1 .octicon-link,
.markdown-body h2 .octicon-link,
.markdown-body h3 .octicon-link,
.markdown-body h4 .octicon-link,
.markdown-body h5 .octicon-link,
.markdown-body h6 .octicon-link {
color: #fff;
}
.markdown-body img {
background-color: transparent;
}
.ui-toc-dropdown .nav>.active:focus>a, .ui-toc-dropdown .nav>.active:hover>a, .ui-toc-dropdown .nav>.active>a {
color: white;
border-left: 2px solid white;
}
.expand-toggle:hover,
.expand-toggle:focus,
.back-to-top:hover,
.back-to-top:focus,
.go-to-bottom:hover,
.go-to-bottom:focus {
color: white;
}
.ui-toc-dropdown {
background-color: #333;
}
.ui-toc-label.btn {
background-color: #191919;
color: white;
}
.ui-toc-dropdown .nav>li>a:focus,
.ui-toc-dropdown .nav>li>a:hover {
color: white;
border-left: 1px solid white;
}
.markdown-body blockquote {
color: #bcbcbc;
}
.markdown-body table tr {
background-color: #5f5f5f;
}
.markdown-body table tr:nth-child(2n) {
background-color: #4f4f4f;
}
.markdown-body code,
.markdown-body tt {
color: #eee;
background-color: rgba(230, 230, 230, 0.36);
}
a,
.open-files-container li.selected a {
color: #5EB7E0;
}
</style>
# Cấu hình WSO2 Identity server 5.11 với PostgreSQL
- Hướng dẫn này giúp cấu hình **WSO2 Identity server 5.11** với primary userstore sử dụng csdl **PostgreSql**
## 1. Tạo các cơ sở dữ liệu cần thiết
Chúng ta phải tạo 03 cơ sở dữ liệu cần thiết cho **WSO2 Identity server 5.11** bao gồm: *shared_db*, *identity_db* và *user_db*(chứa thông tin người dùng primary userstore)
- Bước 1: Đăng nhập vào postgresql với tài khoản postgres
```
psql -U postgres
```
- Bước 2: Tạo database
```
create database identity_db;
create database shared_db;
create database user_db;
```
- Bước 3: Run script để tạo table cho các database
- **identity_db**:
```<IS-HOME>/dbscripts/identity/postgresql.sql```
```<IS-HOME>/dbscripts/identity/uma/postgresql.sql```
```<IS-HOME>/dbscripts/consent/postgresql.sql```
- **shared_db**:
```<IS-HOME>/dbscripts/postgresql.sql```
- **user_db**:
```<IS-HOME>/dbscripts/postgresql.sql```
## 2. Thay đổi ngày hết hạn của keystore mặc định (wso2carbon.jks)
Keystore mặc định của wso2 thường có thời hạn 2 năm. Vì chúng ta sẽ dùng internal keystore để mã hoá cũng như giải mã các dữ liệu nội bộ của wso2is nên chúng ta sẽ thay đổi ngày hết hạn của keystore tránh việc một thời gian sau chúng ta phải thay đổi lại thông tin của keystore
Các keystore của wso2 identity server nằm trong thư mục **<IS_HOME>/repository/resources/security** (<IS_HOME>: là đường dẫn thư mục gốc chứa bộ chạy của wso2 identity server)
- Bước 1: Thay đổi thời gian hết hạn của keystore **wso2carbon.jks**
```
keytool -selfcert -v -alias wso2carbon -keystore <IS_HOME>/repository/resources/security/wso2carbon.jks -validity 7300
Enter keystore password: (nhập mật khẩu của keystore là wso2carbon)
```
- Bước 2: Export certificate của keystore vừa đổi thời giạn ở bước 1
```
keytool -exportcert -keystore <IS_HOME>/repository/resources/security/wso2carbon.jks -alias wso2carbon -file <IS_HOME>/repository/resources/security/wso2carbon.cer
```
- Bước 3: Xoá certificate cũ trong truststore client
```
keytool -delete -alias wso2carbon -keystore <IS_HOME>/repository/resources/security/client-truststore.jks
```
- Bước 4: Import certificate của certificate tạo ở bước 2 vào truststore client của wso2 identity server
```
keytool -importcert -alias wso2carbon -file <IS_HOME>/repository/resources/security/wso2carbon.cer -keystore <IS_HOME>/repository/resources/security/client-truststore.jks
```
- Bước 5: Tạo internal keystore
```
cp <IS_HOME>/repository/resources/security/wso2carbon.jks <IS_HOME>/repository/resources/security/internal-keystore.jks
```
## 3. Cấu hình wso2 identity server
Chúng ta sẽ cấu hình các thông tin cần thiết cho wso2 identity server (*<IS_HOME>/repository/conf/deployment.toml*)
- Bước 1: Cấu hình userstore mặc định dùng database của postgresql
```
[user_store]
type = "database_unique_id"
[database.user]
url = "jdbc:postgresql://localhost:5432/user_db"
username = "postgres"
password = "*****"
driver = "org.postgresql.Driver"
[realm_manager]
data_source = "WSO2USER_DB"
```
- Bước 2: Cấu hình database identity của wso2 identity server
```
[database.identity_db]
type = "postgre"
hostname = "localhost"
name = "identity_db"
username = "postgres"
password = "*****"
port = "5432"
[database.identity_db.pool_options]
maxActive = "80"
maxWait = "60000"
minIdle = "5"
testOnBorrow = true
validationQuery="SELECT 1"
validationInterval="30000"
defaultAutoCommit=true
```
- Bước 3: Cấu hình database shared của wso2 identity server
```
[database.shared_db]
type = "postgre"
hostname = "localhost"
name = "shared_db"
username = "postgres"
password = "*****"
port = "5432"
[database.shared_db.pool_options]
maxActive = "80"
maxWait = "60000"
minIdle = "5"
testOnBorrow = true
validationQuery="SELECT 1"
validationInterval="30000"
defaultAutoCommit=true
```
- Bước 4: Cấu hình primary keystore của wso2 identity server
```
[keystore.primary]
file_name = "wso2carbon.jks"
type = "JKS"
password = "wso2carbon"
alias = "wso2carbon"
key_password = "wso2carbon"
```
- Bước 5: Cấu hình trust store của wso2 identity server
```
[truststore]
file_name="client-truststore.jks"
password="wso2carbon"
type="JKS"
```
- Bước 6: Cấu hình internal store của wso2 identity server
```
# The KeyStore which is used for encrypting/decrypting internal data. By default the primary keystore is used as the internal keystore.
[keystore.internal]
file_name = "internal-keystore.jks"
type = "JKS"
password = "wso2carbon"
alias = "wso2carbon"
key_password = "wso2carbon"
```
- Bước 7: Cấu hình CORS
```
[cors]
allow_generic_http_requests = true
allow_any_origin = true
#Config if allow_any_origin is false
#allowed_origins = [
# "http://wso2.is","http://localhost","http://localhost:8080"
#]
allow_subdomains = true
supported_methods = [
"GET",
"POST",
"HEAD",
"OPTIONS",
"PUT",
"PATCH",
"HEAD",
"DELETE",
"PATCH"
]
support_any_header = true
supported_headers = []
exposed_headers = []
supports_credentials = true
max_age = 3600
tag_requests = false
```
- Bước 8: Cấu hình application system (myaccount, console)
```
[system_applications]
read_only_apps = []
```
- Bước 9: Cấu hình TOTP (*optional*)
```
[authentication.authenticator.totp.parameters]
encodingMethod="Base32"
timeStepSize="30"
windowSize="3"
authenticationMandatory=false
enrolUserInAuthenticationFlow=false
usecase="userAttribute"
userAttribute = "http://wso2.org/foursquare/claims/email"
secondaryUserstore="primary, dulieutnmt.vn, niler.com"
#TOTPAuthenticationEndpointURL="authenticationendpoint/totp.do"
TOTPAuthenticationEndpointErrorPage="authenticationendpoint/totp_error.do"
TOTPAuthenticationEndpointEnableTOTPPage="authenticationendpoint/totp_enroll.do"
Issuer="NILER"
UseCommonIssuer=true
```
## 4. Initial database của wso2 identity server
Nếu chúng ta không execute các scripts sql để tạo các bảng, storeprocedure, function thủ công (thực hiện bước 3 ở phần 1) thì chúng ta có một các để wso2 identity server tự khởi tạo cho chúng ta bằng cách khởi động wso2 identity server lần đầu tiên
- Linux hoặc MacOS:
```
<IS_HOME>/bin/wso2server.sh -Dsetup
```
- Windows:
```
<IS_HOME>/bin/wso2server.bat -Dsetup
```
Sau khi wso2 identity server khởi động xong, nhấn tổ hợp phím Ctr+C để stop wso2 identity server và tiến hành khởi động lại wso2 identity server (không thêm tham số ***-Dsetup***)
## 5. Cấu hình admin console xác thực bằng saml (*optional*)
Nếu chúng ta không muốn đăng nhập vào admin console (https://localhost:9443/carbon) bằng phương thức xác thực mặc định của wso2 identity server (giao diện mặc định) thì chúng ta có thể cấu hình cho phép admin console của wos2 identity server xác thực thông qua SSO với SAML
- Bước 1: Khởi động wso2 identity server
+ Linux hoặc MacOS:
```
<IS_HOME>/bin/wso2server.sh
```
+ Windows:
```
<IS_HOME>/bin/wso2server.bat
```
- Bước 2: Đăng nhập vào admin console (ex: https://localhost:9443/carbon với tài khoản mặc định admin/admin)
- Bước 3: Tạo một **Service Providers** với tên là ex: Admin Console IS
- Bước 4: Mở ***Inbound Authentication Configuration > SAML2 Web SSO Configuration*** và click ***Configure***.
- Bước 5: Nhập các thông tin như trong bảng vào mục ***Manual Configuration***
| Field name | Value |
| -------- | -------- |
| Issuer | carbonServer |
| Assertion Consumer URLs | https://auth.cloudsigning.co:443/acs |
| Enable Response Signing | Selected |
- Bước 6: Thêm cấu hình sau vào file deployment.toml (***<IS_HOME>/repository/conf/deployment.toml***)
```
[admin_console.authenticator.saml_sso_authenticator]
enable=true
priority="1"
assertion_consumer_service_url = "https://auth.cloudsigning.co:443/acs"
identity_provider_sso_service_url = "https://auth.cloudsigning.co:443/samlsso"
```
***NOTE:*** Thay domain theo đúng với tên domain đã tạo và phải có ***port*** tương ứng (kể cả là port 443)
- Bước 7: Khởi động lại wso2 identity server và truy cập lại vào url https://localhost:9443/carbon