亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

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

JAAS 實現in Struts Web App,使用XMLPolicy文件,不改變VM安全配置(1)

2019-11-17 06:26:27
字體:
來源:轉載
供稿:網友

  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 jrelibext directory
(i.e. D:JDK1.3jrelibext).

2) Add the following to the end of the java.security file located in JDK's
jrelibsecurity directory (i.e. D:JDK1.3jrelibsecurity):
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" %>
<logic:redirect forward="index"/>
<%-- welcome.jsp
Redirect 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"%>

<html:html>
<Title>Logon</Title>
<body>
<html:form action="/LoginAction.do">
        <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>


struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>
<!-- ================================================ Form Bean Definitions -->

    <form-beans>        
          <!--2 Login formbean-->    
        <form-bean
            name="LoginForm"
            type="com.nova.colimas.web.form.LoginForm"/>
    </form-beans>
     <global-forwards>
        <!-- Default forward to "Welcome" action -->
        <!-- Demonstrates using index.jsp to forward -->
        <forward
            name="index"
            path="/index.do"/>
    </global-forwards>
<!-- =========================================== Action Mapping Definitions -->
    <action-mappings>
            <!-- Default "Welcome" action -->
            <!-- Forwards to Welcome.jsp -->
        <action  path="/index"
                type="com.nova.colimas.web.action.StartupServlet">
            <forward name="sUCcess" path="/pages/index.jsp"/>  
        </action>
         <!-- 2 Login -->
          <action    path="/LoginAction"
              type="com.nova.colimas.web.action.LoginAction"
              name="LoginForm"
              scope="request"
              input="/pages/indexcon.jsp"
              validate="true">
                <forward name="success" path="/pages/index.jsp"/>
                <forward name="failure" path="/pages/index.jsp"/>
         </action>
    </action-mappings>
</struts-config>


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 - Java - 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 - 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) {

                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 <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[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 password
                                tmpPassword = new char[0];
                        }
                        password = new char[tmpPassword.length];
                        System.arraycopy(tmpPassword, 0, password, 0, tmpPassword.length);
                        ((PasswordCallback) callbacks[1]).clearPassword();

                } catch (java.io.IOException e) {
                        throw new LoginException(e.getMessage());
                } catch (UnsupportedCallbackException e) {
                        throw new LoginException("Error: " + e.getMessage());
                }

                if(debug) {
                        //System.out.println("ColimasLoginModule: userid = " + username);
                        String pwd = new String(password);
                        System.out.println("ColimasLoginModule: password = " + pwd);
                }

                // Check the userid and password
                if (isValidUser(username, password)) {
                        // authentication succeeded
                        if(debug)
                                System.out.println("ColimasLoginModule: authentication succeeded");
                        succeeded = true;
                        return true;
                } else {
                        // authentication failed
                        if(debug)
                                System.out.println("ColimasLoginModule: authentication failed");
                        succeeded = false;
                        // clear the values
                        username = null;
                        password = null;
                        throw new FailedLoginException("Invalid userid or password");
                }
                

        }


        /**
         * This method is called if the LoginContext's overall authentication
         * succeeded (the relevant REQUIRED, REQUISITE, SUFFICIENT and
         * OPTIONAL LoginModules succeeded).
         * <p>
         * If this LoginModule's own authentication attempt succeeded
         * (checked by retrieving the private state saved by the
         * <code>login</code> method), then this method associates the
         * relevant <code>PrincipAlexamples</code> with the
         * <code>Subject</code> located in the <code>LoginModule</code>.
         * If this LoginModule's own authentication attempted failed, then
         * this method removes any state that was originally saved.
         *
         * @exception LoginException if the commit fails.
         *
         * @return true if this LoginModule's own login and commit attempts
         * succeeded, or false otherwise.
         */
        public boolean commit() throws LoginException {

                if(succeeded == false)
                        return false;

                subject.getPrincipals().add(new PrincipalUser(username));
                subject.getPrincipals().addAll(getUserRoles(username));
                
                username = null;
                password = null;

                commitSucceeded = true;
                return true;
        }


        /**
         * This method is called if the LoginContext's overall
         * authentication failed. (the relevant REQUIRED, REQUISITE,
         * SUFFICIENT and OPTIONAL LoginModules did not succeed).
         * <p>
         * If this LoginModule's own authentication attempt succeeded
         * (checked by retrieving the private state saved by the
         * <code>login</code> and <code>commit</code> methods),
         * then this method cleans up any state that was originally
         * saved.
         *
         * @exception LoginException if the abort fails.
         *
         * @return false if this LoginModule's own login and/or commit attempts
         * failed, and true otherwise.
         */
        public boolean abort() throws LoginException {

                if (succeeded == false)
                        return false;
                                                
                if(succeeded == true && commitSucceeded == false) {
                        // login succeeded but overall authentication failed
                        succeeded = false;
                        username = null;
                        password = null;
                } else {
                        // overall authentication succeeded and commit
                        // succeeded, but someone else's commit failed.
                        logout();
                }
                
                return true;
        }


        /**
         * Logouts a Subject.
         * <p>
         * This method removes the <code>PrincipalExample</code>
         * instances that were added by the <code>commit</code>
         * method.
         *
         * @exception LoginException if the logout fails.
         *
         * @return true if this method succeeded, or false if this
         * LoginModule should be ignored.
         */
        public boolean logout() throws LoginException {

                subject.getPrincipals().clear();
                succeeded = false;
                succeeded = commitSucceeded;
                username = null;
                password = null;
                
                return true;
        }

        
        /**
         * Searches the users XML file for the specified
         * userid and password.
         */
        private boolean isValidUser(String uid, char[] passwd) {
                try {
                        /*get userid and password from db */
                        String name="tyrone1979";
                        String password="197913";
                        if (uid.equals(name)){
                                if (password.equals(new String(password)))
                                        return true;
                        }

                } catch (Exception e) {
                        e.printStackTrace();
                        throw new RuntimeException(e.getMessage());
                }
                return false;
        }


        /**
         * Searches the user's group XML file and returns a
         * collection of PrincipalExamples for each group
         * a user is a member of.
         */
        private Collection getUserRoles(String username) {
                Collection collection =null;

                try {
                        /*
                                 get Roles from db
                                */
                        ArrayList roles=new ArrayList();
                        roles.add(new PrincipalUser("00001"));
                        collection=roles;
                } catch (Exception e) {
                        e.printStackTrace();
                        throw new RuntimeException(e.getMessage());
                }

                return collection;
        }

}


