[原创]JAAS 实现in Struts Web App,使用XMLPolicy文件,不改变VM安全配置(1)认证

文章指出JAAS参考资料中流行文章基于JDK1.3,与当前JDK版本不兼容。作者经努力在不改变java VM和Web server环境下,于struts下实现JAAS,详细介绍了实现步骤,包括配置相关JSP和XML文件、初始化系统属性、实现登录模块等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JAAS参考资料中流行的文章是扩展JAAS实现类实例级授权

但它是基于JDK1.3,与目前的JDK1.4JDK1.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文件,而且不使用VM的-Djava.security.manager就不能使用XMLPolicyFile。

我经过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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值