使用shiro进行权限控制的时候,启动抛出异常;

YoungLee16 2017-08-07 09:12:33
使用shiro进行粗粒度的权限控制,基于Filter进行权限控制,URL



严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property user found for type Role! Did you mean 'users','users'?

因为caused by直接指向的是Repository接口,所以一针见血,只贴出DAO类的代码
package cn.itcast.bos.dao.system;


import java.util.List;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.jpa.repository.JpaRepository;

import cn.itcast.bos.domain.system.Role;

public interface RoleRepository extends JpaRepository<Role,Integer> {
@Query("from Role r inner join fetch r.users u where u.id = ?")
List<Role> findByUser(Integer id);

}


applicationContext.xml的代码如下(其中有几个冗余的名称空间,不会影响整体功能):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 安全管理器 -->
<property name="securityManager" ref="securityManager"></property>
<!-- 未认证,跳转页面 -->
<property name="loginUrl" value="/login.html"></property>
<!-- 登陆后跳转的页面 -->
<property name="successUrl" value="/index.html"></property>
<!-- 没有权限跳转的页面 -->
<property name="unauthorizedUrl" value="/unauthorized.html"></property>
<!-- shiro URL控制过滤器规则 -->
<property name="filterChainDefinitions">
<value>
/login.html* = anon
/user_login.action* = anon
/validatecode.jsp* = anon
/css/** = anon
/js/** = anon
/images/** = anon
/services/** = anon
/pages/base/courier.html* = perms[courier.list]
/pages/base/area.html* = roles[base]
/** = authc
</value>
</property>
</bean>
<!-- 配置安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="bosRealm"></property>
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor">
</bean>
</beans>



如果还有需要,烦请各位大佬提出.在线等,急??
...全文
1518 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
爱吃火锅的S 2017-12-19
  • 打赏
  • 举报
回复
引用 2 楼 YoungLee16 的回复:
谢谢你的分享,这个知识点是挺重要的.而且是特别容易出错的,这个坑,坑了我一个晚上.原因在于: 我们使用@Query注解自定义HQL语句的时候,所使用的query是源于JpaRepository与持久层进行交互的.而我的这个异常的抛出是源于我引的@Query的包来自于import org.springframework.data.elasticsearch.annotations.Query,此时,选择的包的时候,会有两个@query,一个来自elasticsearch,另一个就是我们需要使用的JPA.求大神,怎样才能避免这种错误的发生呢??经常引错包的坑实在是跳不出来了..
引用的时候IDE不是会有提示的么,选好就行了
爱吃火锅的S 2017-08-09
  • 打赏
  • 举报
回复
看样子你的用户角色是对应数据库的一张表吧,个人感觉是Shiro动态的和数据库角色表的交互出了问题,你需要把查出的roles 添加到SimpleAuthorizationInfo的实例上才能给当前用户赋予这个角色对应的权限,建议在这部分代码上加个breakpoint,这是我粗略的理解
YoungLee16 2017-08-09
  • 打赏
  • 举报
回复
谢谢你的分享,这个知识点是挺重要的.而且是特别容易出错的,这个坑,坑了我一个晚上.原因在于: 我们使用@Query注解自定义HQL语句的时候,所使用的query是源于JpaRepository与持久层进行交互的.而我的这个异常的抛出是源于我引的@Query的包来自于import org.springframework.data.elasticsearch.annotations.Query,此时,选择的包的时候,会有两个@query,一个来自elasticsearch,另一个就是我们需要使用的JPA.求大神,怎样才能避免这种错误的发生呢??经常引错包的坑实在是跳不出来了..

25,985

社区成员

发帖
与我相关
我的任务
社区描述
高性能WEB开发
社区管理员
  • 高性能WEB开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