Petstore_home/src/components/signon/src/com/sun/j2ee/bluePRints/signon/web/SignOnFilter.java它在初始化时会先读取
。
先看SignOnFilter初始动作,约在87列:
public void init(FilterConfig config) throws ServletException {
this.config = config;
URL protectedResourcesURL = null;
try {
//謮取signon-config.xml
protectedResourcesURL =
config.getServletContext().getResource("/WEB-INF/signon-config.xml");
SignOnDAO dao = new SignOnDAO(protectedResourcesURL);
//读取登入失败画面(signon_error.screen)
signOnErrorPage = dao.getSignOnErrorPage();
//读取登入画面(signon.screen)
signOnPage = dao.getSignOnPage();
//读取所有欲保护画面,组成HashMap
protectedResources = dao.getProtectedResources();
} catch (java.net.MalformedURLException ex) {
System.err.println("SignonFilter: malformed URL exception: " + ex);
}
}
signon-config>
<!-- Form Sign On Page(登入画面)-->
<signon-form-login-page>
signon.screen
</signon-form-login-page>
<!-- Error Page When Sign On fails(登入失败画面)-->
<signon-form-error-page>
signon_error.screen
</signon-form-error-page>
<!-- A Protected Resource-->
<security-constraint>
<web-resource-collection>
<web-resource-name>Customer Screen</web-resource-name>
<url-pattern>customer.screen</url-pattern>
</web-resource-collection>
</security-constraint>
<!-- A Protected Resource(本例之保护画面)-->
<security-constraint>
<web-resource-collection>
<web-resource-name>Customer Action</web-resource-name>
<url-pattern>customer.do</url-pattern>
</web-resource-collection>
</security-constraint>
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest hreq = (HttpServletRequest)request;
String currentURI = hreq.getRequestURL().toString();
String currentURL = hreq.getRequestURI();
// get everything after the context root
int firstSlash = currentURL.indexOf("/",1); // jump past the starting slash
String targetURL = null;
//取得使用者欲前往之URL,以本例来说,即是customer.do
if (firstSlash != -1) targetURL = currentURL.substring(firstSlash + 1,
currentURL.length());
//判断使用者从登入画面(signon.screen)进行验证工作
if ((targetURL != null) && targetURL.equals(FORM_SIGNON_URL)) {
validateSignOn(request, response, chain);
// jump out of this method
return;
}
// check if the user is signed on
//检查使用者是否登入过,从session取出登入标记,作为判断之用
boolean signedOn = false;
if (hreq.getSession().getAttribute(SIGNED_ON_USER) != null) {
signedOn
=((Boolean)hreq.getSession().getAttribute(SIGNED_ON_USER)).booleanValue();
} else {
hreq.getSession().setAttribute(SIGNED_ON_USER, new Boolean(false));
}
// jump to the resource if signed on
//若已登入过,则结束此Filter工作,进入Filter chain,以本例来说,它为Filter chain中最后一个Filter,所以就是不做任何事,让使用者进入他的目的画面
if (signedOn) {
chain.doFilter(request,response);
return;
}
// find out if the patterns match the target URL
//将使用者欲前往之URL与所有保护画面URL做比对,若符合则导入登入画面
(signon.screen)
Iterator it = protectedResources.keySet().iterator();
while (it.hasNext()) {
String protectedName = (String)it.next();
ProtectedResource resource =
(ProtectedResource)protectedResources.get(protectedName);
String urlPattern = resource.getURLPattern();
// now check agains the targetURL
//若符合则将目的URL存入Session,并转导至登入画面,结束Filter工作
if (urlPattern.equals(targetURL)) {
// put the orginal url in the session so others can access
hreq.getSession().setAttribute(ORIGINAL_URL, targetURL);
config.getServletContext().getRequestDispatcher("/" +
signOnPage).forward(request, response);
// Jump out of the filter and go to the next page
return;
}
}
// No matches if we made it to here
chain.doFilter(request,response);
}
public void init(FilterConfig config) throws ServletException {接着请重新编译及部署新的程序代码,在命令模式下:切换至Petstore_home/ src/webservices/apps/petstore/src目录如:cd D:/petstore1.3.1/src/webservices/apps/petstore/src
this.config = config;
URL protectedResourcesURL = null;
try {
//謮取signon-config.xml
protectedResourcesURL =
config.getServletContext().getResource("/WEB-INF/signon-config.xml");
SignOnDAO dao = new SignOnDAO(protectedResourcesURL);
//读取登入失败画面(signon_error.screen)
signOnErrorPage = dao.getSignOnErrorPage();
//读取登入画面(signon.screen)
signOnPage = dao.getSignOnPage();
//请加入侦察程序代码
System.out.println("signOnPage="+signOnPage);
System.out.println("signErrorPage="+signOnErrorPage);
//读取所有欲保护画面,组成HashMap
protectedResources = dao.getProtectedResources();
} catch (java.net.MalformedURLException ex) {
System.err.println("SignonFilter: malformed URL exception: " + ex);
}
}
doFilter()亦加入侦察程序代码:
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest hreq = (HttpServletRequest)request;
String currentURI = hreq.getRequestURL().toString();
String currentURL = hreq.getRequestURI();
// get everything after the context root
int firstSlash = currentURL.indexOf("/",1); // jump past the starting slash
String targetURL = null;
//取得使用者欲前往之URL,以本例来说,即是customer.do
if (firstSlash != -1) targetURL = currentURL.substring(firstSlash + 1,
currentURL.length());
//请加入侦察程序代码
System.out.println("targetURL="+targetURL);
//判断使用者从登入画面(signon.screen)进行验证工作
if ((targetURL != null) && targetURL.equals(FORM_SIGNON_URL)) {
validateSignOn(request, response, chain);
// jump out of this method
return;
}
// check if the user is signed on
//检查使用者是否登入过,从Session取出登入标记,作为判断之用
boolean signedOn = false;
if (hreq.getSession().getAttribute(SIGNED_ON_USER) !
= null) {
signedOn
=((Boolean)hreq.getSession().getAttribute(SIGNED_ON_USER)).booleanValue();
} else {
hreq.getSession().setAttribute(SIGNED_ON_USER, new Boolean(false));
}
// jump to the resource if signed on
//若已登入过,则结束此Filter工作,进入Filter chain,以本例来说,它为
Filter chain中最后一个Filter,所以就是不做任何事,让使用者进入他的目的画面
if (signedOn) {
chain.doFilter(request,response);
return;
}
// find out if the patterns match the target URL
//将使用者欲前往之URL与所有保护画面URL做比对,若符合则导入登入画面
(signon.screen)
Iterator it = protectedResources.keySet().iterator();
while (it.hasNext()) {
String protectedName = (String)it.next();
ProtectedResource resource =
(ProtectedResource)protectedResources.get(protectedName);
String urlPattern = resource.getURLPattern();
// now check agains the targetURL
//若符合则将目的URL存入Session,并转导至登入画面,结束Filter工作
if (urlPattern.equals(targetURL)) {
//请加入侦察程序代码
System.out.println("URL Matched! urlPattern="+urlPattern);
// put the orginal url in the session so others can access
hreq.getSession().setAttribute(ORIGINAL_URL, targetURL);
config.getServletContext().getRequestDispatcher("/" +
signOnPage).forward(request, response);
// Jump out of the filter and go to the next page
return;
}
}
// No matches if we made it to here
chain.doFilter(request,response);
}
(出处:http://www.VeVb.com)
新闻热点
疑难解答