4. callback類

public class LoginCallbackHandler implements CallbackHandler {
        
        private String name=null;
        
        private String password=null;
        
        public LoginCallbackHandler(String name,String password){
                super();
                this.name=name;
                this.password=password;
                
        }
        /**
         * @see CallbackHandler#handle(Callback[])
         */
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                                        
                for (int i = 0; i < callbacks.length; i++) {
                        if (callbacks[i] instanceof TextOutputCallback) {
                                TextOutputCallback textOutputCallback = (TextOutputCallback) callbacks[i];
                                switch (textOutputCallback.getMessageType()) {
                                        case TextOutputCallback.INFORMATION :
                                                //System.out.println(textOutputCallback.getMessage());
                                                break;
                                        case TextOutputCallback.ERROR :
                                                //System.out.println("ERROR: " + textOutputCallback.getMessage());
                                                break;
                                        case TextOutputCallback.WARNING :
                                                //System.out.println("WARNING: " + textOutputCallback.getMessage());
                                                break;
                                        default :
                                                throw new IOException("Invalid message type: " + textOutputCallback.getMessageType());
                                }
                        } else if(callbacks[i] instanceof NameCallback) {
                                        // prompt the user for a userid
                                        NameCallback nc = (NameCallback) callbacks[i];
                                        nc.setName(this.name);
                        } else if(callbacks[i] instanceof PasswordCallback) {
                                        // prompt the user for the password
                                        PasswordCallback pc = (PasswordCallback) callbacks[i];

                                        pc.setPassword(this.password.toCharArray());
                        } else {
                                throw new UnsupportedCallbackException(callbacks[i], "Invalid Callback");
                        }
                }                                                
        }
}


5.com.nova.colimas.web.action.LoginAction類,實現認證

public class LoginAction extends Action {
        
