跳到主要內容

發表文章

Java8 SecureRandom

Random java.util.Random 其實一點也不 Random, 因為屬於一種線性分佈, 線性不比離散, 所以就是有公式可以預測啦~ 大神的論文在 這裡 https://stackoverflow.com/questions/11051205/difference-between-java-util-random-and-java-security-securerandom The standard Oracle JDK 7 implementation uses what's called a Linear Congruential Generator to produce random values in java.util.Random. Predictability of Linear Congruential Generators Hugo Krawczyk wrote a pretty good paper about how these LCGs can be predicted ("How to predict congruential generators"). If you're lucky and interested, you may still find a free, downloadable version of it on the web. And there's plenty more research that clearly shows that you should never use an LCG for security-critical purposes. This also means that your random numbers are predictable right now, something you don't want for session IDs and the like. 隨機數安全議題 http://wps2015.org/drops/drops/%E8%81%8A%E4%B8%80%E8%81%8A%E9%9A%8F%E6%9C%BA%E6%95%B0%E5%AE%89%E5%85%A8.html Secu...

Java8 convert List to Map

Chung Nguyen Sample Data [ {"uid":"1","name":"阿花","age":38,"gender":"F"}, {"uid":"2","name":"阿瓜","age":28,"gender":"M"} ] Entity @Data @JsonInclude(JsonInclude.Include.NON_NULL) @JsonPropertyOrder(value = { "uid", "name", "age", "gender" }) public class Customer { // creativeId equals adId @JsonProperty("uid") private String uid; @JsonProperty("name") private String name; @JsonProperty("age") private Integer age; @JsonProperty("gender") private String gender; } Test @Test public void test() throws IOException { List<Customer> customerList = objectMapper.readValue(json, new TypeReference >() {}); Map customerMap = customerList.stream() .collect(Collectors.toMap( ...

Spring-data JPA 搭配 Optional 的使用

Optional Java 最大的敵人, 我想應該就是 NPT Exception , tears. 所以 Java8 設計了一個 Optional 用來解決 NPT 這淘氣的小東西, fuck. 詳細的使用方式 參閱這個 bloger 的介紹, 寫的超棒, 我覺得啦 :) JpaRepository Spring framework 做 CRUD 的好朋友, 就是 JpaRepository , 沒有用 Optional 的寫法像這樣 @Repository public interface UserInfoRepository extends JpaRepository<UserInfo, String> { UserInfo findByName(String name); } 這樣寫其實也沒什麼不合理, 只是在商業邏輯處理時, 要多一個 if 判斷, 判斷是否有查詢到 userInfo 的資料. public void business() { UserInfo user = userInfoRepository.findByName("dumdum"); if (Objects.isNull(user)) { // throw exception } else { // do something } } 改為用 Optional 試試看 @Repository public interface UserInfoRepository extends JpaRepository<UserInfo, String> { Optional<Userinfo> findByName(String name); } public void business() { Optional<Userinfo> userOpt = userInfoRepository.findByName("dumdum"); UserInfo user = userOpt.orElseThrow(() -> new Exception(...

Spring boot Https 設定

1. 產生 SSL certificate keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650 Enter keystore password: Re-enter new password: What is your first and last name? [Unknown]: jerry What is the name of your organizational unit? [Unknown]: td What is the name of your organization? [Unknown]: com What is the name of your City or Locality? [Unknown]: taipei What is the name of your State or Province? [Unknown]: taiwan What is the two-letter country code for this unit? [Unknown]: tw Is CN=jarvis, OU=td, O=urad, L=taipei, ST=taiwan, C=tw correct? [no]: yes 這個 certificate 是 self-signed certificate 沒有經過第三方認證, 所以沒有公信力, 正式上線會在瀏覽器看到 連線不被信任 要有公信力的 certificate 最簡單的是 Lets Encrypt , 其他就是花一些錢找簽發 certificate 的組織 2. Enable HTTPS in Spring Boot Spring Boot 內建的 tomcat 預設 http 是 8080, Spring Boot 可以設定 http 跟 https, 但沒辦法同時存在這兩個設定, 如果要同時存在兩種 connection, 建議依照 文件 建議設定 https, 再透過 programmatically 去設定 http 會比較容易。 相關範例可以參考 : https://...

Spring Data JPA Transaction

Entity @Data @NoArgsConstructor @AllArgsConstructor @Entity(name = "messenger") @IdClass(MessengerPrimaryKey.class) public class Messenger implements Serializable { @Id @Column(name = "user_id", length = 64, nullable = false) private String userId; @Id @Column(name = "psid", length = 64, nullable = false) private String psId; @Column(name = "page_id", length = 64, nullable = false) private String pageId; } PrimaryKey @Data @NoArgsConstructor @AllArgsConstructor public class MessengerPrimaryKey implements Serializable { private String userId; private String psId; } Test1 transactionalTest1 是基本的 transactional, 在 transactionalTest1 結束後會做一個 commit import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.transaction.annotation.Transactional; import tw.com.u...

Facebook Bot 踩雷筆記

Todd Quackenbush Facebook Messenger Platform 最主要的還是看文件, 偶而需要使用 Facebook Search 功能, 可以看到神秘版的文件。 1. 粉絲頁 與 App 的關係 在開發 Bot 之前, 需要做幾件事情 申請 facebook app 申請 facebook fans-page 規劃流程, 最好有一個懂 UX 的開發者 參與, 不然流程會很 XD 一個 bot 需要一個處理各種 events 的 callback, 一個 app 只能設定一個 callback, 所以 bot 跟 app 是 1 對 1 的關係, 一個 page 可以被最多 10 個 app 訂閱, 所以 page 跟 app 是 1 對 n(n 的關係, 這會有什麼影響呢? 在多人協作開發的時候, 你的好 partner 會送很多 event 給你, 禮尚往來, 你也會送一些 event 給他, 有幾個方式 從原生 App 申請一組開發 app 出來, webhook 只設定自己開發要用的 event 額外申請一個自己專用的 Fans-Page, 來 subscribed 2. GetStartButton Not Work 這邊的雷是因為, 自己開發的時候只 hook 了 messages 這個 event, 後來開發的時候, 就乾脆都拿, 之後再看用了哪些功能在慢慢推給 Facebook 審核 check GetStartButton setting curl -X GET "https://graph.facebook.com/v2.10/me/messenger_profile?fields=get_started&access_token= " check webhook messaging_postbacks event check your callback rsources is exist.

JMH 筆記

Nolan Issac 使用 JMH 进行微基准测试:不要猜,要测试! 這幾天讀了幾篇很有趣的文章, 是關於 lambda 跟 jvm 效能評估的文章, 分別是 Java8 Lambda表达式和流操作如何让你的代码变慢5倍 使用JMH进行微基准测试:不要猜,要测试! JMH JMH is a Java harness for building, running, and analysing nano/micro/milli/macro benchmarks written in Java and other languages targetting the JVM. JMH 是用來衡量 JVM 容器上運作的 (Java, Scala, Kotlin, Groovy, Clojure, etc.) 效能的工具, 官方建議透過 maven 來建立測試專案, 可以避免一些奇怪設定影響效能的問題, groupdId 就替換成自己的 package name 吧, artifactId 就替換成測試的 project name, 會按照這個 project name 在當下路徑建立一個資料夾, mvn archetype:generate \ -DinteractiveMode=false \ -DarchetypeGroupId=org.openjdk.jmh \ -DarchetypeArtifactId=jmh-java-benchmark-archetype \ -DgroupId=com.example \ -DartifactId=jmh-benchmark \ -Dversion=1.0 maven Builde 出來的 code import org.openjdk.jmh.annotations.Benchmark; public class MyBenchmark { @Benchmark public void testMethod() { // This is a demo/sample template for building your JMH ben...