《权限系列》----用SpringAop控制权限一

前言

  • 在一些公司内部用的局域网项目中,常常用SpringAop来控制权限,这种控制权限的方式实现起来相对简单,原理是自定义一个注解,然后定义一个切面,再加一个拦截器,当我们每次访问后台数据时,用拦截器判断用户是否登录(session是否存在),然后利用SpringAop的前置通知判断用户访是否有权限访问此方法。

主要技术

  • SpringMvc+Spring+MyBatis

项目源码

点击这里,到github上下载项目的源码

注意

  • 小编在此只将部分代码贴上,要想看完整项目,小编会在最后附上代码。

配置SpringMvc拦截器

<!-- handle the json -->
<mvc:annotation-driven/>
......
......
......

<!--配置拦截器 -->
<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**" />  
        <mvc:exclude-mapping path="/login" />
        <mvc:exclude-mapping path="/loginsubmit" />
        <bean class="com.spring.security.SecurityInterceptor">
        </bean>
    </mvc:interceptor>
</mvc:interceptors>

<!-- 配置切面 -->
<bean id="aspectPermission" class="com.spring.security.PermissionAspect" />
<!-- 配置切入点 -->
<aop:config proxy-target-class="true">  
    <aop:aspect ref="aspectPermission">  
        <aop:pointcut id="pc"       expression="@annotation(com.spring.security.ValidatePermission) and execution(* com.spring.mybatis.controller..*.*(..)) " />  
      <aop:before pointcut-ref="pc" method="doBefore"/>  
    </aop:aspect>  
</aop:config>

  <!-- 当用户访问没有权限的方法时,抛出自定义异常AccessDeniedException,在此抓住此异常后,转发到没有权限页面 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
   <property name="exceptionMappings">
       <props>
          <prop key="com.spring.security.AccessDeniedException">forward:/accessDenied</prop>
       </props>
   </property>
</bean>

SpringMvc拦截器代码

public boolean preHandle(HttpServletRequest request,
        HttpServletResponse response, Object obj) throws Exception {

    HttpSession session = request.getSession();
    if (session.getAttribute("userLoginName") == null) {
        if ("POST".equalsIgnoreCase(request.getMethod())) {
            response.setContentType("text/html; charset=utf-8");
            PrintWriter out = response.getWriter();
            out.write(JsonUtils.objectToJson(new Result(false, "未登录!")));
            out.flush();
            out.close();
        } else {
            response.sendRedirect(request.getContextPath() + "/login");
        }
        return false;
    } else {
        return true;
    }
}

拦截器主要是判断用户是否登录。

写一个自定义权限注解

@Target(value = ElementType.METHOD)
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
public @interface ValidatePermission {
    String authority() default "";
}
  • 此注解放在Controller的方法上,当我们每次访问Controller的方法时,springaop的切面中的前置通知就会执行,判断用户是否拥有权限访问此方法。

自定义异常代码

public class AccessDeniedException extends RuntimeException  {
    public AccessDeniedException(String message) {
        super(message);
    }
}

小结

为了适应读者,故将博客的篇幅设置小一些,切面代码请看下一篇博客。

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值