applet security problem アップレットの安全

2005-09-18 00:05:31 | Weblog

Hi everyone, My question is applet capable of reading some files located in server where this applet is located without digital signature? I know it is not possible for applet to access client local files, but how about server ?
THANK YOU in advance!


RE:

The applet can access the server that it came from without any additional signing or other security tweaking.


thank you , but ...
Author: wolf2006 Sep 17, 2005 2:30 AM (reply 2 of 5)


thank you so much for your reply!
But I still got some seriours problems , here are some of my codes, my IDE is eclipse3.0 with a tomcat plugin, and I put a test.txt file into the same folder as the class file(what I want to do is read the contents in the test.txt file and print it in the applet), at last I run this machine as a server(address is http://192.168.1.3:8080"), and try to access the page from another machine.
public class TestApplet extends Applet{

StringBuffer sb ;
public void init() {
sb = new StringBuffer();
}
public void start() {
try {
FileReader fr = new FileReader(getCodeBase().toString()+"test.txt");
int c ;
while ((c=fr.read())!=-1){
sb.append(c);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
repaint();
}
public void paint(Graphics g){
g.drawString(sb.toString(),100,100);
}
}
and when I access the html page which embeds the class file of this applet , I got the error message below

java.security.AccessControlException: access denied (java.io.FilePermission http:192.168.1.3:8080someprojectclassestest.txt read)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkRead(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at TestApplet.start(TestApplet.java:23)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

P.S.
below is the HTML code I made :
<html>
<head>
</head>
<body>
<applet code="TestApplet" width="500" height="500">
</applet>


This example demonstrates TestApplet!!!



</body>
</html>

Re: thank you , but ...


That's because you're using a FileReader, which is used for reading off the local filesystem.

Use Resources.
http://java.sun.com/j2se/1.4.2/docs/guide/resources/index.html


Re: thank you , but ...
Author: wolf2006 Sep 17, 2005 7:09 AM (reply 4 of 5)


hi paulcw , Thank you so much for your great advice , it really helped. I did what you told me, and it worked!! thank you so much . But In fact , above is just a simple sample of What I want to do. My real objective is to develop a applet that use a package named JWNL which is a JAVA interface to access WordNet(a semantic English dictionary). I have suceede to develop a application , and now in order to make it possible for user to access it for web , I want to make the application an applet . And before useing JWNL, I have to initialize it at first, something like JWNL.initialize(propertiesStream), and of course the property files are all located in server. When I access mypage , I always got the AccessControlException , So I want to ask a question : Is there no way for me to use this package through Applet , Except I rewrite the core code of this package(that seems to be a quite big job)?Maybe this question should not be asked here , but if you have some similar experiences ... Thank you again for your great help!!


Re: thank you , but ...


I'm not familiar with that package. There's no reason offhand why you shouldn't be able to access something from the same server the applet comes from. So I'm wondering what you're actually trying to do instead. If you post the stack trace you're getting that might help. Or maybe you should ask other users of the package; perhaps there's a forum about it on the site that distributes it.


Thank you very much ,
I tried again to clear the problem , but failed...
What I did with JWNL package is just called initialize method, and below is my code
InputStream s = Class.forName("TestApplet").getResourceAsStream(URL);
JWNL.initialize(s);
and error occured in JWNL.initialize(s), stack trace shows :
net.didion.jwnl.JWNLException: Unable to install net.didion.jwnl.dictionary.FileBackedDictionary
.......
Caused by: java.security.AccessControlException: access denied (java.io.FilePermission c:\program files\wordnet\2.0\dict\adv.idx read)

at java.security.AccessControlContext.checkPermission(AccessControlContext.java:284)


at java.security.AccessController.checkPermission(AccessController.java:415)

at java.lang.SecurityManager.checkPermission(SecurityManager.java:554)

at java.lang.SecurityManager.checkRead(SecurityManager.java:899)

at java.io.RandomAccessFile.<init>(RandomAccessFile.java:216)

at net.didion.jwnl.princeton.file.PrincetonRandomAccessDictionaryFile.openFile(PrincetonRandomAccessDictionaryFile.java:76)


at net.didion.jwnl.dictionary.file.AbstractDictionaryFile.open(AbstractDictionaryFile.java:58)


at net.didion.jwnl.dictionary.file.DictionaryCatalog.open(DictionaryCatalog.java:46)


at net.didion.jwnl.dictionary.file.DictionaryCatalogSet.open(DictionaryCatalogSet.java:34)


at net.didion.jwnl.dictionary.file_manager.FileManagerImpl.<init>(FileManagerImpl.java:54)


at net.didion.jwnl.dictionary.file_manager.FileManagerImpl.create(FileManagerImpl.java:69)


at net.didion.jwnl.util.factory.AbstractValueParam.create(AbstractValueParam.java:32)

because I didn't do anything inside initialize() method and either didn't set anywhere in my code " c:\program files\wordnet\2.0\dict\adv.idx ", so I think maybe this is a bug in the core of this package make it not possible be used with applet, anyway I will do more research with this package.
Thank you so much for your great kindness !!

正規表現の実例

2005-09-15 22:20:34 | Weblog
◆正規表現入門
^ 行の先頭
$ 行の末尾
. 改行以外の任意の1文字
[] []でくくられた中にある任意の1文字
[^] []でくくられた中にない任意の1文字
* 直前の文字の0個以上の並び
+ 直前の文字の1個以上の並び
? 直前の文字が0個または1個
{a} 直前の文字のa個の並び
{a,} 直前の文字のa個以上の並び
{a,b} 直前の文字のa個以上、b個以下の並び
| 2者択一の演算子
 
◆正規表現の使用例
■行の先頭「^」
^java 行の先頭がjavaという文字で始まっている場合
■行の末尾「$」
java$ 行の末尾がjavaという文字で終わっている場合
■改行以外の任意の1文字「.」
java.. javaの後に改行以外の任意の文字が2つ続く場合
■[]でくくられた中にある任意の1文字「[]」
[a-z] 小文字のアルファベットの中の1文字
[A-Z] 大文字のアルファベットの中の1文字
[a-zA-Z] 小文字または大文字のアルファベットの中の1文字
[0-9] 数字の中の1文字
[0-9a-z] 数字または小文字のアルファベットの中の1文字
■[]でくくられた中にない任意の1文字「[^]」
[^a-z] 小文字のアルファベット以外の1文字
[^A-Z] 大文字のアルファベット以外の1文字
[^a-zA-Z] アルファベット以外の1文字
[^0-9] 数字以外の1文字
[^0-9a-z] 数字以外、小文字のアルファベット以外の1文字
■直前の文字の0個以上の並び「*」
J* 0個以上のJの繰り返し
.* 0個以上の任意の文字の繰り返し
J.*D JとDの間に0個以上の任意の文字の繰り返し
■直前の文字の1個以上の並び「+」
J+ 1個以上のJの繰り返し
.+ 1個以上の任意の文字の繰り返し
J.+D JとDの間に1個以上の任意の文字の繰り返し
■直前の文字が0個または1個「?」
JA? JまたはJA
■直前の文字のa個の並び「{a}」
J{2} JJ
J{3} JJJ
■直前の文字のa個以上の並び「{a,}」
J{3,} JJJ,JJJJ,JJJJJ,・・・(3回以上のJの並び)
■直前の文字のa個以上、b個以下の並び「{a,b}」
J{3,5} JJJまたはJJJJまたはJJJJJ
■2者択一の演算子「|」
J|A JまたはA
Java|Hello JavaまたはHello

JavaScriptの問題解決

2005-09-15 00:16:21 | Weblog
javascriptの問題、ユーザのクリックからJavaScriptでクリックした単語のStringを獲得して、それをアプレットに送信し、表示するっていう仕組み。最初は:クリック側のコードは:

(script language='JavaScript')(!--
function toSubWin( obj )
{
window.open('applet.html?'+obj.firstChild.toString(),'sw');
}
// --)(/script)
(HTML)
(HEAD)

ちなみにアプレットは次のようなもので
import java.awt.*;
import java.applet.*;

public class SimpleApplet extends Applet{

public String word1;

public void init(){
word1 = getParameter("word") ;
setBackground(Color.yellow);
setForeground(Color.white);
setFont(new Font("Serif", Font.BOLD, 20));
}

public void start(){
repaint();
}

public void paint(Graphics g){
g.drawString("あるかどうか"+word1+"この前", 100, 60);
}
}
(TITLE)(/TITLE)
(/HEAD)
(BODY)
(a onClick='toSubWin(this)')ここをクリック(/A)
(/BODY)
(/HTML)


クリックしたのを受け取る側のHTMLは
(html)
(body)
データ表示(br)
(script language="JavaScript")
(!-- str = location.search;
   str = str.substring(1,str.length);  
   document.write(str); // --)
(/script)
(applet code='SimpleApplet' name='app' width='500' height='500')
(script)
document.write("(param name='word' value=str)");
(/script)
(/body)
(/html)

しかし、これでは、どうしてもwordの値をアプレットに送信することが出来なかった。そこで、strを直接呼ぶではなく、頭の所で定義した関数を呼ぶことで獲得する
ことで問題解決した。
コードは次のように変えた:
(html)
(head)
(title)渡された値を表示する(/title)
(/head)
(body)
渡された値を表示する(br)
(script language="JavaScript")(!--

function getStr(){
str = location.search;
str = str.substring(1,str.length);
return str ;
}
// --)(/script)
あるかな?

(script language="JavaScript")
document.write("(applet code='SimpleApplet' name='app' width='500' height='500')");
document.write("(param name='word' value="+getStr()+")");
(/script)
(/body)
(/html)
ちなみに、アプレットは次のようなものだった
import java.awt.*;
import java.applet.*;

public class SimpleApplet extends Applet{

public String word1;

public void init(){
word1 = getParameter("word") ;
setBackground(Color.yellow);
setForeground(Color.white);
setFont(new Font("Serif", Font.BOLD, 20));
}

public void start(){
repaint();
}

public void paint(Graphics g){
g.drawString("あるかどうか"+word1+"この前", 100, 60);
}
}

ToolTipに関して

2005-09-03 23:48:38 | Weblog
コンポーネント単位で ToolTip を出すのはちょっと。。。その代わりにコンポーネントに絵を描いて、その絵の単位で ToolTip をだす方法を考えた。
例:
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComponent;

public class ToolTipDemo4 extends JApplet {
static class Drawer extends JComponent {
private Shape rectangle;
private Shape circle;

public Drawer() {
rectangle = new Rectangle2D.Double(10, 10, 50, 50);
circle = new Ellipse2D.Double(70, 70, 120, 120);
}

public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

// このメソッドをオーバライドする
public boolean contains(int x, int y) {
if(rectangle.contains(x, y)) {
// 長方形の内部にある場合
setToolTipText("<html>Rectangle</html>");
} else if (circle.contains(x, y)) {
// 円の内部にある場合
setToolTipText("<html>Circle</html>");
} else {
// どちらでもない場合は ToolTip をリセットする
setToolTipText(null);
}

// 最後にスーパクラスのメソッドをコールする
return super.contains(x, y);
}

public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;

g2d.setColor(Color.RED);
g2d.fill(rectangle);

g2d.setColor(Color.BLUE);
g2d.fill(circle);
}
}

public void init() {
getContentPane().add(new Drawer());
}
}


WordNetのJWNLJAVA インターフェスを使うときに

2005-08-27 15:56:54 | Weblog
wordnet JWNLを使うときに、
JWNL.initialize(new FileInputStream("file_properties.xml"));
で初期化する必要がある、このときに
file_properties.xmlを今のフォルダにコピーする必要がある。

JscrollPaneについて

2005-08-19 02:31:57 | Weblog
JTextArea ta = new JTextArea(10,10);
JScrollPane sc = new JScrollPane( ta ) ;
でJScrollPaneを作って、JTextAreaの変わりにcomponentに追加すればいい
          jp.add(sc);

Java GUIについて

2005-08-19 01:22:52 | Weblog
JComponent glass = (JComponent)jf.getGlassPane()
でglassPaneを獲得、
GlassPaneに置かれたcomponentは他のpaneに置かれたcomponentより必ず上に表示
それから、glass.add(...)で追加

正規表現

2005-07-29 18:43:51 | Weblog

まず、String str = matcher.group(); で正規表現にマッチした文字列を求めるときに、必ず、その前にmatcher.find();を使って、一回マッチすること

次に、定番のやつをいくつか覚えておこう:

  1. .  任意文字
  2. *  0回またはそれ以上マッチする
  3. +   一回またはそれ以上マッチする
  4. ?   0回または一回だけマッチする

実践編:

  1. <.*?>テキスト中の<>タグで囲まれたやつにマッチする。
  2. [wxy] w,x,またはyにマッチする。
  3. [^wxy] w,x,またはyを除いた任意文字にマッチする。

使えそうなテック:

  1. 例えば  
    absence : 4
    absolute : 2
    academic : 1
    を"absence"

        "absolute"

         "academic"に変換するときに、秀丸で使ったテックは、まず  : [1-9]を"にここでスペースはそのままスペースを空ければマッチする。

   次に、^を"に変換^は行の頭にマッチする。

   


設定上の注意

2005-06-22 23:23:08 | Weblog
classファイルの出力の設定をプロジェクト直下のclassesフォルダに設定したんで、WEB-INF/classesの中のクラスファイルが更新されないので、手動で更新する必要がある

javascriptからappletを制御

2005-06-21 01:09:58 | Weblog