Spring Acegi Security实例解析三

1、新建一个Web工程,将以下Jar包导进工程:acegi-security-1.0.3.jar、commons-codec-1.3.jar、commons-logging-1.0.4.jar、mysql-connector-java-5.0.3-bin.jar和spring.jar。

2、在MySql中执行以下SQL语句:

Drop TABLE IF EXISTS `test`.`student`;
Create TABLE `test`.`student` (
`name` varchar(40) NOT NULL,
`psw` varchar(10) NOT NULL,
`enabled` boolean
);
insert into student values("lanp","lanpiao",true);
insert into student values("ph","ph",true);
insert into student values("wxh","wxh",true);

Drop TABLE IF EXISTS `test`.`user_privileges`;
Create TABLE `test`.`user_privileges` (
`name` varchar(40) NOT NULL,
`privilege` varchar(40) NOT NULL
);
insert into user_privileges values("lanp","ROLE_PRESIDENT");
insert into user_privileges values("ph","ROLE_SUPERVISOR");
insert into user_privileges values("wxh","ROLE_USER");


3、web.xml配置信息如下:



	MyAcegiProj
	
	
		contextConfigLocation
		
			/WEB-INF/applicationContext-acegi-security.xml
		
	
	
	
	
		MyAcegiProjFilterChainProxy
		
			org.acegisecurity.util.FilterToBeanProxy
		
		
			targetClass
			
				org.acegisecurity.util.FilterChainProxy
			
		
	
	
		MyAcegiProjFilterChainProxy
		/*
	
	
	
	
		
			org.springframework.web.context.ContextLoaderListener
		
	
	
	
		index.jsp
	
	


4、applicationContext-acegi-security.xml配置文件的信息如下:


 
    
    
    
        
            com.mysql.jdbc.Driver
        
        
            jdbc:mysql://127.0.0.1:3306/test
        
        
            root
        
        
            157891
        
    
    
      
      
        
           
            CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
            PATTERN_TYPE_APACHE_ANT
             /** =authenticationProcessingFilter,logoutFilter,rememberMeProcessingFilter,exceptionTranslationFilter
         
      
       
    
   
    
    
        
        
        
        
    
    
    
    
        
        
        
    
    
        
        
      
    
    
        
       
    
    
    
       
      
         
              
              
         
      
   
    
   
   
      
         
            
            
         
      
      
    
                
        
    
     
	  	
	  	
	    	SELECT name, psw, enabled FROM student WHERE name=?
	    
	    
	    	SELECT name, privilege FROM user_privileges where name=?
	  	
	
	
    
    
        
            
                
                
            
        
        
            
                
            
        
        


5、index.jsp页面代码如下:

<%@ page language="java" pageEncoding="UTF-8"%>



    



Loading ...


6、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" %>

  
    Login Page
  
  
    

Login

<% String strError = request.getParameter("login_error"); if (null != strError){ %> 你的登陆失败,请重试。

原因: <%= ((AuthenticationException) session.getAttribute(AbstractProcessingFilter.ACEGI_SECURITY_LAST_EXCEPTION_KEY)).getMessage() %>
<% }//end if %>
User:
Password:
2周内自动登录


7、userinfo.jsp用户信息页面代码如下:

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="org.acegisecurity.context.SecurityContextHolder"%>
<%@ page import="org.acegisecurity.userdetails.*"%>



	
		User Info Page

		
		
		
		
		
	

	
		当前用户:
		<%
			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("
注销"); }else{ out.print("当前没有有效的用户"); out.print("
登陆"); } %>


OK,3Q!

 

 

 

 

 

 

 

 

其中借鉴了一些别人的东西

你可能感兴趣的:(IT海洋——Web)