其中filterChainProxy就是由web.xml声明的filter(FilterToBeanProxy)的targetClass。它主要是装载filterInvocationDefinitionSource指定的filter类(例子中为authenticationProcessingFilter,exceptionTranslationFilter),并顺序调用它们的doFilter方法,进行安全服务处理。
而authenticationProcessingFilter是处理一个认证表单,登陆用的表单必须提交用户名和密码这两个参数给这个filter.由用户名和密码构造一个UsernamePasswordAuthenticationToken,将传给AuthenticationManager的authenticate方法进行认证处理。该filter默认处理filterProcessesUrl属性指定的URL,认证失败会转到authenticationFailureUrl,认证成功会转到defaultTargetUrl页面。
AuthenticationManager顾名思义认证管理器,它只有一个接口方法authenticate用于返回认证结果,他的实现类由多个AuthenticationProvider进行投票,决定认证是否通过。
daoAuthenticationProvider是检验用户录入的认证数据是否正确(说白了就是用户名和密码是否正确)
inMemoryDaoImpl是给daoAuthenticationProvider提供系统的用户资料。而资料的来源是从配置中装载到内存的。
当认证不通过时,AuthenticationManager的实现类AbstractAuthenticationManager会抛出AuthenticationException类型的异常。这时排在最后的exceptionTranslationFilter会捕获该异常,并转向authenticationEntryPoint。
4.在WebRoot下创建index.jsp(其实不要也没关系,主要是为了方便),直接转向用户资料显示页。内容如下
<% @ page language = " java " pageEncoding = " UTF-8 " %>
<! DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.0 Transitional//EN " >
< html >
< head >
<!--
< META HTTP - EQUIV = " Refresh " CONTENT = " 0;URL=user!list.rgb " >
-->
< META HTTP - EQUIV = " Refresh " CONTENT = " 0;URL=userinfo.jsp " >
</ head >
< body >
< p > Loading
</ p >
</ body >
</ html >
5.在WebRoot下创建userinfo.jsp,用于显示当前登陆的用户信息。内容如下
<% @ page language = " java " pageEncoding = " UTF-8 " %>
<% @ page import = " org.acegisecurity.context.SecurityContextHolder " %>
<% @ page import = " org.acegisecurity.userdetails.* " %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + " :// "
+ request.getServerName() + " : " + request.getServerPort()
+ path + " / " ;
%>
<! DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.01 Transitional//EN " >
< html >
< head >
< base href = " <%=basePath%> " >
< title > My JSP ' pass.jsp ' starting page </ title >
< meta http - equiv = " pragma " content = " no-cache " >
< meta http - equiv = " cache-control " content = " no-cache " >
< meta http - equiv = " expires " content = " 0 " >
< meta http - equiv = " keywords " content = " keyword1,keyword2,keyword3 " >
< meta http - equiv = " description " content = " This is my page " >
<!--
< link rel = " stylesheet " type = " text/css " href = " styles.css " >
-->
</ head >
< body >
当前用户:
<%
Object obj = SecurityContextHolder.getContext().getAuthentication();
if ( null != obj){
Object userDetail = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String username = "" ;
if (userDetail instanceof UserDetails) {
username = ((UserDetails) userDetail).getUsername();
} else {
username = userDetail.toString();
}
out.print(username);
out.print( " <br><a href=\ " j_acegi_logout\ " >注销</a> " );
} else {
out.print( " 当前没有有效的用户 " );
out.print( " <br><a href=\ " acegilogin.jsp\ " >登陆</a> " );
}
%>
</ body >
</ html >
6.在WebRoot下创建acegilogin.jsp
<% @ page language = " java " pageEncoding = " UTF-8 " %>
<% @ page import = " org.acegisecurity.ui.AbstractProcessingFilter " %>
<% @ page import = " org.acegisecurity.ui.webapp.AuthenticationProcessingFilter " %>
<% @ page import = " org.acegisecurity.AuthenticationException " %>
< html >
< head >
< title > Login </ title >
</ head >
< body >
< h1 > Login </ h1 >
< P > Valid users:
< P >
< P > username < b > liuyxit </ b > , password < b > 123 </ b > (supervisor)
< P > username < b > user1 </ b > , password < b > user1 </ b > (normal user)
< p > username < b > user2 </ b > , password < b > user2 </ b > (user disabled)
< p >
<%
String strError = request.getParameter( " login_error " );
if ( null != strError){
%>
< font color = " red " >
你的登陆失败,请重试。 < BR >< BR >
原因: <%= ((AuthenticationException) session.getAttribute(AbstractProcessingFilter.ACEGI_SECURITY_LAST_EXCEPTION_KEY)).getMessage() %>
</ font >
<%
} // end if
%>
< form action = " j_acegi_security_check " method = " POST " >
< table >
< tr >< td > User: </ td >< td >< input type = ' text ' name = ' j_username ' value = ' <%= session.getAttribute(AuthenticationProcessingFilter.ACEGI_SECURITY_LAST_USERNAME_KEY) %> ' ></ td ></ tr >
< tr >< td > Password: </ td >< td >< input type = ' password ' name = ' j_password ' ></ td ></ tr >
< tr >< td >< input type = " checkbox " name = " _acegi_security_remember_me " ></ td >< td > 2周内自动登录 </ td ></ tr >
< tr >< td colspan = ' 2 ' >< input name = " submit " type = " submit " ></ td ></ tr >
< tr >< td colspan = ' 2 ' >< input name = " reset " type = " reset " ></ td ></ tr >
</ table >
</ form >
</ body >
</ html >
上一页 [1] [2] [3] [4] 下一页

【责编:Peng】