<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 37  文章 - 14  trackbacks - 0
    <2008年3月>
    2425262728291
    2345678
    9101112131415
    16171819202122
    23242526272829
    303112345

    常用鏈接

    留言簿

    隨筆分類

    隨筆檔案

    文章分類

    相關鏈接

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    Acegi預設的是通過JdbcDaoImpl訪問數(shù)據(jù)庫進行身份驗證,我們首先來看下配置文件
          <!-- 數(shù)據(jù)庫驗證身份采用內(nèi)存DAO -->
       
    <bean id="daoAuthenticationProvider" class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">
           
    <property name="userDetailsService">
           
    <ref bean="jdbcDaoImpl"/>
           
    </property>
           
    <property name="userCache">
           
    <ref bean="userCache"/>
           
    </property>
       
    </bean>
       
        
    <bean id="jdbcDaoImpl" class="org.acegisecurity.userdetails.jdbc.JdbcDaoImpl">
        
    <property name="dataSource">
        
    <ref bean="dataSource"/>
        
    </property>
        
    <property name="usersByUsernameQuery">
        
    <value>
        SELECT username,password,enabled FROM user WHERE username=?
        
    </value>
        
    </property>
        
    <property name="authoritiesByUsernameQuery">
        
    <value>            
        SELECT username, rolename
           FROM user u, role r, user_role ur
         WHERE u.id = ur.user_id
         and r.id = ur.role_id
         and u.username = ?
        
    </value>
        
    </property>
        
    </bean>
    從配置及源碼可以看出JdbcDaoImpl都是實現(xiàn)了UserDetailsService接口, 而這個接口里只定義了一個方法:UserDetails loadUserByUsername(String username) 就是根據(jù)用戶名加載UserDetails對象。要獲取更多的用戶信息可以通過擴展JdbcDaoImpl,重寫UsersByUsernameMapping方法來封裝UserDetails對象。首先需要擴展UserDetails接口, 并擴展org.acegisecurity.userdetails.User
    public interface IUserDetails extends UserDetails {

        
    public void setUsername_cn(String username_cn);
        
        
    public String getUsername_cn();
        
        
        
    public String getUsername();

        
    public void setUsername(String username);

        
    public GrantedAuthority[] getAuthorities();

        
    public void setAuthorities(GrantedAuthority[] authorities);

    }

    public class UserDetailsImpl extends User implements IUserDetails {
        
        
    private String username_cn;
        
    private String username;
        
    private GrantedAuthority[] authorities;
        
        
        
    public UserDetailsImpl(String username, String password, boolean enabled,
                
    boolean accountNonExpired, boolean credentialsNonExpired,
                
    boolean accountNonLocked, GrantedAuthority[] authorities)
                
    throws IllegalArgumentException {
            
    super(username, password, enabled, accountNonExpired, credentialsNonExpired,
                    accountNonLocked, authorities);
            setUsername(username);
            setAuthorities(authorities);        
        }
        
        
        
    public UserDetailsImpl(String username_cn, String username, String password, boolean enabled,
                
    boolean accountNonExpired, boolean credentialsNonExpired,
                
    boolean accountNonLocked, GrantedAuthority[] authorities)
                
    throws IllegalArgumentException {
            
    super(username, password, enabled, accountNonExpired, credentialsNonExpired,
                    accountNonLocked, authorities);
            
    this.username_cn = username_cn;
            setUsername(username);
            setAuthorities(authorities);    
        }


        
    public GrantedAuthority[] getAuthorities() {
            
    return authorities;
        }


        
    public void setAuthorities(GrantedAuthority[] authorities) {
            
    this.authorities = authorities;
        }


        
    public String getUsername() {
            
    return username;
        }


        
    public void setUsername(String username) {
            
    this.username = username;
        }


        
    public String getUsername_cn() {
            
    return username_cn;
        }


        
    public void setUsername_cn(String username_cn) {
            
    this.username_cn = username_cn;
        }

    }
    接著通過擴展JdbcDaoImpl,重寫IUserDetails loadUserByUsername(String username)返回我們所擴展的UserDetail對象
    public class AcegiJdbcDaoImpl extends JdbcDaoImpl {
    .

    public IUserDetails loadUserByUsername(String username)
           
    throws UsernameNotFoundException, DataAccessException {
           List users 
    = usersByNameMapping.execute(username);

           
    if (users.size() == 0) {
               
    throw new UsernameNotFoundException("User not found");
           }

           IUserDetails user 
    = (IUserDetails) users.get(0); // contains no GrantedAuthority[]
           
           List dbAuths 
    = rolesByUsernameMapping.execute(user.getUsername());

           addCustomAuthorities(user.getUsername(), dbAuths);

           
    if (dbAuths.size() == 0) {
               
    throw new UsernameNotFoundException("User has no GrantedAuthority");
           }

           GrantedAuthority[] arrayAuths 
    = (GrantedAuthority[]) dbAuths.toArray(new GrantedAuthority[dbAuths.size()]);

           user.setAuthorities(arrayAuths);

           
    if (!usernameBasedPrimaryKey) {
               user.setUsername(username);
           }

           
    return user;
       }
       
    protected class UsersByUsernameMapping extends MappingSqlQuery {
           
    protected UsersByUsernameMapping(DataSource ds) {
               
    super(ds, usersByUsernameQuery);
               declareParameter(
    new SqlParameter(Types.VARCHAR));
               compile();
           }

           
    protected Object mapRow(ResultSet rs, int rownum)
               
    throws SQLException {
               String username = rs.getString("username");
                  String username_cn = rs.getString("username_cn")
    ;
               String password 
    = rs.getString(3);
               
    boolean enabled = rs.getBoolean("enabled");
               IUserDetails user = new UserDetailsImpl(username, password, enabled, truetruetrue,
                       
    new GrantedAuthority[] {new GrantedAuthorityImpl("HOLDER")});

               user.setUsername_cn(username_cn);

               
    return user;
           }
       }
    }
    相應的配置文件更改為AcegiJdbcDaoImpl bean
        <bean id="daoAuthenticationProvider"
            class
    ="org.acegisecurity.providers.dao.DaoAuthenticationProvider">
            
    <property name="userDetailsService" ref="userDetailsService" />
        
    </bean>

        
    <bean id="userDetailsService"
            class
    ="com.emms.security.acegi.AcegiJdbcDaoImpl">
            
    <property name="dataSource">
                
    <ref bean="dataSource" />
            
    </property>

            
    <property name="usersByUsernameQuery">
                
    <value>
                    SELECT distinct(u.username), u.username_cn, password, enabled from emms_role r, user_role ur, emms_user u 
                     where r.id = ur.role_id and ur.user_id = u.username and u.username = ?
                
    </value>
            
    </property>

            
    <property name="authoritiesByUsernameQuery">
                
    <value>
                     SELECT u.username, r.rolename FROM user_role ur, emms_user u, emms_role r
                      WHERE ur.user_id = u.username and ur.role_id = r.id and u.username = ?
                
    </value>
            
    </property>
        
    </bean>


    posted on 2008-03-11 09:29 扭曲的鉛筆 閱讀(1532) 評論(1)  編輯  收藏 所屬分類: Spring

    FeedBack:
    # re: Acegi擴展JdbcDaoImpl獲取更多的用戶信息 2009-03-22 21:18 acegi
    請問可以使用ID登錄嗎??不用用戶名登錄。為什么在AcegiJdbcDaoImpl 使用了SQL語句還要在配置文件用再使用呢?
    指點:email:zhoushangbin@gmail.com
    可以發(fā)這個例子來學習下嗎》》??  回復  更多評論
      
    主站蜘蛛池模板: 久久久综合亚洲色一区二区三区| 亚洲中文字幕无码一久久区| 亚洲午夜国产精品无卡| 国产精品免费看久久久| 久久青草亚洲AV无码麻豆| 精品国产麻豆免费人成网站| 久久久综合亚洲色一区二区三区 | 亚洲色大成网站WWW国产| 四虎国产精品免费久久| 亚洲一线产区二线产区区| 在线观看91精品国产不卡免费| WWW亚洲色大成网络.COM| 四虎影在线永久免费观看| 日韩毛片一区视频免费| 亚洲一区二区三区无码中文字幕| 女人隐私秘视频黄www免费| 亚洲色图在线观看| 国产乱子精品免费视观看片| 亚洲日韩AV无码一区二区三区人| 日韩一区二区免费视频| 一级毛片大全免费播放| 亚洲AV无码一区二区乱孑伦AS| 国产一卡二卡四卡免费| 免费在线观看亚洲| 国产成人亚洲精品狼色在线| 99xxoo视频在线永久免费观看| 亚洲中文字幕无码中文| 亚洲人成无码www久久久| 99精品视频免费观看| 亚洲kkk4444在线观看| 亚洲免费一区二区| 99热这里有免费国产精品| 亚洲日韩国产AV无码无码精品| 精品国产亚洲一区二区在线观看 | 亚洲一区二区三区AV无码| 日韩免费无码视频一区二区三区| 在线观看亚洲AV每日更新无码| 亚洲国产日韩成人综合天堂| 亚洲视频免费在线观看| 亚洲s码欧洲m码吹潮| 亚洲AV无码成人专区片在线观看 |