首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Linux频道
中国IT教育
Google
首页 资讯动态 认证考试 新手入门 核心技术 高级技术 J2EE J2ME Java&XML 开源技术 其他技术 RSS订阅 论坛 专题
您现在的位置: 中国IT实验室 >> Java >> 考试认证 >> 考试心得 >> 正文

学习Acegi-认证(authentication)

其中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】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性…
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题…
 ·展现C#世界 C#程序设计专题…
 ·Java入门 Tomcat的配置技巧精华专题…
 ·Oracle RMAN物理备份技术详解…
 今日更新
 社区讨论
 博客论点
 频道精选
 Java 频道导航