第十二章 与Spring集成(三) Shiro权限注解

本文介绍如何在Spring MVC环境中使用Shiro权限注解进行权限控制,并演示如何配置ShiroSpringAOP支持及异常处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

跟我学Shiro第12章Demo(仅JAVA SE+Web+Shiro权限注解)


Shiro 提供了相应的注解用于权限控制,如果使用这些注解就需要使用AOP 的功能来进行判断,如Spring AOP;Shiro 提供了Spring AOP 集成用于权限注解的解析和验证。为了测试,此处使用了Spring MVC来测试Shiro 注解,当然Shiro 注解不仅仅可以在web环境使用,在独立的JavaSE 中也是可以用的,此处只是以web 为例了。


在spring-mvc.xml配置文件添加Shiro Spring AOP权限注解的支持:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    <context:component-scan base-package="web">
    	<!-- 这两个注解其实可以忽略掉,意思是只扫描并实例化指定包下的Controller或ControllerAdvice注解的类 -->
    	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    	<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>
    
    <!-- Shiro Spring AOP权限注解的支持 -->
    <!-- 表示代理类 -->
    <aop:config proxy-target-class="true"></aop:config>
    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
    	<property name="securityManager" ref="securityManager"></property>
    </bean>
    
    <!-- 
    	平时访问页面都需要通过Controller,但有时候只想直接地跳转到目标页面,这时候就可以使用mvc:view-controller标签
    	path="/hello":就是你访问的路径(相当于RequestMapping("/hello")) 
		view-name="hello":是你所要的视图(如hello.jsp,相当于return "hello")
    -->
    <mvc:view-controller path="/" view-name="index"/>
    <!-- 上面一行配置成功后,可能访问其他的页面会失败,不要慌,加下面这行-->
    <mvc:annotation-driven/>
    
    <!-- 默认的视图解析器,在上边的解析错误时使用(默认使用html) -->
    <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
    	<property name="contentType" value="text/html"></property>
    	<property name="prefix" value="/"></property>
    	<property name="suffix" value=".jsp"></property>
    </bean>
    
    <!-- 控制 器异常处理 -->
    <bean id="exceptionHandlerExceptionResolver" class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver"></bean>
    <bean class="web.exception.DefaultExceptionHandler"></bean>
</beans>

如上配置用于开启Shiro Spring AOP 权限注解的支持;<aop:config proxy-target-class="true">表示代理类。


接着就可以在相应的控制器(AnnotationController)中使用如下方式进行注解:

@Controller
public class AnnotationController {
	@RequestMapping("/hello1")
	public String hello1(){
		SecurityUtils.getSubject().checkRole("admin");
		return "success";
	}
	
	/*
	 * 访问hello2方法的前提是当前用户有admin角色
	 */
	@RequiresRoles("admin")
	@RequestMapping("/hello2")
	public String hello2(){
		return "success";
	}
}

访问hello2方法的前提是当前用户有admin角色。


当验证失败,其会抛出UnauthorizedException异常,此时可以使用Spring的ExceptionHandler(DefaultExceptionHandler)来进行拦截处理:

@ControllerAdvice
public class DefaultExceptionHandler {
	/**
	 * 当出现 没有权限 异常 走这个方法
	 * 后续根据不同的需求定制即可 
	 */
	@ExceptionHandler({UnauthorizedException.class})
	@ResponseStatus(HttpStatus.UNAUTHORIZED)
	public ModelAndView processUnauthenticatedException(NativeWebRequest request,UnauthorizedException e){
		ModelAndView mv = new ModelAndView();
		mv.addObject("exception",e);
		mv.setViewName("unauthorized");
		return mv;
	}
}

如果集成Struts2,需要注意《Shiro+Struts2+Spring3 加上@RequiresPermissions 后@Autowired失效》问题:

http://jinnianshilongnian.iteye.com/blog/1850425


权限注解

@RequiresAuthentication

表示当前Subject已经通过login 进行了身份验证;即Subject. isAuthenticated()返回true。

@RequiresUser

表示当前Subject已经身份验证或者通过记住我登录的。

@RequiresGuest

表示当前Subject没有身份验证或通过记住我登录过,即是游客身份。

@RequiresRoles(value={“admin”, “user”}, logical= Logical.AND)

表示当前Subject需要角色admin 和user。

@RequiresPermissions (value={“user:a”, “user:b”}, logical= Logical.OR)

表示当前Subject需要权限user:a或user:b。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值