跳到主要內容

發表文章

目前顯示的是有「Java Library」標籤的文章

AD/LDAP with Spring API

Articles Simple Authentication   SSL and StartTLS Spring-Security with LDAP物件關係 ActiveDirectoryLdapAuthenticationProvider Support SAM-Account-Name Support TLS LdapAuthenticationProvider 如何透過LDAP做認證? Support Secure connection Support StartTLS with anonymous login Search AD User and Groups LdapContextSource contextSource = new DefaultSpringSecurityContextSource ( "ldap://10.134.15.138:389" ) ; contextSource. setBase ( "DC=TEST,DC=SSM" ) ; contextSource. setUserDn ( "test@TEST.SSM" ) ; contextSource. setPassword ( "123456" ) ; DirContextAuthenticationStrategy strategy = createTLSStrategy ( ) ; contextSource. setAuthenticationStrategy ( strategy ) ; contextSource. afterPropertiesSet ( ) ;   LdapTemplate ldapTemplate = new LdapTemplate ( contextSource ) ; ldapTemplate. afterPropertiesSet ( ) ;   SearchControls sc = new SearchControls ( ) ; sc. setSearchScope ( SearchControls . SUBTREE_SCOPE ) ;   ldapTemplate. search ( ...

Jackson文章列表

Introduction 在開發Rest API時,透過Jackson提供的API,將物件轉為json或xml格式。它曾經算是效能最好的函式庫之一,現在就不一定了。 Jackson - Hello World From 2.4.5 to 2.7.1 Convert the key fields with special char @JsonSerialize(keyUsing=KeySerializer.class) Convert the Date format to ISO8601 @JsonSerialize(using=Serializer.class) @JsonDeserialize(using=Deserializer.class) Optionally ignore some fields @JsonIgnore @JsonIgnoreProperties The visibility of the fields @JsonAutoDetect Convert the value fields with special char @JsonSerialize @JsonDeserialize DeepClone with Jackson Resource How to rename element names? @JsonProperty(“contractor”) Serializing Map<Date, String> with Jackson Jackson Modules for Map Serialization Jackson Annotations

Jasypt With Hibernate

Problem 使用這個API的目的主要是為了無痛去加解密資料庫的某個欄位,且它支援Hibernate、Spring的整合方式。但最後發現,要無痛是很困難的,除非只是很單純的加密像密碼的東西。 How to? 安裝Java Cryptography Extension (JCE) 如果出現Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy File錯誤訊息,請自行到Oracle官網下載JCE包(JCE for Java8),並安裝到%JAVA_HOME%/jre/lib/security中。如果有問題,可以設定JAVA_HOME環境變數再試試看。 (JCE包內容有US_export_policy.jar與local_policy.jar) Setup Hibernate & Spring 我的Ivy.xml相依於此兩個libraries: <dependency org = "org.jasypt" name = "jasypt" rev = "1.9.2" /> <dependency org = "org.jasypt" name = "jasypt-hibernate4" rev = "1.9.2" /> 最簡單的方式 只要在hbm檔內,將你要加密的欄位設定一下就搞定了。 Hibernate設定: <hibernate-mapping > <typedef name = "encryptedString" class = "org.jasypt.hibernate4.type.EncryptedStringType" > <param name = "algorithm" > PBEWithMD5AndTripleDES </param > <param name = "password" > jasypt </pa...

Jasypt With Properties File

How to? Jasypt的EncryptableProperties類別,提供讀取加密過後的properties檔案,其中密文以ENC()包住。 # #Sat Jan 16 00:52:05 CST 2016 password=ENC(xWWu6PWYHwfVmNMTXCJG5A\=\=) account=root 以下為測試範例,存檔是透過一般的Properties類別,需要加密的部分透過PropertyValueEncryptionUtils類別去做轉換;讀檔則使用EncryptableProperties類別: @Test public void test ( ) throws IOException { StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor ( ) ; encryptor. setPassword ( "jasypt" ) ;   Properties p = new Properties ( ) ; p. setProperty ( "account" , "root" ) ; p. setProperty ( "password" , PropertyValueEncryptionUtils. encrypt ( "123456" , encryptor ) ) ;   File file = new File ( "test.properties" ) ; file. createNewFile ( ) ;   FileOutputStream fos = null ; try { fos = new FileOutputStream ( file ) ; p. store ( fos, "gg" ) ; } finally { close ( fos ) ; }   EncryptableProperties ep = new EncryptableP...

Afresco - JLAN

Introduction 因為某個功能,我們需要用到Samba與NFS。又基於為了讓客戶能方便的使用,我們就只好苦幹實幹。我是以Java solution為主,因此發現了Afresco產品下的JLAN。JLAN有以下特點: 授權: LGPL。能在SourceForge下載到的JLAN,是2009年版本,屬於GPL授權。後來在2011年全被改為LGPL,也代表著可以正當的商用。 功能: 提供Samba(netbios)、FTP與NFS的實做。並有權限控管之設定。 跨平台: 因為Java-based solution。 對於原本已存在於系統的相同服務,可以透過修改port去與原生服務共存。 使用 jlanConfig.xml為jlan設定檔。詳細內容可以參考它的使用者手冊,我只挑幾個項目做說明: servers block 透過<SMB>、<FTP>、<NFS>標籤啟動服務,如果不要使用可以加上no: <NOSMB>。 SMB broadcast: 輸入broadcast的mask。 netBIOSSMB: 屬性platforms可用linux,windows;屬性port預設為139。 tcpipSMB: 屬性platforms可用linux,windows;屬性port預設為445。 如果系統上有SMB service,會造成port的conflict,可以選擇將它disable或使用其它的port。經過我的測試,使用其它替代port是無法讓檔案瀏覽器正常連結SMB的;但如果使用SMB client API link,如JCFS,是能夠正常存取,應能達到系統整合需求。 NFS PortMapperPort: 預設為111。 NFSServerPort: 預設為2049。 如果系統上有NFS service,會造成port的conflict。PortMapper也許不只有NFS在使用,因此建議使用其它port較好。而在設定完其它port後,可以透過以下指令去mount: mount -oport =yourNfsPort, mountport =yourMountPort, proto =udp yourFfsServerName: / alfresco / mnt / alfresco / 經過我測試,有幾個問題: ...

JCIFS Hello World

Introduction JCIFS是java-based存取網路芳鄰通訊協定的API。 之所以接觸這個API,是因為好奇Windows管理工具psexec,能將Local程式放到Remote並執行的做法。後來看到類似工具 paexec 程式碼,與經過自己的測試,發現原理是將程式複製到遠端的 管理共享資料夾 ,再透過WMI呼叫外部程式。 接下來將說明我有使用的API。 How to? 首先至 link 下載library。接下來就是寫寫寫。 Create File 大部分的操作都可以透過SmbFile類別完成。而與遠端的機器認證,是透過NTLM通訊協定。 private static NtlmPasswordAuthentication createAuth ( String aUser, String aPasswd ) { StringBuffer sb = new StringBuffer ( aUser ) ; sb. append ( ':' ) . append ( aPasswd ) ; return new NtlmPasswordAuthentication ( sb. toString ( ) ) ; }   public static SmbFile createSmbFile ( String aUser, String aPasswd, String aTarget ) throws IOException { NtlmPasswordAuthentication auth = createAuth ( aUser, aPasswd ) ; return new SmbFile ( aTarget, auth ) ; } 如果有網域,可以使用 new NtlmPasswordAuthentication ( domain, user, passwd ) ; 在建立完SmbFile物件後,剩下其實就和操作一般檔案無異。 public static void createFile ( String aUser, String aPasswd, String aTarget, String aContent ) ...