熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

用JAAS 實現in Struts Web App(一)

2022-06-13   來源: Java開源技術 

  JAAS參考資料中流行的文章是擴展JAAS實現類實例級授權
  
  但它是基於JDK與目前的JDKJDK不兼容例如其中的配置如下
  
  The following assumes you are using JDK and the files were extracted to
  the d:\JaasExample directory You will save some work by extracting the files
  to this directory otherwise you will have to modify the policy and the ResourceSecurityxml
  policy files with the correct path names
  
  ) Copy the jaasjar and the jaasmodjar to your JDK jre\lib\ext directory
  (ie D:\JDK\jre\lib\ext)
  
  ) Add the following to the end of the javasecurity file located in JDKs
  jre\lib\security directory (ie D:\JDK\jre\lib\security):
  authpolicyprovider=comibmresourcesecurityauthXMLPolicyFile
  
  ) Execute the runbat file
  
  以後為policyprovider=PolicyFile而且需要修改javasecurity文件
  
  我經過天的嘔血奮戰實現了不改變java VM環境和Web server環境在struts下實現JAAS
  
  步驟如下
  
   welcomejsp indexjsp strutsconfigxml
  
  <%@ taglib uri=/tags/strutslogic prefix=logic %>
  <logic:redirect forward=index/>
  <% welcomejspRedirect default requests to Welcome global ActionForwardBy using a redirect the useragent will change address to match the path of our Welcome ActionForward %>
  
  indexjsp
  
  <%@ page contentType=text/html; charset=UTF%>
  <%@ taglib uri=/tags/strutsbean prefix=bean%>
  <%@ taglib uri=/tags/strutshtml prefix=html%>
  <%@ taglib uri=/tags/strutslogic prefix=logic%>
  <html:html>
  <Title>Logon
  </Title>
  <body><html:form action=/LoginActiondo>
  <p>User ID:
  <input type=text name=userID value=tyrone />
  <br>
  Passord: <input type=password name=password value=password/>
  <br>
  <html:submit />
  </p>
  </html:form>
  </body>
  </html:html>
  
  strutsconfigxml
  
  <?xml version= encoding=ISO ?>
  <!DOCTYPE strutsconfig PUBLIC
  //Apache Software Foundation//DTD Struts Configuration //EN
  config__dtd>
  <strutsconfig>
  <! ================================================ Form Bean Definitions >
  <formbeans>
  <! Login formbean>
  <formbean
  name=LoginForm
  type=limaswebformLoginForm/>
  </formbeans>
  <globalforwards>
  <! Default forward to Welcome action >
  <! Demonstrates using indexjsp to forward >
  <forward
  name=index
  path=/indexdo/>
  </globalforwards><! =========================================== Action Mapping Definitions >  <actionmappings>
  <! Default Welcome action >
  <! Forwards to Welcomejsp >
  <action path=/index
  type=limaswebactionStartupServlet>
  <forward name=success path=/pages/indexjsp/>
  </action>
  <! Login >
  <action
  path=/LoginAction
  type=limaswebactionLoginAction
  name=LoginForm
  scope=request
  input=/pages/indexconjsp
  validate=true>
  <forward name=success path=/pages/indexjsp/>
  <forward name=failure path=/pages/indexjsp/>
  </action>
  </actionmappings></strutsconfig>
  
   實現limaswebactionStartupServlet用來初始化JAAS需要的系統屬性
  
  public class StartupServlet extends Action { public ActionForward execute(ActionMapping mapping
  ActionForm form
  HttpServletRequest request
  HttpServletResponse response)
  throws Exception{
  
  // Initialization of the log
  //LoggerFactorysetFactory(new EPricerLogFactory ());
  // (this Startup of Settings application);
  initJAAS();   return mappingfindForward(success);
  }  //初始化JAAS需要的系統屬性  private void initJAAS(){
  //set env variable  //用於認證JAASConstants接口內保存nfig文件地址
  SystemsetProperty(javasecnfigJAASConstantsAUTH_SECURITY_LOGINFILE);
  }
  }
  
  public interface JAASConstants {
  String AUTH_SECURITY_POLICYXMLFILE=D:\\MyProject\\colimas\\clmsweb\\colimas\\securitypolicyxml;
  String AUTH_SECURITY_LOGINFILE=D:\\MyProject\\colimas\\clmsweb\\colimas\\nfig;
  String AUTH_SECURITY_MODULENAME=ColimasLogin;}
  
  nfig文件內容
  
  ColimasLogin
  {
  limassecurityauthColimasLoginModule required debug=true;
  };
  
  實現ColimasLoginModule登錄模塊
  
  /*
  * Created on //
  *
  * TODO To change the template for this generated file go to
  * Window Preferences Java Code Style Code Templates
  */package limassecurityauth;import javautil*;
  import javaxsecurityauth*;import javaxsecurityauthcallback*;
  import javaxsecurityauthlogin*;
  import javaxsecurityauthspiLoginModule;//import javasecurity*;
  //import orgwcdomtraversal*;
  import orgwcdom*;//import orgapachexpath*;
  /**
  * @author tyrone
  *
  * TODO To change the template for this generated type comment go to
  * Window Preferences Java Code Style Code Templates
  */public class ColimasLoginModule implements LoginModule {  private Subject subject;
  private CallbackHandler callbackHandler;
  private boolean debug = false;
  private boolean succeeded = false;
  private boolean commitSucceeded = false;
  private String username;
  private char[] password;
  /**
  * Initializes the <code>LoginModule</code>
  *
  * @param subject the <code>Subject</code> to be authenticated
  *
  * @param callbackHandler a <code>CallbackHandler</code> for
  * prompting and retrieving the userid and password from the user *
  * @param sharedState shared <code>LoginModule</code> state
  *
  * @param options options specified in the login configuration
  * file for this <code>LoginModule</code>
  */  public void initialize(Subject subject CallbackHandler callbackHandler Map sharedState Map options)
  {
  thissubject = subject;
  thiscallbackHandler = callbackHandler;
  // initialize configuration options
  debug = trueequalsIgnoreCase((String) optionsget(debug));
  }
  /**
  * Prompts the user for a userid and password
  *
  * @return true if the authentication succeeded
  * or false if this LoginModule should be ignored
  *
  * @exception FailedLoginException if the authentication fails
  *
  * @exception LoginException if the <code>LoginModule<
  /code>
  * is unable to authenticate
  */  public boolean login() throws LoginException {
  if (callbackHandler == null)
  throw new LoginException(Error: CallbackHandler cannot be null);
  Callback[] callbacks = new Callback[];
  callbacks[] = new NameCallback(userid: );
  callbacks[] = new PasswordCallback(password: false);
  try {
  callbackHandlerhandle(callbacks);
  username = ((NameCallback) callbacks[])getName();
  char[] tmpPassword = ((PasswordCallback) callbacks[])getPassword();
  if (tmpPassword == null)
  {
  // treat a NULL password as an empty
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28702.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.