紧急求助:shiro realm里JPA no session

小红帽吃香蕉 发布于 2013/08/23 17:08
阅读 694
收藏 1

用的Spring + hibernate + spring data jpa + shiro

已经打开openEntityManagerInViewFilter

但是进行权限认证的时候,出现no session

realm代码片段

private UserService userService;
	
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	@Override
	/**
	 * 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用.
	 */
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		LoginUser loginUser = (LoginUser) principals.fromRealm("ShiroRealm").iterator().next();
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		info.addStringPermissions(userService.findStringPermissions(loginUser.getLoginCode()));
		return info;
	}

	@Override
	/**
	 * 认证回调函数, 登录时调用.
	 */
	protected AuthenticationInfo doGetAuthenticationInfo(
			AuthenticationToken authcToken) throws AuthenticationException {
		UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
		User user = userService.findEnabled(token.getUsername(), String.valueOf(token.getPassword()));
		if (user != null) {
			token.setPassword(user.getPassword().toCharArray());
			return new SimpleAuthenticationInfo(new LoginUser(null, user.getLoginCode(), user.getLoginName()), user.getPassword(), "ShiroRealm");
		} else {
			return null;
		}
	}
UserService代码片段
public Set<String> findStringPermissions(String loginCode) {
		User user = this.userRepository.findOne(loginCode);
		Assert.notNull(user, "用户不存在");
		Set<String> stringPermissions = new HashSet<>();
		Set<Role> roles = user.getRoles();
		Iterator<Role> iterator = roles.iterator();
		while(iterator.hasNext()) {
			Role role = iterator.next();
			Set<Permission> permissions = role.getPermissions();
			Iterator<Permission> iter = permissions.iterator();
			while(iter.hasNext()) {
				Permission permission = iter.next();
				stringPermissions.add(permission.getResource().getName() + ":" + permission.getOperator().getName());
			}
		}
		return stringPermissions;
	}
这里user.getRoles()就报no session,延迟加载的。

我在其他地方调用这个方法就没问题,甚至在realm的doGetAuthenticationInfo里用也没问题,就是在doGetAuthorizationInfo里会no session ,搞的我没脾气了,求助啊


加载中
0
江南白衣
江南白衣
好像都升到最新版的问题少一点,Springside4.1.1-SNAPSHOT里好像没什么问题了
0
b
big_tu
请问运行quick-start.bat出现这个错误是什么引起的?
返回顶部
顶部