728x90

SMACK 4.3.0 으로 OpenFire Server 에 로그인하는 기능을 적어둔다.

4.1.0 으로 해도 에러가 발생하고 ... 엄청난 삽질을 하고서 겨우 제대로 로그인되는 걸 확인했다.


build.gradle

repositories {
    maven {
        url 'https://oss.sonatype.org/content/repositories/snapshots'
    }
    mavenCentral()
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:recyclerview-v7:27.1.1'
    implementation 'gun0912.ted:tedpermission:2.0.0'
    implementation 'com.google.code.gson:gson:1.7.2'
    implementation "org.igniterealtime.smack:smack-tcp:4.3.0"
    implementation "org.igniterealtime.smack:smack-extensions:4.3.0"
    implementation "org.igniterealtime.smack:smack-experimental:4.3.0"
    implementation ("org.igniterealtime.smack:smack-android:4.3.0"){
        exclude module:'smack-omemo'
        exclude module:'smack-omemo-signal'
    }
}
 



로그인이 성공되어야 채팅 기능을 만드는 걸 해볼 수 있는데 smack 4.2.0 으로 테스트하면 로그인에서 거부당한다.

아래 코드는 로그인이 성공적으로 잘 되는 걸 확인했으며, 동영상 예제를 따라하면서 적은 코드를 수정 보완한 것이라고 보면 된다.

따라서 구글링으로 검색된 다른 코드와는 약간 다르다. 즉 확장성이 떨어질 수 있다는 말이다.

로그인이 성공적으로 동작되도록 하는 코드라고 이해하면 된다.


Anroid 8.0에서 테스트를 했으며, 보안 강화로 IP주소를 직접 적으면 동작에 오류가 발생하는 거 같다.


private String loginURL = "abc.com"; // IP주소 적으면 오류 발생함 (The public/private ip address don't work)
private AbstractXMPPConnection mConnection;

public static final String TAG = MainActivity.class.getSimpleName();


private void setConnection() {
    // Create the configuration for this new connection
    new Thread() {
        @Override
        public void run() {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);

            InetAddress addr = null;
            try {
                addr = InetAddress.getByName(loginURL);
                Log.e(TAG,"addr : "+addr);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
            HostnameVerifier verifier = new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return false;
                }
            };
            DomainBareJid serviceName = null;
            try {
                serviceName = JidCreate.domainBareFrom(loginURL);
                Log.e(TAG,"serviceName : "+serviceName);
            } catch (XmppStringprepException e) {
                e.printStackTrace();
            }
            SmackConfiguration.DEBUG = true;
            XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
                    //.setUsernameAndPassword("test02", "test1234")
                    .setPort(5222)
                    .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                    .setXmppDomain(serviceName)
                    .setHostnameVerifier(verifier)
                    .setHostAddress(addr)
                    //.setDebuggerEnabled(true) // smack 4.3.0 error, smack 4.2.0 ok
                    .setSendPresence(true)
                    .build();

            SASLAuthentication.blacklistSASLMechanism("SCRAM-SHA-1");
            SASLAuthentication.blacklistSASLMechanism("DIGEST-MD5");
            SASLAuthentication.unBlacklistSASLMechanism("PLAIN");
            mConnection = new XMPPTCPConnection(config);
            mConnection.setReplyTimeout(10000);

            // Connect to the server
            try {
                Log.e(TAG,"mConnection.connect try");
                mConnection.connect();               
                mConnection.login("test01", "test1234");
                if (mConnection.isAuthenticated() && mConnection.isConnected()) {
                    // message send and receive code here.
                    Log.e(TAG,"run auth done and connected successfully");                    
                } else{
                    Log.e(TAG,"connected fail");
                }

            } catch (SmackException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XMPPException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }.start();
}



참고 사이트(reference)


'openfire' 카테고리의 다른 글

CentOS 6.6에 openfire 4.2.3 설치  (0) 2018.09.21
블로그 이미지

Link2Me

,
728x90

XML에 기반한 실시간 메시지 지향 공개 표준 통신프로토콜인 XMPP(Extensible Messaging and Presence Protocol)을 사용한 오픈소스 서버와 클라이언트가 이미 공개돼 있다.

이를 이용해서 간단히 메신저를 구축 할 수 있다.


유투브 동영상 강좌를 우연히 보다가 openfire 메신저를 설치해봤다.


설치환경 : CentOS 6.6 64bit, MySQL 5.6.30


설치하면서 MySQL 구동중인 것이 장애가 발생하는 증상이 있어서 MySQL 문제발생을 조치하고 나서, MySQL을 stop시킨 후 openfire 를 Stop ==> Remove 한 다음 재설치를 하고 나서 성공했다.


OpenFire 사이트 : https://www.igniterealtime.org/downloads/index.jsp


/etc/init.d/mysqld start
/etc/init.d/mysqld stop

0. 가장 먼저 해야 할 일은 MySQL DB를 stop 시키고 나서 설치를 하기 바란다.
    에러가 나면 에러 원인 찾아서 해결하기가 쉽지 않더라.

1. mkdir -p /home/openfire

2. openfire.rpm 다운받기
   wget http://igniterealtime.org/openfire/openfire-4.2.3-1.x86_64.rpm  

3. rpm 설치하기
   yum install openfire-4.2.3-1.x86_64.rpm
   설치를 하면 /opt/openfire 디렉토리에 설치된다.
   설치했던 openfire 를 삭제하는 방법은 yum remove openfire 다. MySQL DB를 stop하고 하는 것이 좋을 거 같다.

4. service 시작하기
   service openfire start

5. 방화벽 설정에 9090 포트를 추가한다.


6. MySQL DB에서 openfire 라는 DB를 생성한다.

    /etc/init.d/mysqld start

    MySQL 접속 후 openfire DB 생성


7. Web 브라우저 콘솔 접속
   http://ipaddress:9090

   순서에 따라 설치
   DB Setting 화면에서 설정을 잘못하면 고생을 할 수 있다.


Database URL : jdbc:mysql://localhost:3306/openfire

Username : root 또는 별도 생성한 db_username

Password : root 패스워드 또는 db_username의 패스워드


이 부분을 신경쓰면 설치하는데는 문제없이 잘 넘어간다.

admin 과 지정한 패스워드를 입력하면 관리자 화면으로 접속된다.



phpMyAdmin 으로 살펴보면

openfire DB에 테이블이 34개 생성되어 있는 걸 알 수 있다.




이제 http://www.igniterealtime.org/downloads/index.jsp 에서 Spark 를 받아서 설치한다.





시스템이 재부팅되어도 openfire 자동 실행되도록 설정
#chkconfig --add openfire
#chkconfig openfire on


Android 어플 개발시에 OpenFire 서버와 통신하기 위해서 Security : SSL Certificates 가 필요하다고 한다.
Because our server doesn’t have a public facing domain name, and is running locally, we can’t set up an SSL certificate for it. So we will need to disable security when connecting to our server as shown below
.setDebuggerEnabled(true)
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled) // Should make required on Public Server with SSL Configured
.setCompressionEnabled(true).build();


검색하다보니 XMPP 또다른 서버가 있다.




블로그 이미지

Link2Me

,