麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

用JAAS 實現in Struts Web App(二)

2019-11-18 12:54:39
字體:
來源:轉載
供稿:網友

  5. 實現xmlPolicyFile類。
  
  public class XMLPolicyFile extends Policy implements JAASConstants {
  PRivate Document doc = null;
  //private CodeSource noCertCodeSource=null;
  /*
  * constrUCtor
  * refresh()
  */  public XMLPolicyFile(){
  refresh();
  }  public PermissionCollection getPermissions(CodeSource arg0) {
  // TODO Auto-generated method stub
  return null;
  }
  /*
  * Creates a DOM tree document from the default XML file or
  * from the file specified by the system property,
  * <code>com.ibm.resource.security.auth.policy</code>. This
  * DOM tree document is then used by the
  * <code>getPermissions()</code> in searching for permissions.
  *
  * @see javax.security.auth.Policy#refresh()
  */  public void refresh() {
  FileInputStream fis = null;
  try {
  // Set up a DOM tree to query
  fis = new FileInputStream(AUTH_SECURITY_POLICYXMLFILE);
  InputSource in = new InputSource(fis);
  DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
  dfactory.setNamespaceAware(true);
  doc = dfactory.newDocumentBuilder().parse(in);
  } catch (Exception e) {
  e.printStackTrace();
  throw new RuntimeException(e.getMessage());
  } finally {
  if(fis != null) {
  try { fis.close(); } catch (IOException e) {}
  
  }
  }
  }  public PermissionCollection getPermissions(Subject subject,CodeSource codeSource) {
  ResourcePermissionCollection collection = new ResourcePermissionCollection();
  try {
  // Iterate through all of the subjects principals
  Iterator principalIterator = subject.getPrincipals().iterator();
  while(principalIterator.hasNext()){
  Principal principal = (Principal)principalIterator.next();
  // Set up the XPath string to retrieve all the relevant permissions
  // Sample xpath string: "/policy/grant[@codebase=/"sample_actions.jar/"]/principal[@classname=/"com.fonseca.security.SamplePrincipal/"][@name=/"testUser/"]/permission"
  StringBuffer xpath = new StringBuffer();
  xpath.append("/policy/grant/principal[@classname=/"");
  xpath.append(principal.getClass().getName());
  xpath.append("/"][@name=/"");
  xpath.append(principal.getName());
  xpath.append("/"]/permission");
  //System.out.println(xpath.toString());
  NodeIterator nodeIter = XPathAPI.selectNodeIterator(doc, xpath.toString());
  Node node = null;
  while( (node = nodeIter.nextNode()) != null ) {
  //here
  CodeSource codebase=getCodebase(node.getParentNode().getParentNode());
  if (codebase!=null codebase.implies(codeSource)){
  Permission permission = getPermission(node);
  collection.add(permission);
  }
  }
  }
  } catch (Exception e) {
  e.printStackTrace();
  throw new RuntimeException(e.getMessage());
  }
  if(collection != null)
  return collection;
  else {
  // If the permission is not found here then delegate it
  // to the standard java Policy class instance.
  Policy policy = Policy.getPolicy();
  return policy.getPermissions(codeSource);
  }
  }
  /**
  * Returns a Permission instance defined by the provided
  * permission Node attributes.
  */
  private Permission getPermission(Node node) throws Exception {
  NamedNodeMap map = node.getAttributes();
  Attr attrClassname = (Attr) map.getNamedItem("classname");
  Attr attrName = (Attr) map.getNamedItem("name");
  Attr attrActions = (Attr) map.getNamedItem("actions");
  Attr attrRelationship = (Attr) map.getNamedItem("relationship");
  if(attrClassname == null)
  throw new RuntimeException();
  Class[] types = null;
  Object[] args = null;
  // Check if the name is specified
  // if no name is specified then because
  // the types and the args variables above
  // are null the default constructor is used.
  if(attrName != null) {
  String name = attrName.getValue();
  // Check if actions are specified
  // then setup the array sizes accordingly
  if(attrActions != null) {
  String actions = attrActions.getValue();
  // Check if a relationship is specified
  // then setup the array sizes accordingly
  if(attrRelationship == null) {
  types = new Class[2];
  args = new Object[2];
  } else {
  types = new Class[3];
  args = new Object[3];
  String relationship = attrRelationship.getValue();
  types[2] = relationship.getClass();
  args[2] = relationship;
  }
  types[1] = actions.getClass();
  args[1] = actions;
  } else {
  
  types = new Class[1];
  args = new Object[1];
  
  }
  types[0] = name.getClass();
  args[0] = name;
  }   String classname = attrClassname.getValue();
  Class permissionClass = Class.forName(classname);
  Constructor constructor = permissionClass.getConstructor(types);
  return (Permission) constructor.newInstance(args);
  }
  /**
  * Returns a CodeSource object defined by the provided
  * grant Node attributes.
  */
  private java.security.CodeSource getCodebase(Node node) throws Exception {
  Certificate[] certs = null;
  URL location;
  if(node.getNodeName().equalsIgnoreCase("grant")) {
  NamedNodeMap map = node.getAttributes();
  Attr attrCodebase = (Attr) map.getNamedItem("codebase");
  if(attrCodebase != null) {
  String codebaseValue = attrCodebase.getValue();
  location = new URL(codebaseValue);
  return new CodeSource(location,certs);
  }
  }
  return null;
  }
  }
  
  6.繼續Principal類PrincipalUser
  public class PrincipalUser implements Principal {
  private String name;
  /**
  *
  * @param name the name for this principal.
  *
  * @exception InvalidParameterException if the <code>name</code>
  * is <code>null</code>.
  */  public PrincipalUser(String name) {
  if (name == null)
  throw new InvalidParameterException("name cannot be null");
  //search role of this name.
  this.name = name;
  }
  /**
  * Returns the name for this <code>PrincipalUser</code>.
  *
  * @return the name for this <code>PrincipalUser</code>
  */
  public String getName() {
  return name;
  }
  /**
  *
  */  public int hashCode() {
  return name.hashCode();
  }
  }
  
  7.繼續Permission和PermissionCollection類
  public class ResourcePermission extends Permission {
  static final public String OWNER_RELATIONSHIP = "OWNER";
  static private int READ  = 0x01;
  static private int WRITE  = 0x02;
  static private int EXECUTE = 0x04;
  static private int CREATE = 0x08;
  static private int DELETE = 0x10;
  static private int DEPLOY = 0x16;
  static private int CONFIRM = 0x24;
  static final public String READ_ACTION = "read";
  static final public String WRITE_ACTION  = "write";
  static final public String EXECUTE_ACTION = "execute";
  static final public String CREATE_ACTION = "create";
  static final public String DELETE_ACTION = "delete";
  static final public String DEPLOY_ACTION = "deploy";
  static final public String CONFIRM_ACTION = "confirm";
  protected int mask;  protected Resource resource;
  protected Subject subject;
  /**
  * Constructor for ResourcePermission
  */
  public ResourcePermission(String name, String actions, Resource resource, Subject subject) {
  super(name);
  this

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美一级小视频 | a网站在线 | 久久久久久久久久久高潮一区二区 | 亚洲精品无码不卡在线播放he | 免费毛片随便看 | 精品国产一区二区三区四区阿崩 | 欧美日韩在线免费观看 | 中文字幕综合在线观看 | 亚洲最新色 | 国产一级毛片在线看 | 久久91亚洲精品久久91综合 | 草久影视| 国产一级在线免费观看 | 一级免费黄色免费片 | 51国产偷自视频区视频小蝌蚪 | 91小视频在线观看免费版高清 | 久草网在线| 亚洲精中文字幕二区三区 | 久久国产精品99国产 | 精品久久久久久久久久久下田 | 午夜精品久久久久久毛片 | 久久精热 | a免费视频 | 91成人免费网站 | 一本在线高清码电影 | 欧美一级淫片免费播放口 | 久久久久久久久久久国产精品 | 日韩一级网站 | 亚洲极色 | av成人在线电影 | 一本色道久久综合亚洲精品图片 | 国产一区精品在线观看 | 日韩中文字幕一区二区三区 | 日本中文字幕高清 | 欧美日韩成人一区二区 | 福利免费在线观看 | 久久成人黄色 | 久久艹艹艹 | 国产91对白叫床清晰播放 | 中文字幕国产亚洲 | 成人免费毛片在线观看 |