1.springmvc配置文件中配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:aop="http://www.springframework.org/schema/aop"
  4. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop.xsd">
  14.  
  15. <!-- 默认的注解映射的支持 -->
  16. <mvc:annotation-driven />
  17.  
  18. <!-- 将 springSwaggerConfig加载到spring容器 -->
  19. <bean class="com.mangofactory.swagger.configuration.SpringSwaggerConfig" />
  20. <!-- 将自定义的swagger配置类加载到spring容器 -->
  21. <bean class="com.aisino.qysds.common.util.SwaggerConfig" />
  22. <!-- 静态资源文件,不会被Spring MVC拦截 -->
  23. <mvc:resources mapping="/api-doc/**" location="/api-doc/" />
  24. <mvc:resources mapping="/js/**" location="/js/" />
  25. <!-- 自动扫描的包名 -->
  26. <context:component-scan base-package="com.controller"/>
  27.  
  28. <!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
  29. <bean id="mappingJacksonHttpMessageConverter"
  30. class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
  31. <property name="supportedMediaTypes">
  32. <list>
  33. <value>text/html;charset=UTF-8</value>
  34. <value>text/plain;charset=UTF-8</value>
  35. <!-- <value>application/x-www-form-urlencoded;charset=UTF-8</value> -->
  36. </list>
  37. </property>
  38. </bean>
  39.  
  40. <mvc:interceptors>
  41.  
  42. <mvc:interceptor>
  43. <mvc:mapping path="/**"/>
  44. <bean class="AuthorityAnnotationInterceptor"/>
  45. </mvc:interceptor>
  46. </mvc:interceptors>
  47. <aop:aspectj-autoproxy />
  48.  
  49. </beans>

  2.自定义拦截器,实现HandlerInterceptor接口或继承HandlerInterceptor

  1. import java.util.List;
  2.  
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import javax.servlet.http.HttpSession;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import org.springframework.web.method.HandlerMethod;
  9. import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
  10.  
  11. import com.alibaba.fastjson.JSON;
  12.  
  13. public class AuthorityAnnotationInterceptor extends HandlerInterceptorAdapter {
  14.  
  15. final Logger logger = LoggerFactory.getLogger(getClass());
  16.  
  17. @SuppressWarnings("unchecked")
  18. @Override
  19. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  20. //开启swagger时,打开
  21. // if (handler instanceof ResourceHttpRequestHandler) {
  22. // logger.error("swagger ok");
  23. // return true;
  24. // }
  25. Authority authority=null;
  26. HandlerMethod handler2=(HandlerMethod) handler;
  27. Class<?> clazz=handler2.getBeanType();
  28. //类注解
  29. if(clazz.isAnnotationPresent(Authority.class)){
  30. authority=clazz.getAnnotation(Authority.class);
  31. }
  32. //方法注解
  33. if(handler2.getMethodAnnotation(Authority.class)!=null){
  34. authority = handler2.getMethodAnnotation(Authority.class);
  35. }
  36. if(null == authority){
  37. //没有声明权限,放行
  38. return true;
  39. }
  40.  
  41. logger.debug("fireAuthority", authority.toString());
  42. HttpSession session = request.getSession();
  43. boolean aflag = false;
  44.  
  45. for(AuthorityType at : authority.authorityTypes()){
  46. List<String> role = (List<String>)session.getAttribute("用户权限");
  47. if(role.contains(at.getId())){
  48. aflag = true;
  49. if(aflag){
  50. aflag = true;
  51. break;
  52. }
  53. }
  54. }
  55. if(false == aflag){
  56. response.getWriter().println("没有权限");
  57. }
  58. return aflag;
  59. }
  60.  
  61. }

3.自定义权限注解

  1. import java.lang.annotation.Documented;
  2. import java.lang.annotation.ElementType;
  3. import java.lang.annotation.Retention;
  4. import java.lang.annotation.RetentionPolicy;
  5. import java.lang.annotation.Target;
  6. //支持在类和方法上
  7. @Target({ElementType.TYPE,ElementType.METHOD})
  8. @Retention(RetentionPolicy.RUNTIME)
  9. @Documented
  10. public @interface Authority {
  11. AuthorityType[] authorityTypes();
  12. }

4.权限枚举

  1. public enum AuthorityType{
  2.  
  3. ONE("一级", "1"),
  4. TWO("二级", "2"),
  5. THREE("三级", "3"),
  6. ;
  7. private String name;
  8. private String id;
  9.  
  10. private AuthorityType(String name, String id) {
  11. this.name = name;
  12. this.id = id;
  13. }
  14.  
  15. public String getName() {
  16. return name;
  17. }
  18.  
  19. public void setName(String name) {
  20. this.name = name;
  21. }
  22.  
  23. public String getId() {
  24. return id;
  25. }
  26.  
  27. public void setId(String id) {
  28. this.id = id;
  29. }
  30.  
  31. }

5.控制器Controller

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestMethod;
  5. import org.springframework.web.bind.annotation.RequestParam;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7.  
  8. @Controller
  9. @RequestMapping("/test/allow")
  10. @Authority(authorityTypes =AuthorityType.ONE)
  11. public class TestController extends BaseController {
  12.  
  13. @ResponseBody
  14. @RequestMapping(value = "test", method = RequestMethod.GET)
  15. @Authority(authorityTypes =AuthorityType.TWO)
  16. public boolean test() {
  17. return true;
  18. }
  19.  
  20. }

