本篇博客主要记录过滤器这个东西,以前也没怎么用过,用过也没怎么看过,
最近的一个项目有使用到,纯手工登录和过滤器拦截请求的url;
所以来记录一下,方便自己也方便大家快速使用;
操作session存取信息可参考我的博客:操作的Session工具类
登录方法:
@RequestMapping("login")
@ResponseBody
public String login(HttpServletRequest request) {
Map rtnMap = new HashMap();
rtnMap.put("code", 500);
String username = request.getParameter("username");//用户名
String password = request.getParameter("password");//密码
//查询用户名和密码是否匹配,是否存在
User userInfo = userServiceImpl.findNameAndPsw(username,password);
if ( null != userInfo){
//如果存在则先保存Session,在返回页面
SessionUtils.setUser(request,userInfo);
rtnMap.put("code", 200);
rtnMap.put("errMsg", "登录成功");
logger.info("登录成功");
}else{
rtnMap.put("errMsg", "用户名密码错误");
logger.info("用户名密码错误");
}
return JsonUtils.toJsonNoException(rtnMap);
}
过滤器代码:实现(implements)javax.servlet.Filter接口,并重写其中的方法
import cn.cnic.research.utils.JsonUtils;
import cn.cnic.research.utils.SessionUtils;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@WebFilter(filterName = "sessionFilter",urlPatterns = {"/*"})
public class SessionFilter implements Filter {
//标示符:表示当前用户未登录(可改为json样式:如下方我使用的是map)
String NO_LOGIN = "403";
Map rtnMap = new HashMap();
//免登录就可访问的路径(比如:注册,登录,注册页面上的一些获取数据等)
String[] includeUrls = new String[]{"/organ/organInfoByName","/user/register","/user/login","/user/getSessionUserInfo"};
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
HttpSession session = request.getSession(false);
//当前请求的url
String uri = request.getRequestURI();
System.out.println("filter url:"+uri);
//判断url是否需要过滤
boolean needFilter = isNeedFilter(uri);
if (!needFilter) { //不需要过滤直接传给下一个过滤器
filterChain.doFilter(servletRequest, servletResponse);
} else { //需要过滤器
// session中包含user对象,则是登录状态
if(null != session && null != SessionUtils.getUser(request)){
filterChain.doFilter(request, response);
}else{
rtnMap.put("code", 403);
rtnMap.put("errMsg", "您还未登录,请先登录!!!");
response.setContentType("text/html; charset=utf-8");
response.getWriter().write(JsonUtils.toJsonNoException(rtnMap));
}
}
}
/**
* @Author: wdd
* @Description: 是否需要过滤
* @Date: 2019-02-21 13:20:54
* @param uri
*/
public boolean isNeedFilter(String uri) {
for (String includeUrl : includeUrls) {
if(includeUrl.equals(uri)) {
return false;
}
}
return true;
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
}
.通过注解方式配置过滤器:
@EnableAutoConfiguration
@ComponentScan(basePackages = "XXXXXXXXXXX项目总包名")
@MapperScan(basePackages = "XXXXXXXXXXXX包名.mapper")
@SpringBootApplication
@ServletComponentScan
public class ResearchApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(ResearchApplication.class, args);
}
}
好了,代码就这么多,大家可以根据自己的需求来设置自己要过滤的url了,
如果有问题请及时留言,我会第一时间反馈,也可添加我的qq:983469079