        LoginContext loginContext=null;
        LoginForm loginForm=null;
        public ActionForward execute(ActionMapping mapping,
                         ActionForm form,
                         HttpServletRequest request,
                         HttpServletResponse response)
        throws Exception{
                
                /**
                 * 1 get Login form Bean
                 * 2 get the value
                 * 3 call JAAS Login Module
                 */
                try {                
                        loginForm=(LoginForm)form;
                        loginContext=new LoginContext(JAASConstants.AUTH_SECURITY_MODULENAME, new LoginCallbackHandler(loginForm.getUserID(),loginForm.getPassword()));
                        
                }catch(SecurityException e){
                        e.printStackTrace();
                } catch (LoginException e) {
                        e.printStackTrace();
                        //System.exit(-1);
                }
                // Authenticate the user
                try {
                        loginContext.login();//先運行ColimasLoginModule的initialize(Subject, CallbackHandler, Map, Map)方法,然后運行ColimasLoginModule的login()
                        System.out.println("Creating a new UserProfile...");                                        
                        System.out.println("Successfully!");        
                        
                } catch (Exception e) {
                        System.out.println("Unexpected Exception - unable to continue");
                        e.printStackTrace();
                        //System.exit(-1);
                        return mapping.findForward("failure");
                }                
      return mapping.findForward("success");
        }
}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
日韩成人在线网站| 亚洲第一精品久久忘忧草社区| 精品视频一区在线视频| 成人性生交大片免费看视频直播| 青草青草久热精品视频在线观看| 中日韩美女免费视频网站在线观看| 日韩电影免费在线观看| 亚洲jizzjizz日本少妇| 国产精品久久久久久av下载红粉| 欧洲成人在线视频| 欧美精品电影免费在线观看| 亚洲精品色婷婷福利天堂| 中文字幕av一区中文字幕天堂| 久久久久久久久亚洲| 亚洲自拍偷拍福利| 久久av在线看| 久久九九精品99国产精品| 一区二区欧美久久| 亚洲一区二区国产| 久久免费福利视频| 日韩精品在线观| 精品久久久久久久久久| 中文字幕国产亚洲2019| 国产精品亚洲视频在线观看| 国产精品免费久久久久影院| 51精品在线观看| 国产97在线|日韩| 亚洲人成毛片在线播放| 欧美片一区二区三区| 日本亚洲精品在线观看| 久久久www成人免费精品张筱雨| 欧美日韩一二三四五区| 亚洲精品久久7777777| 国产一区二区三区在线看| 不卡av在线网站| 热re99久久精品国产66热| 欧美日韩亚洲一区二区三区| 亚洲91精品在线观看| 欧美激情精品久久久久久变态| 亚洲欧美激情在线视频| 久久精品成人动漫| 亚洲自拍偷拍在线| 91精品综合视频| 国产91成人在在线播放| 欧美激情视频三区| 国内精品久久久久久中文字幕| 亚洲精品一区在线观看香蕉| 亚州成人av在线| 91tv亚洲精品香蕉国产一区7ujn| 中文字幕一区二区三区电影| 在线播放国产精品| 欧美黑人国产人伦爽爽爽| 国产日韩精品一区二区| 成人黄色免费片| 中文字幕亚洲无线码a| 国产精品色婷婷视频| 国产精品视频永久免费播放| 国产ts一区二区| 欧美性受xxxx白人性爽| 亚洲少妇中文在线| 456国产精品| 久久免费国产视频| 亚洲夜晚福利在线观看| 成人国产精品久久久久久亚洲| 亚洲国产日韩欧美综合久久| 久久久久北条麻妃免费看| 中文国产成人精品久久一| 国内免费久久久久久久久久久| 久久91亚洲精品中文字幕奶水| 日韩亚洲欧美中文在线| 国产成人精品久久| 国产精品主播视频| 欧美乱妇高清无乱码| 97成人精品区在线播放| 欧美丝袜第一区| 日韩av在线一区二区| 国产精品v片在线观看不卡| www欧美xxxx| 久久综合电影一区| 91中文字幕在线| 亚洲美女av黄| 亚洲欧美日本精品| 欧美日韩中文字幕在线视频| 日韩欧美aaa| 久久久国产91| 国产精品免费小视频| 成人黄色午夜影院| 91国产视频在线| 国产成人精品久久久| 欧日韩不卡在线视频| 欧美日韩亚洲视频| 欧美在线一区二区视频| 国产成人精品电影久久久| 国产精品成人一区| 亚洲欧洲国产精品| 亚洲欧美色图片| 久久国内精品一国内精品| 久久久久久久久网站| 欧美日韩国产一区二区| 中文字幕精品一区二区精品| 亚洲国产日韩欧美在线图片| 色哟哟入口国产精品| 亚洲日本欧美日韩高观看| 日韩一区在线视频| 久久精品99久久久久久久久| www.久久草.com| 亚洲成年人在线播放| 国产日本欧美一区二区三区| 亚洲欧洲日本专区| 97超级碰碰碰| 中文字幕亚洲综合久久| 国产精品免费看久久久香蕉| 色噜噜狠狠狠综合曰曰曰| 国产精品9999| 精品久久久久久国产| 国产免费亚洲高清| 国产精品成熟老女人| 亚洲伊人一本大道中文字幕| 欧美性猛交xxxx富婆弯腰| 久久久免费在线观看| 2020欧美日韩在线视频| 亚洲天天在线日亚洲洲精| 国产欧美精品一区二区三区-老狼| 91九色综合久久| 国产欧美最新羞羞视频在线观看| 日韩三级成人av网| 欧美极品欧美精品欧美视频| 伊人久久久久久久久久久| 精品亚洲永久免费精品| 欧美视频13p| 丝袜亚洲另类欧美重口| 这里只有精品在线观看| 亚洲天堂av网| 欧美一区亚洲一区| 亚洲a∨日韩av高清在线观看| 日韩免费观看在线观看| 欧美在线视频网站| www.日本久久久久com.| 欧美成人免费一级人片100| 成人亚洲综合色就1024| 98精品国产自产在线观看| 成人激情黄色网| 伊人激情综合网| 国产精品入口福利| 国产一区二区三区精品久久久| 欧美精品在线播放| 亚洲女人天堂成人av在线| 日韩中文字幕免费视频| 国产午夜精品视频免费不卡69堂| 日韩欧美国产成人| 26uuu日韩精品一区二区| 51精品国产黑色丝袜高跟鞋| 国产亚洲日本欧美韩国| 91国产在线精品| 欧美日韩国产黄| 国产精品嫩草视频| 午夜精品福利在线观看| 国产精品9999| 78色国产精品| 一本一道久久a久久精品逆3p| 国产日韩一区在线| 亚洲精品中文字幕女同| 中文字幕一区二区三区电影| 中文字幕日韩精品有码视频|