acegi security实践教程—定制userDetailsService

前面我们都是使用默认的UserDetailsService,无论是使用InMemoryDaoImpl还是JdbcDaoImpl这种形式。那这篇文章给大家讲解如何自定义userDetailsService,正如咱们前面写过自己的logoutFilter类。

源码讲解

UserDetailsService是个对用户信息操作的接口,其中只有一个方法UserDetails loadUserByUsername(String username),若自定义userDetailsService则需要实现acegi中的userDetailsService接口,实现此方法即可。
  package org.acegisecurity.userdetails;
  import org.springframework.dao.DataAccessException;
  public abstract interface UserDetailsService
 {
    public abstract UserDetails loadUserByUsername(String paramString)
    throws UsernameNotFoundException, DataAccessException;
  }


另外,此方法返回的UserDetails也是接口,acegi中实现其接口的类是User,所以若自定义返回结果,也须实现acegi中的UserDetails接口。
package org.acegisecurity.userdetails;
import java.io.Serializable;
import org.acegisecurity.GrantedAuthority;
public abstract interface UserDetails extends Serializable
{
  public abstract GrantedAuthority[] getAuthorities();
  public abstract String getPassword();
  public abstract String getUsername();
  public abstract boolean isAccountNonExpired();
  public abstract boolean isAccountNonLocked();
  public abstract boolean isCredentialsNonExpired();
  public abstract boolean isEnabled();
}

开发步骤:

开发环境:

MyEclispe10.7.1+tomcat6.0.37+acegi1.0.5+spring2.0+oracle10g+dbcp数据源

项目目录如下:

其中readme主要用来记录本次验证目的
acegi security实践教程—定制userDetailsService_第1张图片

代码关键:

jdbcTemplate.queryForList返回的map类型的List,其中map的key值默认是数据库列名。
实现UserDetail中的GrantedAuthority[] authorities 是个接口形式,主要存放权限信息。获取的list对象转化成数组对象如下:
for(int i=0;i

另外注意:实现UserDetail类中方法,默认为false,根据实际情况而定,若不做处理,可以设置为true。
   public boolean isAccountNonExpired() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isAccountNonLocked() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isCredentialsNonExpired() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isEnabled() {
// TODO Auto-generated method stub
if("1".equals(enabled)){
return true;
}else{
return false;
}
}

定制类:

package com.extend;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.GrantedAuthorityImpl;
import org.acegisecurity.userdetails.User;
import org.acegisecurity.userdetails.UserDetails;
import org.acegisecurity.userdetails.UserDetailsService;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

public class MyUserDetailService implements UserDetailsService {

	 private JdbcTemplate jdbcTemplate;
	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {
		//根据用户名查询用户基本信息
		String baseSql="select * from test_user t where t.user_name=?";
		List list=this.jdbcTemplate.queryForList(baseSql,new Object[]{username});
		if(list.size()==0){
			throw new UsernameNotFoundException("User not Found");
		}
		Map pMap=(Map)list.get(0);
		MyUser myUser=new MyUser();
		myUser.setUsername((String)pMap.get("USER_NAME"));
		myUser.setPassword((String)pMap.get("PWD"));
		myUser.setEnabled((String)pMap.get("ENABLED"));
		//根据用户名查询用户权限信息
		String authSql="select AUTHS from test_auths t where t.user_name=?";
		List dbAuths=this.jdbcTemplate.queryForList(authSql,new Object[]{username});
		if(dbAuths.size()==0){
			throw new UsernameNotFoundException("User has no GrantAuthority");
		}
		List listAuth=new ArrayList();
		for(int i=0;i
package com.extend;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.GrantedAuthorityImpl;
import org.acegisecurity.userdetails.User;
import org.acegisecurity.userdetails.UserDetails;
import org.acegisecurity.userdetails.UserDetailsService;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

public class MyUserDetailService implements UserDetailsService {

	 private JdbcTemplate jdbcTemplate;
	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {
		//根据用户名查询用户基本信息
		String baseSql="select * from test_user t where t.user_name=?";
		List list=this.jdbcTemplate.queryForList(baseSql,new Object[]{username});
		if(list.size()==0){
			throw new UsernameNotFoundException("User not Found");
		}
		Map pMap=(Map)list.get(0);
		MyUser myUser=new MyUser();
		myUser.setUsername((String)pMap.get("USER_NAME"));
		myUser.setPassword((String)pMap.get("PWD"));
		myUser.setEnabled((String)pMap.get("ENABLED"));
		//根据用户名查询用户权限信息
		String authSql="select AUTHS from test_auths t where t.user_name=?";
		List dbAuths=this.jdbcTemplate.queryForList(authSql,new Object[]{username});
		if(dbAuths.size()==0){
			throw new UsernameNotFoundException("User has no GrantAuthority");
		}
		List listAuth=new ArrayList();
		for(int i=0;i

acegi配置文件:

>    
	
		
	

    
	
		 
	
	
	   
	
	
                
	               
	               
	               
	               
    

debug流程:

acegi security实践教程—定制userDetailsService_第2张图片
acegi security实践教程—定制userDetailsService_第3张图片
acegi security实践教程—定制userDetailsService_第4张图片
acegi security实践教程—定制userDetailsService_第5张图片
acegi security实践教程—定制userDetailsService_第6张图片
acegi security实践教程—定制userDetailsService_第7张图片
上述带领大家进入debug调试,是为了通过分析源码进一步了解acegi的调用流程。

项目下载:

你可能感兴趣的:(acegi security实践教程—定制userDetailsService)