filter和session实现登录控制

使用filter做登录控制

  • 目的是防止未登录的用户访问页面的内容
  • 把未登录用户的请求重定向为登录请求

遇到过的问题

  • 静态资源的过滤:允许未登录状态下访问静态资源,以便展示登录页面的样式
  • session的使用:登陆后要设置session的值,退出时清空
  • 无数次重定向:拦截未授权请求时,进行重定向后的请求一定要是 不登录就可以访问的请求,不然会有重定向死循环

项目结构

-java
--plan.demo
---config
----filteConfig
---controller
----loginController
---filter
----MyFilter
-resources
--static
---js
----login.js
----logout.js
---css
--templates
---login.html
---index.html

MyFilter

//继承Filter接口
public class MyFilter implements Filter {
    //重写接口的三个方法
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
        HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper(httpResponse);
        //得到表示请求的字符串
        String url = httpRequest.getRequestURI().substring(httpRequest.getContextPath().length());
        //得到session中“username”属性,以便接下来判断是否已登录
        Object user = httpRequest.getSession().getAttribute("username");
        //设置未经登录就可以访问的请求
        // “/static":允许访问静态资源,以便展示login.html的样式,如果不这么做
        // 不仅无法展示样式。而且js文件被禁止访问的话,就无法响应点击操作,就会卡死在login.html
        // “/index”:此请求的作用是返回登录界面,可以在 LoginController 中看到
        // “/login”:这个请求在在login.js中,是登录事件的请求路径
        // 如果不允许这个请求,就会造成把登录请求无数次重定向为 /index 请求
        if (url.startsWith("/static") || url.equals("/index") || url.startsWith("/login")) 
            //在白名单中的url,放行访问
            filterChain.doFilter(httpRequest, httpResponse);
            return;
        }
        //其他的请求都需要判断是否登陆,如果登录,接下来该干嘛就干嘛
        else if (user != null) {
            System.out.println("user不为空");
            //若为登录状态 放行访问
            filterChain.doFilter(httpRequest, httpResponse);
            return;
        }
        //如果没登录,就重定向为 /index 请求
         else {
            //否则把请求重定向为 /index,在loginController中,这个请求会返回登录页面
            System.out.println("还未登陆");
            wrapper.sendRedirect("/index");
        }
    }

    @Override
    public void destroy() {

    }
}

filterConfig

//这个注解肯定要加的
@Configuration
public class filterConfig {
    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new MyFilter());
        registrationBean.addUrlPatterns("/*");
        registrationBean.setName("FirstFilter");
        return registrationBean;
    }
	//把自己的过滤器创建成一个bean
    @Bean(name = "myFilter")
    public Filter myFilter() {
        return new MyFilter();
    }
}

LoginController

@Controller
public class LoginController {
	//这个就是MyFilter中允许直接访问的请求,会返回登录页面
    @RequestMapping("/index")
    public String goToLogin(){
        return "login";
    }
    @Autowired
    LoginService loginService;
    @ResponseBody
    //login请求也要可以直接访问
    @RequestMapping("/login")
    public String login(String name, String password, HttpSession session){
        System.out.println(name+" "+password);
        String getPassword = loginService.getPassword(name);
        System.out.println("得到的密码:"+getPassword);
        //如果可以登录,再对session赋值
        if (getPassword.equals(password)){
            session.setAttribute("username", name);
            System.out.println("设置了session");
            return "1";
        }
        else {
            return "0";
        }
    }
    @ResponseBody
    @RequestMapping("/logout")
    //退出的时候清空session
    public String logout(HttpSession session){
        session.invalidate();
        System.out.println("清空session");
        return "1";
    }
}

login.js

$(document).ready(function () {
    $('#btn').click(function () {
        var login_name = $("#login_name").val();
        var login_password = $("#password").val();
        $.ajax({
        	//如果 filter不允许 login 请求直接访问,就会在这里造成死循环
            url: "/login",
            type: "post",
            dataType: "json",
            data : {
                "name": login_name,
                "password": login_password
            },
            success: function (data) {
                console.log(data);
                if(data==1)
                    window.location.href='../templates/index.html';
                else{
                    alert("用户名或密码不正确!");
                    window.location.href='../templates/login.html';
                }
            },
            error: function (e) {
                console.log("faild")
            }
        })
    });
})

你可能感兴趣的:(filter和session实现登录控制)