Chinaunix首页 | 论坛 | 博客
  • 博客访问: 446038
  • 博文数量: 750
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 4970
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-09 12:36
文章分类

全部博文(750)

文章存档

2011年(1)

2008年(749)

我的朋友
最近访客

分类:

2008-09-09 15:24:43


  JAAS参考资料中流行的文章是扩展JAAS实现类实例级授权
  
  但它是基于JDK1.3,与目前的JDK1.4,JDK1.5不兼容,例如其中的配置如下:
  
  The following assumes you are using JDK 1.3 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 ResourceSecurity.xml
  policy files with the correct path names.
  
  1) Copy the jaas.jar and the jaasmod.jar to your JDK jre\lib\ext directory
  (i.e. D:\JDK1.3\jre\lib\ext).
  
  2) Add the following to the end of the java.security file located in JDK's
  jre\lib\security directory (i.e. D:\JDK1.3\jre\lib\security):
  auth.policy.provider=com.ibm.resource.security.auth.XMLPolicyFile
  
  3) Execute the run.bat file.
  
  1.4以后为policy.provider=PolicyFile。而且需要修改java.security文件
  
  我经过2天的呕血奋战实现了不改变java VM环境和Web server环境,在struts下实现JAAS。
  
  步骤如下:
  
  1. welcome.jsp, index.jsp, struts-config.xml
  
  <%@ taglib uri="/tags/struts-logic" prefix="logic" %>
  
  <%-- welcome.jspRedirect default requests to Welcome global ActionForward.By using a redirect, the user-agent will change address to match the path of our Welcome ActionForward. --%>
  
  index.jsp
  
  <%@ page contentType="text/html; charset=UTF-8"%>
  <%@ taglib uri="/tags/struts-bean" prefix="bean"%>
  <%@ taglib uri="/tags/struts-html" prefix="html"%>
  <%@ taglib uri="/tags/struts-logic" prefix="logic"%>
  
  Logon<br>  
  
  

User ID:
  
  

  Passord:
  

  
  


  

  
  

  
  struts-config.xml
  
  
    "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
  "">
  
  
  
  
    name="LoginForm"
  type="com.nova.colimas.web.form.LoginForm"/>
  

  
  
  
    name="index"
  path="/index.do"/>
  
  
  
  
    type="com.nova.colimas.web.action.StartupServlet">
  
  
  
    path="/LoginAction"
  type="com.nova.colimas.web.action.LoginAction"
  name="LoginForm"
  scope="request"
  input="/pages/indexcon.jsp"
  validate="true">
  
  
  
  

  
  2. 实现com.nova.colimas.web.action.StartupServlet用来初始化JAAS需要的系统属性
  
  public class StartupServlet extends Action { public ActionForward execute(ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response)
  throws Exception{
  
  // Initialization of the log
  //LoggerFactory.setFactory(new EPricerLogFactory ());
  //Log.info (this, "Startup of Settings application");
  initJAAS();   return mapping.findForward("success");
  }  //初始化JAAS需要的系统属性  private void initJAAS(){
  //set env variable  //用于认证JAASConstants接口内保存login.config文件地址
  System.setProperty("java.security.auth.login.config",JAASConstants.AUTH_SECURITY_LOGINFILE);
  }
  }
  
  public interface JAASConstants {
  String AUTH_SECURITY_POLICYXMLFILE="D:\\MyProject\\colimas\\clms-web\\colimas\\security-policy.xml";
  String AUTH_SECURITY_LOGINFILE="D:\\MyProject\\colimas\\clms-web\\colimas\\login.config";
  String AUTH_SECURITY_MODULENAME="ColimasLogin";}
  
  Login.config文件内容:
  
  ColimasLogin
  {
  com.nova.colimas.security.auth.ColimasLoginModule required debug=true;
  };
  
  3.实现ColimasLoginModule登录模块
  
  /*
  * Created on 2005/07/01
  *
  * TODO To change the template for this generated file go to
  * Window - Preferences - - Code Style - Code Templates
  */package com.nova.colimas.security.auth;import java.util.*;
  import javax.security.auth.*;import javax.security.auth.callback.*;
  import javax.security.auth.login.*;
  import javax.security.auth.spi.LoginModule;//import java.security.*;
  //import org.w3c.dom.traversal.*;
  import org.w3c.dom.*;//import org.apache.xpath.*;
  /**
  * @author tyrone
  *
  * TODO To change the template for this generated type comment go to
  * Window - Preferences - - 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 LoginModule.
  *
  * @param subject the Subject to be authenticated.
  *
  * @param callbackHandler a CallbackHandler for
  * prompting and retrieving the userid and password from the user. *
  * @param sharedState shared LoginModule state.
  *
  * @param options options specified in the login configuration
  * file for this LoginModule.
  */  public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options)
  {
  this.subject = subject;
  this.callbackHandler = callbackHandler;
  // initialize configuration options
  debug = "true".equalsIgnoreCase((String) options.get("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 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[2];
  callbacks[0] = new NameCallback("userid: ");
  callbacks[1] = new PasswordCallback("password: ", false);
  try {
  callbackHandler.handle(callbacks);
  username = ((NameCallback) callbacks[0]).getName();
  char[] tmpPassword = ((PasswordCallback) callbacks[1]).getPassword();
  if (tmpPassword == null)
  {
  // treat a NULL password as an empty
【责编:admin】

--------------------next---------------------

阅读(225) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~