java

位置:IT落伍者 >> java >> 浏览文章

用JAAS 实现in Struts Web App(一)


发布日期:2022年02月23日
 
用JAAS 实现in Struts Web App(一)

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 >

<actionpath=/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               

上一篇:JBOSS4 数据源配置大全(二)

下一篇:Myeclipse 6.0.1安装及其配置