分享

shiro用authc配置url和用注解@RequiresAuthentication的问题

 忠波irlphwt1ng 2019-12-01

   <bean id="chainFilterBuff"   class="com.zqws.third.shiro.framework.security.ChainFilterBuff">

    <property name="filterChainDefinitions">

<value>

/index = authc

/home/login = anon

/home/tologin = anon

/home/logout = anon

/demo/** = user

</value>

<!-- /demo/** = authc,anyRoles["e"] -->

</property>

</bean>

针对在配置中加authc的url如果用户没登陆会让他跳转到登陆  
如果我用注解@RequiresAuthentication去实现 
1. 如果用户没登陆系统,系统给出的是异常(org.apache.shiro.authz.UnauthenticatedException: The current Subject is not authenticated.  Access denied.  )而不是让他跳转到登陆
2.如果是用户已经登陆但是没某个操作的权限,我在方法上加@RequiresPermissions(value="user:create")让他具有权限 才能执行 ,抛
org.apache.shiro.authz.UnauthorizedException: Subject does not have permission [user:create]
我是觉得第一种情况是让他登陆,第二种情况是让跳转到403

但是程序只是一味得抛异常

咋办

这个问题我以前就遇到到过了,shiro 跟权限相关的都是抛异常来处理的.如果要是使用注解方式.你需要定义全局异常处理. 在spring-mvc.xml中配置上这段话:

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                登录
                <prop key="org.apache.shiro.authz.UnauthenticatedException">
                    redirect:/login
                </prop>
                授权
                 <prop key="org.apache.shiro.authz.UnauthorizedException">
                    redirect:/admin/common/exceptionLog
                </prop>
            </props>
        </property>
        <property name="defaultErrorView" value="error/genericView"/>
    </bean>

但是最好不要这样做.还是配置路径拦截靠谱. 因为spring mvc全局异常还要做错误信息记录的.所以用在这不好.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多