跳到主要內容

Java EE(一):Glassfish JAAS JDBC Realm 設定 & SSO補充

甚麼是JAAS可以參考一下JWorld@TWOracle Jaas,在進行專案開發時,基本上人員權限一定是考量進去的,這次來介紹一下如何在Glassfish專案中設定 jdbc realm的方式進行。

既然是jdbc當然要先規劃一下db schema,有三個資料表要建立,user(人員資料表)、role(角色群組表)、userrolemapping(人員角色對應表),user資料表內要有帳號(username)及密碼(password),role資料表角色代號(role_code),userrolemapping資料表帳號(username)及角色代號(role_code)對應,這裡我會建議設計一個view比較方便:

 SELECT user.username, user.password, role.role_code
   FROM user, role, userrolemapping
  WHERE user.username = userrolemapping.username AND role.role_code= userrolemapping.role_code;

若是JPA當然就用Id囉,

 SELECT user.username, user.password, role.role_code
   FROM user, role, userrolemapping
  WHERE user.id= userrolemapping.user_id AND role.id= userrolemapping.role_id;

啟動Glassfis並進入admin console
http://localhost:4848



  • 選擇資源(Resources)--JDBC-- JDBC Connection Pool,按下新增
Pool Name:test
資源類型:javax.sql.DataSource
資料庫驅動程式供應商:這裡使用Postgresql,就選Postgresql Driver。

按下Next


 輸入以下特性,並按下確定。
 可以測試是否設定正確,按下Ping進行測試。


  • 新增JDBC資源,資源--JDBC--JDBC資源,按下新增
JNDI名稱:jdbc/test
池名稱:剛剛建立的Pool名稱即可。
輸入完畢後按下確定。


  • 新增JDBCRealm,配置--server-config--安全性--範圍,按下新建,選擇JDBCRealm,
名稱:testAuth。
JAAS環境:jdbcRealm,這要注意就是jdbcRealm大小寫要一模一樣。
JNDI:jdbc/test,剛剛設定的JDBC名稱。主要用於連接資料庫使用。
使用表格名稱:view table名稱。
使用者名稱欄:username
密碼欄:password
群組表格:view table名稱。
群組名稱欄:role
摘要演算法:MD5,這裡要注意一下,Default為SHA-256,並注意資料表中密碼欄位請以MD5方式儲存,參考程式如下:

    public String encoderByMd5(String str) throws UnsupportedEncodingException {       
         java.security.MessageDigest md5 = java.security.MessageDigest.getInstance("MD5");
        return new String(org.apache.commons.codec.binary.Hex.encodeHex(md5.digest(str.getBytes())));

    }

並按下儲存。






  • 進行Application web.xml檔案設定,Netbeans中打開web.xml(也可以直接修改啦),點選Security--Login Configuration,勾選選Form的選項
Form Login Page:輸入登入頁的路徑,請注意該頁面有幾個必要欄位,如下

Form Action:j_security_check
Form Input:j_username及j_password

Form Error Page:輸入登入失敗對應的路徑頁面。
Security Role:新增與role一樣的角色。
儲存檔案,重新啟動Glassfish,並Deploy Application,開啟Browser輸入Application網址,這時候應該不管輸入哪個網址都會redirect到Login的頁面囉。




SSO補充:

有時候會有多組Application需要access同一Realm做到SSO的情形,就可以在配置--server-config--虛擬伺服器--選擇你要SSO的Server,通常與Realm同一台,將SSO選項選擇已啟用,然後在其他特性中增加兩組特性。

sso-max-inactive-seconds:300
sso-reap-interval-seconds:60

按下儲存,重新啟動Glassfish,就可以兩組或兩組以上Application使用同一個realm,而且只要登入其中一組Application的Login頁面後,進入其他Application時就不用在登入了。













留言

這個網誌中的熱門文章

IReport中的條碼類別BcImage