每次请求有权限的接口,都需要验证当前用户是否有该权限,有则通过,反之不通过,最后附上springmvc执行流程

SpringVC 拦截器+自定义注解 实现权限拦截的更多相关文章

  1. mybaits拦截器+自定义注解

    实现目的:为了存储了公共字典表主键的其他表在查询的时候不用关联查询(所以拦截位置位于mybaits语句查询得出结果集后) 项目环境 :springboot+mybaits 实现步骤:自定义注解——自定 ...

  2. spring boot 使用拦截器,注解 实现 权限过滤

    http://www.cnblogs.com/zhangXingSheng/p/7744997.html spring boot 使用拦截器 实现 用户登录拦截 http://www.cnblogs. ...

  3. struts2拦截器加自定义注解实现权限控制

    https://blog.csdn.net/paul342/article/details/51436565 今天结合Java的Annotation和Struts2进行注解拦截器权限控制. 功能需求: ...

  4. spring自定义注解实现登陆拦截器

    1.spring自定义注解实现登陆拦截器 原理:定义一个注解和一个拦截器,拦截器拦截所有方法请求,判断该方法有没有该注解.没有,放行:有,要进行验证.从而实现方法加注解就需要验证是否登陆. 2.自定义 ...

  5. springboot + 注解 + 拦截器 + JWT 实现角色权限控制

    1.关于JWT,参考: (1)10分钟了解JSON Web令牌(JWT) (2)认识JWT (3)基于jwt的token验证 2.JWT的JAVA实现 Java中对JWT的支持可以考虑使用JJWT开源 ...

  6. Spring实现自定义注解并且配置拦截器进行拦截

    有时候我们会自定义注解,并且需要配置拦截器对请求方法含有该自定义注解的方法进行拦截操作 自定义注解类 NeedToken.java import java.lang.annotation.Docume ...

  7. Struts2知识点小结(四)--拦截器与注解开发

    一.Struts2的拦截器(interceptor) 作用:当请求进入struts2框架后(进入之前可以用filter进行拦截),想对请求进行拦截操作(功能增强.权限控制),需要拦截器组件 1.str ...

  8. struts2学习笔记--拦截器(Interceptor)和登录权限验证Demo

    理解 Interceptor拦截器类似于我们学过的过滤器,是可以在action执行前后执行的代码.是我们做web开发是经常使用的技术,比如权限控制,日志.我们也可以把多个interceptor连在一起 ...

  9. SpringMVC拦截器(资源和权限管理)

    1.DispatcherServlet SpringMVC具有统一的入口DispatcherServlet,所有的请求都通过DispatcherServlet.    DispatcherServle ...

随机推荐

  1. 省选九省联考T2 IIIDX(线段树)

    题目传送门:https://www.luogu.org/problemnew/show/P4364 期中考后记:期中考刚考完,感觉不咋滴,年排第3.我抗压力太差了..期末得把rank1抢回来. 本来感 ...

  2. Linux Shell中的反引号,单引号,双引号

    反引号位 (`) 位于键盘的Tab键的上方.1键的左方.注意与单引号(')位于Enter键的左方的区别. 在Linux中起着命令替换的作用.命令替换是指shell能够将一个命令的标准输出插在一个命令行 ...

  3. jQuery 方法 属性

    Attribute:$("p").addClass(css中定义的样式类型); 给某个元素添加样式$("img").attr({src:"test.j ...

  4. Git常用操作命令收集

      Git常用操作命令收集 1.进入本地仓库访问位置之后执行命令 1) 远程仓库相关命令 检出仓库:$ git clone git://github.com/jquery/jquery.git 查看远 ...

  5. IE6、7下块级元素设置display:inline-block不换行的解决办法

    使用背景 在实际的工作中,我们有的时候会把块元素设置为inline-block,这样做的目的有2个,一是块元素能够排列到一行,二是块元素就形成包裹性,能够自适应content area,而不必设置宽和 ...

  6. Intellij IDEA 最头大的问题,如何自定义注释模板?

    想栈长我当初从 Eclipse 转用 IDEA 真是纠结,放弃然后尝试了N次,不过现在已经算是转型成功了,可以完全脱离 Eclipse 撸码了,虽然说我现在真的撸得非常少了.. 说到 IDEA 的痛点 ...

  7. WPF动画的几种模式

    最近在用WPF做简单动画,以下是几点经验总结: 1. 使用DispatcherTimer做动画 VB6的年代大家就用Timer做动画了,不用多解释,这个DispatcherTimer和本身的Timer ...

  8. 从零开始的全栈工程师——html篇1.8(知识点补充与浏览器兼容性)

    知识点补充 一.浏览器的兼容问题(关于浏览器的兼容问题 有很多大佬已经解释的很清楚了 这个得自己百度去多花点时间去了解 这里咱们只说一下前面的漏点) 浏览器兼容性问题又被称为网页兼容性或网站兼容性问题 ...

  9. sublime 主要使用方法

    ---------------最常用的1.新建文件-输入"html:xt"后 按"Ctrl+E键"或 "tab键" ,可快速生成xhtml ...

  10. 数学建模大赛-NO.1

    数学建模大赛-NO.1   论文精析 近期,在网上各种的收罗,张开了各式各样的捕抓.哈哈… .终于,在一个不经意之间发现了一个巨大无比的宝藏式的网站,此网站网址为:http://cjsxjm.gzsi ...