解决方案 »
- JACOB操作word 报错 VariantChangeType failed
- ServletContextListener的问题,求帮助
- 最近打算写个关于cas的系列博客,欢迎各位大牛提出宝贵的意见建议
- weblogic错误getPort()
- 如何通过java从二进制文件中读取以前用c写入的一个结构体?
- 在Struts 中点击操作页面不跳转怎么做?
- 好多错误呀,麻烦高手帮忙看看.
- 解释线程同步和异步
- 各位高手帮帮忙!请教关于JAVAMAIL的问题(急!在线等)
- 實體bean的CMP中關於cmr的問題
- 用iframe对页面分块,每个里面嵌一个jsp页面,jsp页面显示时占不满页面。在IE11不可以。
- 对 log4j 生成的日志内容处理问题
package cn.zzkj.wp.security;import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;import cn.zzkj.wp.dao.ResourcesDao;
import cn.zzkj.wp.dao.UserDao;
import cn.zzkj.wp.model.Resources;
@Component
public class MyUserDetailServiceImpl implements UserDetailsService {
@Autowired
private UserDao userDao;
@Autowired
private ResourcesDao resourcesDao; @Override
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException, DataAccessException {
System.err.println("-----------MyUserDetailServiceImpl loadUserByUsername ----------- ");
//取得用户的权限
cn.zzkj.wp.model.User users = userDao.querySingleUser(username);
if (users==null)
throw new UsernameNotFoundException(username+" not exist!");
Collection<GrantedAuthority> grantedAuths = obtionGrantedAuthorities(users);
// 封装成spring security的user
User userdetail = new User(
users.getUserName(),
users.getUserPassword(),
true,
true,
true,
true,
grantedAuths //用户的权限
);
return userdetail;
} //或得用户权限
@SuppressWarnings("deprecation")
private Collection<GrantedAuthority> obtionGrantedAuthorities(
cn.zzkj.wp.model.User user) {
//根据用户id,加载该用户拥有的所有资源
List<Resources> resources = resourcesDao.getUserResources(String.valueOf(user.getUserId()));
Set<GrantedAuthority> authSet = new HashSet<GrantedAuthority>();
for (Resources res : resources) {
// ZZQ 用户可以访问的资源名称(或者说用户所拥有的权限) 注意:必须"ROLE_"开头
// 关联代码:applicationContext-security.xml
// 关联代码:com.huaxin.security.MySecurityMetadataSource#loadResourceDefine
authSet.add(new GrantedAuthorityImpl("ROLE_" + res.getResKey()));
}
return authSet;
}}
spring security版本我用的3.1