当前位置:   article > 正文

利用spring mvc 拦截器 鉴权

spring mvc 拦截器 鉴权

1.需求场景

利用拦截器实现用户登录鉴权

2.项目环境

spring spring mvc mybatis  mysql

3.实现方法

  1. package com.jlc.action;
  2. import java.util.Enumeration;
  3. import java.util.List;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import net.sf.json.JSONObject;
  7. import org.apache.commons.lang3.StringUtils;
  8. import org.apache.log4j.Logger;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.stereotype.Service;
  11. import org.springframework.web.servlet.HandlerInterceptor;
  12. import org.springframework.web.servlet.ModelAndView;
  13. /*****
  14. *
  15. * @ClassName: LoginInterceptorAction
  16. * @Description: TODO 拦截器
  17. * @author demo
  18. *
  19. *
  20. */
  21. @Service
  22. public class LoginInterceptorAction implements HandlerInterceptor {
  23. //记录日志对象
  24. Logger log = Logger.getLogger(LoginInterceptorAction.class.getName());
  25. @Autowired
  26. private UserRoleService userRoleService;
  27. @Override
  28. public void afterCompletion(HttpServletRequest request,
  29. HttpServletResponse respone, Object obj, Exception e)
  30. throws Exception {
  31. }
  32. @Override
  33. public void postHandle(HttpServletRequest request, HttpServletResponse respone,
  34. Object obj, ModelAndView view) throws Exception {
  35. // TODO Auto-generated method stub
  36. }
  37. @Override
  38. public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
  39. Object obj) throws Exception {
  40. String username = (String) request.getSession().getAttribute("username");
  41. //获取用户拥有的权限列表(getAllRight方法用户登陆后自行实现)
  42. List<String> list = getAllRight(username);
  43. String callback=request.getParameter("callbackparam");
  44. String url = request.getRequestURI();
  45. String ip=getIpAddr(request);
  46. JSONObject jo = new JSONObject();
  47. String param = getAllParameter(request);
  48. boolean flag = false;
  49. //登录地址不必拦截
  50. if( url.indexOf("login/in")!=-1
  51. flag = true;
  52. }else{
  53. if(null == username ||username.equals("")){
  54. jo.put("code", "1002");//会话超时 退出系统
  55. response.getWriter().write(callback+"("+jo.toString()+")");
  56. flag = false;
  57. }else if(hasRight(url,list)){
  58. flag = true;
  59. }else{
  60. flag = false;
  61. jo.put("code", "1003");//没有权限访问
  62. response.getWriter().write(callback+"("+jo.toString()+")");
  63. }
  64. }
  65. return flag;
  66. }
  67. /***
  68. *获取访问者IP
  69. *
  70. ***/
  71. public static String getIpAddr(HttpServletRequest request) {
  72. String ip = request.getHeader("X-Real-IP");
  73. if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
  74. return ip;
  75. }
  76. ip = request.getHeader("X-Forwarded-For");
  77. if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
  78. // 多次反向代理后会有多个IP值,第一个为真实IP。
  79. int index = ip.indexOf(',');
  80. if (index != -1) {
  81. return ip.substring(0, index);
  82. } else {
  83. return ip;
  84. }
  85. } else {
  86. return request.getRemoteAddr();
  87. }
  88. }
  89. /**
  90. *
  91. * @Title: hasRight
  92. * @Description: TODO 鉴权
  93. * @param authList
  94. * @param url
  95. * @return
  96. */
  97. public boolean hasRight(String url,List<String> list){
  98. boolean flag=false;
  99. if(list.size()>0){
  100. if(list.contains(url)){
  101. flag=true;
  102. }else{
  103. flag =false;
  104. }
  105. }else{
  106. flag = false;
  107. }
  108. return flag;
  109. }
  110. /***
  111. *
  112. * @Title: getAllParameter
  113. * @Description: TODO 返回参数列表
  114. * @param request
  115. * @return
  116. */
  117. public String getAllParameter(HttpServletRequest request){
  118. String str="";
  119. Enumeration<String> keys = request.getParameterNames();
  120. while(keys.hasMoreElements()) {
  121. String k = keys.nextElement();
  122. String v = request.getParameter(k);
  123. //System.out.println(k + " = " + request.getParameter(k) );
  124. str+=k+"="+v+",";
  125. }
  126. return str;
  127. }
  128. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/182390?site
推荐阅读
相关标签
  

闽ICP备14008679号