📄 hibernatedaoimpl.java
字号:
package sample.auth;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
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 sample.auth.cache.AuthorityBasedUserCache;
import sample.dao.IBaseDao;
import sample.mappings.role.Role;
import sample.utils.MisUtils;
public class HibernateDaoImpl implements UserDetailsService{
private String rolePrefix = "";
private boolean usernameBasedPrimaryKey = false;
private AuthorityBasedUserCache cache;
private IBaseDao baseDao;
public String getRolePrefix() {
return rolePrefix;
}
public void setRolePrefix(String rolePrefix) {
this.rolePrefix = rolePrefix;
}
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
UserDetails user = getUsersByUsernameQuery(username);
if(user == null) return null;
GrantedAuthority[] arrayAuths =getAuthoritiesByUsernameQuery(username);
if (arrayAuths.length == 0) {
throw new UsernameNotFoundException("User has no GrantedAuthority");
}
return new User(username, user.getPassword(), user.isEnabled(),
true, true, true, arrayAuths);
}
public UserDetails getUsersByUsernameQuery(String username)throws DataAccessException {
sample.mappings.user.User misUser = (sample.mappings.user.User)baseDao.loadByKey(sample.mappings.user.User.class,"name",username);
if(misUser != null)
{
org.acegisecurity.userdetails.UserDetails user =
new User(misUser.getName(),misUser.getPassword(),MisUtils.parseBoolean(misUser.getEnable()),true,true,true,getAuthoritiesByUsernameQuery(username));
return user;
}else
return null;
}
public GrantedAuthority[] getAuthoritiesByUsernameQuery(String username)
throws DataAccessException {
sample.mappings.user.User misUser = (sample.mappings.user.User)baseDao.loadByKey(sample.mappings.user.User.class,"name",username);
if(misUser != null){
GrantedAuthority[] grantedAuthoritys = cache.getAuthorityFromCache(misUser.getName());
if(grantedAuthoritys == null){
Set roles = misUser.getRoles();
Iterator it = roles.iterator();
List list = new ArrayList();
while(it.hasNext() ){
GrantedAuthorityImpl gai = new GrantedAuthorityImpl( ((Role)it.next()).getName() );
list.add(gai);
}
grantedAuthoritys =(GrantedAuthority[]) list.toArray(new GrantedAuthority[0]);
cache.putAuthorityInCache(misUser.getName(),grantedAuthoritys);
return grantedAuthoritys;
}
return grantedAuthoritys;
}
return null;
}
public IBaseDao getBaseDao() {
return baseDao;
}
public void setBaseDao(IBaseDao baseDao) {
this.baseDao = baseDao;
}
public AuthorityBasedUserCache getCache() {
return cache;
}
public void setCache(AuthorityBasedUserCache cache) {
this.cache = cache;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -