最近我看好多人的朋友圈都流行發九宮格照片,別人都能擁有,碼農必須有。當然,我們要比其他人更為高調,我們就用Python來對圖片進行處理,這肯定能秀翻你的朋友圈。廢話不說,開乾。 一、圖片導入與信息查看 在對圖像進行處理之前,我們首先需要載入出來一張圖片,我們以載入文件中存在的圖像為例子,載入圖片並查 ...
本文主要介紹 Keycloak 的安裝,使用到的軟體版本:JDK 1.8.0_151、Keycloak 16.1.1、Redhat 6.6。
1、standalone mode--獨立模式安裝
1.1、下載安裝包並解壓
下載地址:https://www.keycloak.org/downloads
解壓:
tar zxvf keycloak-16.1.1.tar.gz
1.2、修改ip
預設綁定的地址為 127.0.0.1,可修改為具體的 ip,編輯 standalone/configuration/standalone.xml:
<interfaces> <interface name="management"> <inet-address value="${jboss.bind.address.management:10.49.196.10}"/> </interface> <interface name="public"> <inet-address value="${jboss.bind.address:10.49.196.10}"/> </interface> </interfaces>
1.3、啟動
nohup bin/standalone.sh &
1.4、訪問控制台
http://10.49.196.10:8080/
1.5、增加用戶
可以通過 http://localhost:8080/auth 頁面 或 bin/add-user-keycloak.sh 腳本增加用戶,如果部署在 linux 伺服器上無法通過 http://localhost:8080/auth 來訪問頁面,可通過腳本來增加用戶:
bin/add-user-keycloak.sh -u admin
設置密碼後,用戶增加成功;需重啟應用才能使用該用戶登錄系統。
1.6、使用 MySQL
Keycloak 預設使用的是 h2 資料庫,可根據需要改為其他的數據,這裡演示改為 MySQL。
1.6.1、安裝 MySQL 驅動
創建 MySQL 模塊目錄:
cd /modules/system/layers/base/com mkdir -p mysql/main
上傳 mysql-connector-java-8.0.28.jar 到 modules/system/layers/base/com/mysql/main 目錄,併在該目錄下創建 module.xml 文件:
<?xml version="1.0" encoding="UTF-8"?> <module name="com.mysql" xmlns="urn:jboss:module:1.9"> <resources> <resource-root path="mysql-connector-java-8.0.28.jar"/> </resources> <dependencies> <module name="javax.api"/> <module name="javax.transaction.api"/> </dependencies> </module>
1.6.2、修改數據源
修改 standalone/configuration/standalone.xml 文件:
<subsystem xmlns="urn:jboss:domain:datasources:6.0"> <datasources> <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true" statistics-enabled="${wildfly.datasources.statistics-enabled:${wildfly.statistics-enabled:false}}"> <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url> <driver>h2</driver> <security> <user-name>sa</user-name> <password>sa</password> </security> </datasource> <datasource jndi-name="java:jboss/datasources/KeycloakDS" pool-name="KeycloakDS" enabled="true" use-java-context="true" statistics-enabled="${wildfly.datasources.statistics-enabled:${wildfly.statistics-enabled:false}}"> <!--connection-url>jdbc:h2:${jboss.server.data.dir}/keycloak;AUTO_SERVER=TRUE</connection-url--> <connection-url>jdbc:mysql://10.49.196.10:3306/keycloak?useUnicode=true&characterEncoding=UTF-8&useSSL=false</connection-url> <driver>mysql</driver> <security> <user-name>root</user-name> <password>123456</password> </security> </datasource> <drivers> <driver name="h2" module="com.h2database.h2"> <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class> </driver> <driver name="mysql" module="com.mysql"> <xa-datasource-class>com.mysql.cj.jdbc.MysqlXADataSource</xa-datasource-class> </driver> </drivers> </datasources> </subsystem>
1.6.3、重啟應用
重啟應用後,Keycloak 會自動在 MySQL 中初始化表;初始化完成後,需重新增加用戶。
2、standalone clustered mode--獨立集群模式安裝
2.1、集群規劃
ip | 安裝軟體 |
10.49.196.10 | keycloak |
10.49.196.11 | keycloak |
10.49.196.12 | nginx |
2.2、先在一臺機器上(10.49.196.10)安裝 Keycloak
2.2.1、配置為使用 MySQL 資料庫
步驟與 1.6、使用 MySQL 一致,但這裡修改的配置文件為 standalone/configuration/standalone-ha.xml。
2.2.2、修改 ip
修改 standalone/configuration/standalone-ha.xml:
<interfaces> <interface name="management"> <inet-address value="${jboss.bind.address.management:10.49.196.10}"/> </interface> <interface name="private"> <inet-address value="${jboss.bind.address.private:10.49.196.10}"/> </interface> <interface name="public"> <inet-address value="${jboss.bind.address:10.49.196.10}"/> </interface> </interfaces>
2.2.3、設置 proxy-address-forwarding 為 true
編輯 standalone/configuration/standalone-ha.xml:
<server name="default-server"> <ajp-listener name="ajp" socket-binding="ajp"/> <http-listener name="default" socket-binding="http" redirect-socket="https" enable-http2="true" proxy-address-forwarding="true"/> <https-listener name="https" socket-binding="https" ssl-context="applicationSSC" enable-http2="true"/> <host name="default-host" alias="localhost"> <location name="/" handler="welcome-content"/> <http-invoker http-authentication-factory="application-http-authentication"/> </host> </server>
2.2.4、啟動
nohup bin/standalone.sh --server-config=standalone-ha.xml &
2.3、在 10.49.196.11 上以同樣的方式安裝 Keycloak
如果集群有多台,也是同樣的安裝方式。
2.4、增加用戶
只需在一臺機器上執行該操作即可。
bin/add-user-keycloak.sh -u admin
用戶增加完成後,重啟該伺服器上的 Keycloak 即可。
2.5、配置代理
這裡使用 nginx 作為代理伺服器,其配置文件如下:
upstream keycloak { server 10.49.196.10:8080 weight=1; server 10.49.196.11:8080 weight=1; ip_hash; } server { listen 8080; server_name localhost; location /auth { proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Port $server_port; proxy_pass http://keycloak/auth; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
這裡建議開啟 ip_hash,因為 keycloak 預設只會在一臺機器上緩存會話信息,如果會話信息不在本機器上會遠程查找,這樣會影響效率。
啟動 nginx 後,通過 http://10.49.196.12:8080/ 來訪問 Keycloak。
2.6、使用 JDBC_PING 協議(可選)
Keycloak 集群間信息同步預設使用 udp 方式下的 PING 協議,可以根據需要該為 tcp 方式或 改為 JDBC_PING 協議。這裡演示修改為 JDBC_PING 協議。修改 standalone/configuration/standalone-ha.xml:
<subsystem xmlns="urn:jboss:domain:jgroups:8.0"> <channels default="ee"> <channel name="ee" stack="udp" cluster="ejb"/> </channels> <stacks> <stack name="udp"> <transport type="UDP" socket-binding="jgroups-udp"/> <!--protocol type="PING"/--> <protocol type="JDBC_PING"> <property name="datasource_jndi_name">java:jboss/datasources/KeycloakDS</property> <property name="initialize_sql">CREATE TABLE IF NOT EXISTS JGROUPSPING (own_addr varchar(200) NOT NULL, cluster_name varchar(200) NOT NULL, updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, ping_data varbinary(5000) DEFAULT NULL, PRIMARY KEY (own_addr, cluster_name)) </property> </protocol> <protocol type="MERGE3"/> <socket-protocol type="FD_SOCK" socket-binding="jgroups-udp-fd"/> <protocol type="FD_ALL"/> <protocol type="VERIFY_SUSPECT"/> <protocol type="pbcast.NAKACK2"/> <protocol type="UNICAST3"/> <protocol type="pbcast.STABLE"/> <protocol type="pbcast.GMS"/> <protocol type="UFC"/> <protocol type="MFC"/> <protocol type="FRAG3"/> </stack> <stack name="tcp"> <transport type="TCP" socket-binding="jgroups-tcp"/> <socket-protocol type="MPING" socket-binding="jgroups-mping"/>
...
所有節點修改後,重啟 Keycloak 即可。
3、domain clustered mode--域集群模式安裝
3.1、集群規劃
ip | 安裝軟體 |
10.49.196.10 | keycloak master |
10.49.196.11 | keycloak slave |
10.49.196.12 | nginx |
3.2、先在 10.49.196.10 上安裝 keycloak master
該模式對應的配置文件為 domain/configuration/domain.xml,該文件中有三個 profile,一般的修改都是針對 auth-server-clustered profile:
3.2.1、配置為使用 MySQL 資料庫
步驟與 1.6、使用 MySQL 一致,這裡修改的地方為 domain/configuration/domain.xml 中 auth-server-clustered profile 下對應的配置。
3.2.2、設置 proxy-address-forwarding 為 true
編輯 domain/configuration/domain.xml:
<server name="default-server"> <ajp-listener name="ajp" socket-binding="ajp"/> <http-listener name="default" socket-binding="http" redirect-socket="https" enable-http2="true" proxy-address-forwarding="true"/> <https-listener name="https" socket-binding="https" ssl-context="applicationSSC" enable-http2="true"/> <host name="default-host" alias="localhost"> <location name="/" handler="welcome-content"/> <http-invoker http-authentication-factory="application-http-authentication"/> </host> </server>
3.2.3、修改 ip
編輯 domain/configuration/host-master.xml:
<interfaces> <interface name="management"> <inet-address value="${jboss.bind.address.management:10.49.196.10}"/> </interface> <interface name="public"> <inet-address value="${jboss.bind.address:10.49.196.10}"/> </interface> </interfaces>
3.2.4、啟動
cd bin nohup ./domain.sh --host-config=host-master.xml -Djboss.bind.address.private=10.49.196.40 &
3.2.5、訪問單點 Keycloak
使用域集群模式時,Keycloak 埠相對 8080 有個偏移量,具體可參考 domain/configuration/host-master.xml:
<servers> <server name="load-balancer" group="load-balancer-group"> <jvm name="default"/> </server> <server name="server-one" group="auth-server-group" auto-start="true"> <jvm name="default"/> <socket-bindings port-offset="150"/> </server> </servers>
偏移量為 150,則地址為 http://10.40.96.132:8230/
3.2.6、添加 Keycloak 用戶
cd $KEYCLOAK_HOME/domain/servers/server-one mkdir configuration #需先創建該目錄 cd $KEYCLOAK_HOME/bin ./add-user-keycloak.sh --sc ../domain/servers/server-one/configuration -u admin #這裡需通過--sc指定server的位置
用戶增加完成後,重啟 Keycloak。
3.2.7、添加 slave 和 master 通信的用戶
shell> ./add-user.sh What type of user do you wish to add? a) Management User (mgmt-users.properties) b) Application User (application-users.properties) (a): Enter the details of the new user to add. Using realm 'ManagementRealm' as discovered from the existing property files. Username : admin User 'admin' already exists and is enabled, would you like to... a) Update the existing user password and roles b) Disable the existing user c) Type a new username (a): Password recommendations are listed below. To modify these restrictions edit the add-user.properties configuration file. - The password should be different from the username - The password should not be one of the following restricted values {root, admin, administrator} - The password should contain at least 8 characters, 1 alphabetic character(s), 1 digit(s), 1 non-alphanumeric symbol(s) Password : WFLYDM0099: Password should have at least 8 characters! Are you sure you want to use the password entered yes/no? yes Re-enter Password : What groups do you want this user to belong to? (Please enter a comma separated list, or leave blank for none)[ ]: Updated user 'admin' to file '/dataload/chatops/soft/keycloak-16.1.1-domain/standalone/configuration/mgmt-users.properties' Updated user 'admin' to file '/dataload/chatops/soft/keycloak-16.1.1-domain/domain/configuration/mgmt-users.properties' Updated user 'admin' with groups to file '/dataload/chatops/soft/keycloak-16.1.1-domain/standalone/configuration/mgmt-groups.properties' Updated user 'admin' with groups to file '/dataload/chatops/soft/keycloak-16.1.1-domain/domain/configuration/mgmt-groups.properties' Is this new user going to be used for one AS process to connect to another AS process? e.g. for a slave host controller connecting to the master or for a Remoting connection for server to server Jakarta Enterprise Beans calls. yes/no? yes To represent the user add the following to the server-identities definition <secret value="MTIzNDU2" />
創建完成後會生成一個密鑰:<secret value="MTIzNDU2" /> , 後面在配置 slave 時會用到該密鑰。
3.3、在 10.49.196.11 上安裝 keycloak slave
3.3.1、安裝 MySQL 驅動
步驟與 1.6.1、安裝 MySQL 驅動 一致。
3.2.2、修改 ip
編輯 domain/configuration/host-slave.xml:
<interfaces> <interface name="management"> <inet-address value="${jboss.bind.address.management:10.49.196.11}"/> </interface> <interface name="public"> <inet-address value="${jboss.bind.address:10.49.196.11}"/> </interface> </interfaces>
3.2.3、修改與 master 通信的鑒權信息
A、對於 16.0.0 之前(不包含)的版本
編輯 domain/configuration/host-slave.xml:
<management> <security-realms> <security-realm name="ManagementRealm"> <server-identities> <secret value="MTIzNDU2Nw==" /> </server-identities> <authentication> <local default-user="$local" skip-group-loading="true"/> <properties path="mgmt-users.properties" relative-to="jboss.domain.config.dir"/> </authentication> ... <domain-controller> <remote security-realm="ManagementRealm" username="admin"> <discovery-options> <static-discovery name="primary" protocol="${jboss.domain.master.protocol:remote+http}" host="${jboss.domain.master.address}" port="${jboss.domain.master.port:9990}"/> </discovery-options> </remote> </domain-controller>
修改的部分參見斜體字。
B、對於 16.0.0 之後(包含)的版本
編輯 domain/configuration/host-slave.xml:
<domain-controller> <remote authentication-context="hcAuthContext"> <discovery-options> <static-discovery name="primary" protocol="${jboss.domain.master.protocol:remote+http}" host="${jboss.domain.master.address}" port="${jboss.domain.master.port:9990}"/> </discovery-options> </remote> </domain-controller> ... <profile> <subsystem xmlns="urn:jboss:domain:core-management:1.0"/> <subsystem xmlns="urn:wildfly:elytron:15.0" final-providers="combined-providers" disallowed-providers="OracleUcrypto"> <authentication-client> <authentication-configuration name="hostAuthConfig" authentication-name="admin" realm="ManagementRealm"> <credential-reference clear-text="123456"/> </authentication-configuration> <authentication-context name="hcAuthContext"> <match-rule authentication-configuration="hostAuthConfig"/> </authentication-context> </authentication-client> <providers> <aggregate-providers name="combined-providers"> <providers name="elytron"/> <providers name="openssl"/> </aggregate-providers> <provider-loader name="elytron" module="org.wildfly.security.elytron"/> <provider-loader name="openssl" module="org.wildfly.openssl"/> </providers>
...
修改的部分參見斜體字。
3.2.4、啟動
cd bin nohup ./domain.sh --host-config=host-slave.xml -Djboss.bind.address.private=10.49.196.11 -Djboss.domain.master.address=10.40.96.10 &
3.2.5、訪問單點 Keycloak
使用域集群模式時,Keycloak 埠相對 8080 有個偏移量,具體可參考 domain/configuration/host-slave.xml:
<servers> <server name