spring整合shiro后无权操作未进入配置的指定unauth页面而是报错: Not authorized to invoke method: - Go语言中文社区

spring整合shiro后无权操作未进入配置的指定unauth页面而是报错: Not authorized to invoke method:


spring集成了shiro后,页面可以正常访问了,但是当设置了权限后,某些角色没有权限去访问需要鉴权的接口,按照配置,是应该进入指定的某个页面;

	<!--shiro过滤器配置,bean的id值须与web中的filter-name的值相同 -->
	<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
		<property name="securityManager" ref="securityManager" />
		<!-- 没有权限或者失败后跳转的页面 -->
		<property name="loginUrl" value="/login" />
		<property name="unauthorizedUrl" value="/unAuth" /><!--失效 -- >
		<property name="filterChainDefinitions">
			<value>
				/login = anon
				/logout = logout
				/error404 = anon
				/user/user_login = anon
				/user/user_logout = anon
				/bootstrap/** = anon
				/css/** = anon
				/js/** = anon
				/libs/** = anon
				/pages/** = anon
				/aooreyHome/** = anon
				/images/** = anon
				/** = authc
			</value>
		</property>
	</bean>


后来查找资料得知;这个和配置的权限拦截器有关:

贴上一些具体的资料:

anon  : org.apache.shiro.web.filter.authc.AnonymousFilter

author:org.apache.shiro.web.filter.authc.FormAuthenticationFilter

authcBasic:org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter

perms: org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter

port: org.apache.shiro.web.filter.authz.PortFilter

rest: org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter

roles: org.apache.shiro.web.filter.authz.RolesAuthorizationFilter

ssl: org.apache.shiro.web.filter.authz.SslFilter

user: org.apache.shiro.web.filter.authc.UserFilter

logout: org.apache.shiro.web.filter.authc.LogoutFilter

anon:例子/admins/**=anon 没有参数,表示可以匿名使用。

authc:例如/admins/user/**=authc表示需要认证(登录)才能使用,没有参数

roles:例子/admins/user/**=roles[admin],参数可以写多个,多个时必须加上引号,并且参数之间用逗号分割,当有多个参数时,例如admins/user/**=roles["admin,guest"],每个参数通过才算通过,相当于hasAllRoles()方法。

perms:例子/admins/user/**=perms[user:add:*],参数可以写多个,多个时必须加上引号,并且参数之间用逗号分割,例如/admins/user/**=perms["user:add:*,user:modify:*"],当有多个参数时必须每个参数都通过才通过,想当于isPermitedAll()方法。

rest:例子/admins/user/**=rest[user],根据请求的方法,相当于/admins/user/**=perms[user:method] ,其中method为post,get,delete等。

port:例子/admins/user/**=port[8081],当请求的url的端口不是8081是跳转到schemal://serverName:8081?queryString,其中schmal是协议http或https等,serverName是你访问的host,8081是url配置里port的端口,queryString

是你访问的url里的?后面的参数。

authcBasic:例如/admins/user/**=authcBasic没有参数表示httpBasic认证

ssl:例子/admins/user/**=ssl没有参数,表示安全的url请求,协议为https

user:例如/admins/user/**=user没有参数表示必须存在用户,当登入操作时不做检查

注:anon,authcBasic,auchc,user是认证过滤器,

perms,roles,ssl,rest,port是授权过滤器

由shiro源代码得知;

ShiroFilterFactoryBean.java

 private void applyUnauthorizedUrlIfNecessary(Filter filter) {
        String unauthorizedUrl = getUnauthorizedUrl();
        if (StringUtils.hasText(unauthorizedUrl) && (filter instanceof AuthorizationFilter)) {
            AuthorizationFilter authzFilter = (AuthorizationFilter) filter;
            //only apply the unauthorizedUrl if they haven't explicitly configured one already:
            String existingUnauthorizedUrl = authzFilter.getUnauthorizedUrl();
            if (existingUnauthorizedUrl == null) {
                authzFilter.setUnauthorizedUrl(unauthorizedUrl);
            }
        }
    }

配置的角色拦截器必须是 满足     filterinstanceof AuthorizationFilter无权页面配置才会生效
定义的filter必须满足filter instanceof AuthorizationFilter,只有perms,roles,ssl,rest,port才是属于AuthorizationFilter,而anon,authcBasic,auchc,user是AuthenticationFilter,所以unauthorizedUrl设置后页面不跳转;

shiro注解模式下,登录失败或者是没有权限都是抛出异常,并且默认的没有对异常做处理,因此解决方法要么就使用perms,roles,ssl,rest,port,或者是按照springmvc的处理方式,专门配置一个异常处理;

<bean
		class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="exceptionMappings">
			<props>
				<prop key="org.apache.shiro.authz.UnauthorizedException">/unAuth</prop>
			</props>
		</property>
	</bean>

处理后当非法访问的时候就可以走配置的无权页面





版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/ljxbbss/article/details/77837524
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-08 13:24:41
  • 阅读 ( 814 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