JAAS參考資料中流行的文章是擴展JAAS實現類實例級授權
但它是基於JDK
The following assumes you are using JDK
the d:\JaasExample directory
to this directory otherwise you will have to modify the policy and the ResourceSecurity
policy files with the correct path names
(i
jre\lib\security directory (i
auth
我經過
步驟如下
<%@ taglib uri=
<logic:redirect forward=
<%
index
<%@ page contentType=
<%@ taglib uri=
<%@ taglib uri=
<%@ taglib uri=
<html:html>
<Title>Logon
</Title>
<body><html:form action=
<p>User ID:
<input type=
<br>
Passord: <input type=
<br>
<html:submit />
</p>
</html:form>
</body>
</html:html>
struts
<?xml version=
<!DOCTYPE struts
<struts
<!
<form
<!
<form
name=
type=
</form
<global
<!
<!
<forward
name=
path=
</global
<!
<!
<action path=
type=
<forward name=
</action>
<!
<action
path=
type=
name=
scope=
input=
validate=
<forward name=
<forward name=
</action>
</action
public class StartupServlet extends Action { public ActionForward execute(ActionMapping mapping
ActionForm form
HttpServletRequest request
HttpServletResponse response)
throws Exception{
// Initialization of the log
//LoggerFactory
// (this
initJAAS(); return mapping
} //初始化JAAS需要的系統屬性 private void initJAAS(){
//set env variable //用於認證JAASConstants接口內保存nfig文件地址
System
}
}
public interface JAASConstants {
String AUTH_SECURITY_POLICYXMLFILE=
String AUTH_SECURITY_LOGINFILE=
String AUTH_SECURITY_MODULENAME=
nfig文件內容
ColimasLogin
{
limas
};
/*
* Created on
*
* TODO To change the template for this generated file go to
* Window
*/package limas
import javax
import javax
import javax
//import org
import org
/**
* @author tyrone
*
* TODO To change the template for this generated type comment go to
* Window
*/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
{
this
this
// initialize configuration options
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(
Callback[] callbacks = new Callback[
callbacks[
callbacks[
try {
callbackHandler
username = ((NameCallback) callbacks[
char[] tmpPassword = ((PasswordCallback) callbacks[
if (tmpPassword == null)
{
// treat a NULL password as an empty
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28702.html