最近有一個繳費單的專案,需要列印條碼,因為IReport本身就有提供列印條碼的功能,所以就用IReport設計繳費單然後再用Jasperreport API寫批次程式去產生PDF,資料量大概3000多筆(頁)拆成幾個檔案,可是就發生了一個問題居然發生部分繳費單的條碼有問題,看了一下程式研判是Race Condition因為程式採用的是多執行緒,若是依序個別產生是不會有問題,但是同時執行的時候就會亂掉,而且都錯在條碼,令我覺得很奇怪,後來我查了一下發現it.businesslogic.ireport.barcode.BcImage 類別我猜是這裡的問題,就先加上synchronized重新編譯在去執行,嘿嘿條碼就對了,做了一下壓測也正常,程式碼如下,不過這樣事不是對了可能還要仔細查查看。 public class BcImage { private static net.sourceforge.barbecue.Barcode bc = null; public synchronized static net.sourceforge.barbecue.Barcode getBarcode() { return bc; } public synchronized static BufferedImage getBarcodeImage(int type, Object aText, boolean showText, boolean checkSum) { return getBarcodeImage(type, aText, showText, checkSum,"",0,0); } public synchronized static BufferedImage getBarcodeImage(int type, Object aText, boolean showText, boolean checkSum, String applicationIdentifier, int width, int height) { // 2of7, 3of9, Bookland, Codabar, Code128,...

IBM MQ 5.3 如何匯出Qmgr

使用IBM MQ探險家也沒有匯出的功能,這樣其實在一些備份或是Cluster時,滿困擾的,不過你也知道IBM就是喜歡用SupportPacs等等的方式,ms03這個SupportPacs就是可以匯出Qmgr中所有的資訊也可以選擇想要的,以下是他的連結 http://www-1.ibm.com/support/docview.wss?rs=171&q1=mA1J&uid=swg24000673&loc=en_US&cs=utf-8&lang=en 若是Window的版本可以直接使用,若是unix或是linux系列的就請重新編譯了﹝makeFile﹞我有試過在RHEL編譯使用是可以的。 ※請注意一定要在有安裝MQ的Server方可執行喔 使用方式如下: saveqmgr -m xxx -v 53 -o xxx.cfg -m 指定需要匯出的Qmgr名稱 -v 版本 -o 匯出後檔案名稱 以下是相關參數的列表 Usage is: ./saveqmgr. [options], where [options] are one or more of the following optional switches -h | -? : gives help (this) -v version : determines which version of MQSC to generate and can be '2','5','51','52','53' or '6' The default is to generate mqsc at the version of the connected queue manager -m lqmgr : is the name of the local qmgr to connect (MQCONN) -r rqmgr : is the name of the remote qmgr (XMITQ name) -f [file] : allows the output file to be named, if -f is not specified, the outpu...

Ext-Js Grid + DWR

Ext-Js中有Grid的sample,想說試試看加上DWR的效果如何?感覺上還不錯,以下是我參考Ext-Js附的grid array sample,加上DWR調整一下的code,我想可能還要加上資料在Loading的效果會比較好。 array-grid.js Ext.onReady(function(){ Ext.state.Manager.setProvider(new Ext.state.CookieProvider()); // example of custom renderer function function change(val){ if(val > 0){ return ' ' + val + ' '; }else if(val ' + val + ' '; } return val; } // example of custom renderer function function pctChange(val){ if(val > 0){ return ' ' + val + '% '; }else if(val ' + val + '% '; } return val; } //要設定Dwr傳回的Map的對應格式 var recordType = Ext.data.Record.create( [ {name:"reportid",mapping:"reportid",type:"string"}, {name:"reportName",mapping:"reportName",type:"string"} ] ); var myReader = new Ext.data.JsonReader( { totalProperty:"totalSize", root:"data" },recordType ); // create the data store //這裡是很重要的,這裡還可以加上listener等等的屬性喔 var store = new Ext.da...