2017年4月17日 星期一

自己sign憑證

ip使用ssl,會出現Hostname ip not verified:subjectAltNames: []
若ip要使用,先找出openssl.cnf檔案位置,可用find / -name openssl.cnf指令
並將該檔案複製到憑證目錄下,並在[ v3_ca ]的區塊加入subjectAltName = IP:${ip}
在建憑證時就會將subjectAltName放到憑證中


//create private key
openssl genrsa -out private.pem 2048

//Create a self-signed X509 certificate (csr)
openssl req -new -x509 -key private.pem -out certificate.pem -days 730
//若是只使用ip則
openssl req -new -x509 -key private.pem -out certificate.pem -days 730 -config ./openssl.cnf


//create public key
openssl rsa -in private.pem -pubout > public.pem


給node express用的可以上方三步驟就好,下面要sign出keystore,設定到tomcat中


//create pkcs12 keystore
openssl pkcs12 -export -out keystore.pkcs12 -in certificate.pem -inkey private.pem


//Convert the PKCS12 keystore to Java keystore using Java keytool
keytool -importkeystore -srckeystore keystore.pkcs12 -srcstoretype PKCS12 -destkeystore keystore.jks -deststoretype JKS


ps: 之後再將keystore設定在server端,將trust store設定在client 端,如此便可以加密的方式連線
如果是單純app和server side可以自己sign憑證就好,若需要web建議用第三方的憑證




產生private key 然後利用private key產出request file就是csr
然後提供給SSL提供商 他們透過csr產出cer 就是認證檔
他們把我的server domain加入到他們的chain裡
接著將cer, key, chain cer, root cer加入到server

cer就像是public key


第三方會給 cer, chain cer, root cer
查詢的流程: cer指向chain cer, chain cer指向root cer


2016年7月20日 星期三

Git 指令



  • git clone ${git.url} - 取出git專案
  • git commit ${file} -m "describe" - commit git檔案,只在本機端
  • git push -u origin master - 上傳commit的檔案到server端
  • git pull - 將server端的檔案update回本機端

2016年7月10日 星期日

錯誤:java.lang.NoClassDefFoundError: org/eclipse/core/resources/ResourcesPlugin

當啟動eclipse出現下方Error時
java.lang.NoClassDefFoundError: org/eclipse/core/resources/ResourcesPlugin


可嘗試到workspace下,刪掉*.snap的檔案

${workspace}/.metadata/.plugins/org.eclipse.core.resources/

2016年6月23日 星期四

Java Servlet HttpServletRequest getReader in Tomcat 筆記

HttpServletRequest.getReader() 

  • 在tomcat下,若post過來的資料有指定content-type=text/plain才抓得到資料。
    • 目前在tomcat6, tomcat8下測試都是此情形
  • 在resin下則是正常(resin 3.x)
  • 而在php下的話若content-type=text/plain,則要用file_get_contents('php://input')才取得到值。
  • 測試用php程式碼如下:

<?php
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "${url}" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "a=abc&body goes here" ); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain')); 

$result=curl_exec ($ch);
echo $result;
?>

2016年6月21日 星期二

Laravel HTTP Route (一)

基本說明

  • route資料定義在{laravel_dir}/app/Http/routes.php
  • 可指定get, post, put, delete等請求方式
  • 有哪些method可以使用,可以參考{laravel_dir}/vendor/laravel/src/Illuminate/Routing/Router.php

註冊路由



Route::get('/user/{userName}', function ($userName) {
.....
});
  • 當執行http://domain/user/test時,則$userName會被注入test這個值
  • 若使用post的方式存取http://domain/user/{userName}則會丟出MethodNotAllowedHttpException
  • 若userName為非必填則可將{userName}改為{userName?},並將callback function內的$userName給予一個預設值,ex: $userName = null



Route::post('/updateUser', function () {
    ...
});
  • 僅允許使用post的方式,若使用get的方式存取則會丟出MethodNotAllowedHttpException
  • 若使用任一html post到此網址,則會丟出TokenMismatchException ,防止CSRF(跨網站偽造請求)
    • Laravel會產生一個CSRF的token給予session做驗證使用,驗證是否為本身的app發出的Request
  • 在發出request的html內加入
    <input name="_token" type="hidden" value="<?php echo csrf_token(); ?>" />
    也可將該input的tag整個改為{{ csrf_field() }} (blade template engine 的語法),則會自動產生該句input
  • 若有url不要CSRF的保護可以去{laravel_dir}/app/Http/Middleware/VerifyCsrfToken.php內加入例外,該程式如下:

class VerifyCsrfToken extends BaseVerifier
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        //
    '/updateUser'
    ];
}


2016年6月20日 星期一

Laravel Migration 使用


Migration的指令


在建立migration前,先確認config的user, password的設定是否符合資料庫系統,並確認database是否存在。
確認config可參考:http://kon-tw.blogspot.tw/2016/06/laravel-config-cache.html

建立migration (建立table)

php artisan make:migration create_order_table --create="order"

至於程式可以用哪些method 可以去看這支程式{laravel_dir}\vendor\laravel\framework\src\Illuminate\Database\Schema\Blueprint

建立完預設程式碼如下

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateOrderTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('order', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('order');
    }
}


對table插入一個新的欄位

php artisan make:migration add_member_id_to_order_table --table="order"


執行migration

php artisan migrate

  • 第一次執行migration時,laravel會在db自動建立一個table: migrations
  • 該table存放執行過哪些migration 的檔案
  • 下次執行就不會去執行已存在該table內的那些檔
  • 執行migrate時,是執行migration檔內up這個method。


Rollback migration

php artisan migrate:rollback

  • 當執行rollback時,會去執行各個migration檔內down這個method。並刪掉migrations內的資料。
  • 其rollback的順序是看migrations這個table內的batch,從數字最大的開始rollback。
  • 若batch內有1,2,第一次執行rollback,則會去執行batch=2的檔案,再執行rollback才會執行batch=1的檔案。
  • 會去執行migration檔內down這個 method

清除所有migration


php artisan migrate:reset


清除所有migration, 並重新執行migration


php artisan migrate:refresh



2016年6月19日 星期日

laravel artisan 執行流程


1. 使用{laravel dir}/bootstrap/app.php

2. 在app.php中會去建立一個Illuminate\Foundation\Application的物件

3. 該Application
        a. 繼承Container(Illuminate\Container\Container)
b. 實作 ApplicationContract(Illuminate\Contracts\Foundation\Application)
c. 實作HttpKernelInterface(Symfony\Component\HttpKernel\HttpKernelInterface)

4. app.php中,會再去建立三個singleton(在Container singleton的method)的物件, artisan用到的則     是console Kernel的部份
        a. Illuminate\Contracts\Http\Kernel
b. Illuminate\Contracts\Console\Kernel
c. Illuminate\Contracts\Debug\ExceptionHandler

5. {laravel dir}/artisan 去取得Console Kernel的singleton物件
        a. 執行app的handle method() (Illuminate\Foundation\Console\Kernel)
b. handle內會去new Illuminate\Console\Application(artisan主要程式
            繼承Illuminate\Console\Application)
c. 執行 Illuminate\Console\Application 內的method run();
d. run() 內會再執行Illuminate\Console\Application doRun 並回傳結果