当前位置:   article > 正文

servlet和springmvc用户鉴权_springmvc 鉴权

springmvc 鉴权

servlet

创建filter文件:


  1. package com.filter;
  2. import java.io.IOException;
  3. import javax.servlet.Filter;
  4. import javax.servlet.FilterChain;
  5. import javax.servlet.FilterConfig;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.ServletRequest;
  8. import javax.servlet.ServletResponse;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import javax.servlet.http.HttpSession;
  12. /**
  13. * Servlet Filter implementation class login
  14. */
  15. public class login_filter implements Filter {
  16. /**
  17. * Default constructor.
  18. */
  19. public login_filter() {
  20. // TODO Auto-generated constructor stub
  21. }
  22. /**
  23. * @see Filter#destroy()
  24. */
  25. public void destroy() {
  26. // TODO Auto-generated method stub
  27. }
  28. /**
  29. * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
  30. */
  31. String passUrl = "";
  32. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  33. HttpServletRequest httpRequest=(HttpServletRequest)request;
  34. HttpServletResponse httpResponse=(HttpServletResponse)response;
  35. String[] strArray = passUrl.split(";");
  36. for (String str : strArray) {
  37. if (str.equals(""))
  38. continue;
  39. if (httpRequest.getRequestURL().indexOf(str) >= 0) {
  40. chain.doFilter(request, response);
  41. return;
  42. }
  43. }
  44. HttpSession session=httpRequest.getSession();
  45. if(session.getAttribute("username")!=null){
  46. chain.doFilter(request, response);
  47. }
  48. else{
  49. httpResponse.sendRedirect(httpRequest.getContextPath()+"/login.jsp");
  50. }
  51. }
  52. /**
  53. * @see Filter#init(FilterConfig)
  54. */
  55. public void init(FilterConfig fConfig) throws ServletException {
  56. // TODO Auto-generated method stub
  57. passUrl = fConfig.getInitParameter("passUrl");
  58. }
  59. }

配置xml文件

  1. <filter>
  2. <filter-name>login_filter</filter-name>
  3. <display-name>login_filter</display-name>
  4. <description></description>
  5. <filter-class>com.filter.login_filter</filter-class>
  6. <init-param>
  7. <param-name>passUrl</param-name>
  8. <param-value>login.jsp;register.jsp;login;register;</param-value> //无需filter的文件
  9. </init-param>
  10. </filter>
  11. <filter-mapping>
  12. <filter-name>login_filter</filter-name>
  13. <url-pattern>/*</url-pattern>
  14. </filter-mapping>

重启之后,除了login和register文件,其他都需要登录才能访问


springmvc

创建class文件

  1. package com.springmvc.fastdfs;
  2. import org.springframework.web.servlet.HandlerInterceptor;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import javax.servlet.http.HttpSession;
  6. import org.springframework.web.servlet.ModelAndView;
  7. public class SecurityInterceptor implements HandlerInterceptor{
  8. public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) throws Exception {
  9. HttpSession session = req.getSession(true);
  10. Object obj = session.getAttribute("username");
  11. if (obj == null || "".equals(obj.toString())) {
  12. res.sendRedirect("login");
  13. }
  14. return true;
  15. }
  16. public void postHandle(HttpServletRequest req, HttpServletResponse res, Object arg2, ModelAndView arg3) throws Exception {
  17. }
  18. public void afterCompletion(HttpServletRequest req, HttpServletResponse res, Object arg2, Exception arg3) throws Exception {
  19. }
  20. }

修改springmvc.xml文件


  1. <mvc:annotation-driven />
  2. <mvc:interceptors>
  3. <mvc:interceptor>
  4. <!-- 需拦截的地址 -->
  5. <mvc:mapping path="/**" />
  6. <!-- 需排除拦截的地址 -->
  7. <mvc:exclude-mapping path="/login"/>
  8. <mvc:exclude-mapping path="/logindo"/>
  9. <mvc:exclude-mapping path="/register"/>
  10. <mvc:exclude-mapping path="/registerdo"/>
  11. <bean class="com.springmvc.fastdfs.SecurityInterceptor" />
  12. </mvc:interceptor>
  13. </mvc:interceptors>

重启之后可以进行拦截




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

闽ICP备14008679号