import java.util.*;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.*;
import java.io.*;
import java.security.*;
import javax.swing.*;
class PermissionTest{
public static void main(String[] args){
System.setProperty("java.security.policy","PermissionTest.policy");
System.setSecurityManager(new
SecurityManager());
EventQueue.invokeLater(new
Runnable(){
public void run(){
JFrame
frame=new PermissionTestFrame();
//frame.setIconImage(Toolkit.getDefaultToolkit().createImage("images1/1.png"));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class PermissionTestFrame extends JFrame{
private JTextField textField;
private WordCheckTextArea textArea;
public PermissionTestFrame(){
this.setTitle("permissionTestFrame");
this.setSize(300,300);
textField=new
JTextField(20);
JPanel panel=new
JPanel();
panel.add(textField,BorderLayout.EAST);
JButton openButton=new
JButton("insert");
panel.add(openButton,BorderLayout.WEST);
openButton.addActionListener(new
ActionListener(){
public void
actionPerformed(ActionEvent event){
insertWords(textField.getText());
}
});
add(panel,BorderLayout.NORTH);
textArea=new
WordCheckTextArea();
add(new
JScrollPane(textArea),BorderLayout.CENTER);
}
public void insertWords(String words){
try{ textArea.append(words+"\n");
}catch(SecurityException
ex){
JOptionPane.showMessageDialog(this,"i
am sorry i cannot do that");
}
}
}
import java.security.Permission;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JTextArea;
public class WordCheckPermission extends Permission {
private String action;
public WordCheckPermission(String target,String anAction){
super(target);
action=anAction;
}
@Override
public boolean implies(Permission other) {
// TODO Auto-generated method stub
if(!(other instanceof WordCheckPermission))
return false;
WordCheckPermission
b=(WordCheckPermission)other;
if(action.equals("insert")){
System.out.print("h");
return
b.action.equals("insert") &&
getName().indexOf(b.getName())>=0;
}
else if(action.equals("avoid")){
if(b.action.equals("avoid"))
return b.badWordSet().containsAll(badWordSet());
else
if(b.action.equals("insert")){
for(String
badWord:badWordSet())
if(b.getName().indexOf(badWord)>=0)
return false;
return
true;
}
else return false;
}
else return false;
}
@Override
public boolean equals(Object other) {
// TODO Auto-generated method stub
if(other==null) return false;
if(!getClass().equals(other.getClass())) return
false;
WordCheckPermission b=(WordCheckPermission )
other;
if(!action.equals(b.action))return false;
if(action.equals("insert")) return
getName().equals(b.getName());
else if(action.equals("avoid")) return
badWordSet().equals(b.badWordSet());
else return false;
}
public Set badWordSet(){
Set set=new HashSet();
set.addAll(Arrays.asList(getName().split(",")));
return set;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return
getName().hashCode()+action.hashCode();
}
@Override
public String getActions() {
// TODO Auto-generated method stub
return action;
}
}
class WordCheckTextArea extends JTextArea{
public void append(String text){
WordCheckPermission p=new
WordCheckPermission(text,"insert");
SecurityManager
manager=System.getSecurityManager();
if(manager!=null) {
System.out.print("wo");
manager.checkPermission(p);
System.out.print("ss");
}
super.append(text);
}
}
权限文件PermissionTest.policy文件下的内容
grant
{
permission WordCheckPermission
"bad,wocao","avoid";
permission WordCheckPermission "Mary had a
little lamb","insert";
};