maven项目中的过滤器的实现以及用户管理
接着上一篇博客内容进行编程
-
过滤器的实现
-
在web.xml中配置过滤器
<filter> <filter-name>loginfilter</filter-name> <filter-class>com.frist.filter.LoginFilter</filter-class> </filter> <!--过滤器映射--> <filter-mapping> <filter-name>loginfilter</filter-name> <!--对所以后缀名为.do的路进进行过滤--> <url-pattern>*.do</url-pattern> </filter-mapping>
-
实现过滤器类
在first目录下,创建filter目录,并在filter目录下创建LoginFilter过滤器类
package com.frist.filter; import com.frist.bean.User; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; public class LoginFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("login过滤器初始化"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("login过滤器执行"); //过滤器的放行 HttpServletRequest request=(HttpServletRequest)servletRequest; HttpServletResponse response=(HttpServletResponse)servletResponse; HttpSession session=request.getSession(); User user=(User)session.getAttribute("user"); String url=request.getRequestURI();//得到request中的路径 if(user==null && url.indexOf("login.do")==-1){ System.out.println("没有登陆,请用户进行登陆"); //跳转至登陆界面 response.sendRedirect(request.getContextPath()+"/page/login.jsp"); } else{ System.out.println("登陆信息已存在"); //过滤器放行 filterChain.doFilter(servletRequest,servletResponse); } } @Override public void destroy() { System.out.println("取消login过滤"); } }
-
修改请求路径
- 在controller层中将所有的@RequestMapping("/X")改成@RequestMapping("/X.do")
- 将所有.jsp文件中 /user/x 改为 /user/x.do
- 让过滤器类进行识别
-
运行项目进行测试
当直接输入url为http://localhost:8080/user/findBypage.do?currentPage=1,而没有进行用户登录的时候不会显示用户列表,而是直接进入登陆界面,因为没有登陆,提醒用户进行登陆。
-
-
用户管理的实现
- 创建两张数据库表t_role和t_userrole
-
在Dao层创建Role和UserRole类
Role
public class Role { private int id; private String rolename; private String roledesc; public Role() { } public Role(int id, String rolename, String roledesc) { this.id = id; this.rolename = rolename; this.roledesc = roledesc; } //略 }
UserRole
package com.frist.bean; public class UserRole { private int id; private int userId; private int roleId; public UserRole() { } public UserRole(int id, int userId, int roleId) { this.id = id; this.userId = userId; this.roleId = roleId; } //略 }
-
修改controller层中login,并在UserHandler中注入RoleService roleService
@AutowiredRoleService roleService;
public ModelAndView login(String username, String password, HttpSession session){ System.out.println("响应/user/login请求"); // 对 用户名和密码 进行 校验 User user=userService.login(new User(username, password)); if (user == null) { System.out.println("登录失败"); ModelAndView mv=new ModelAndView(); mv.setViewName("page/failer"); return mv; } else { System.out.println("登录成功"); session.setAttribute("user", user); //利用session存储用户角色信息 List<Integer> list = roleService.findRoleId(user.getId()); session.setAttribute("roleIds",list); ModelAndView mv=new ModelAndView(); mv.setViewName("page/success"); return mv; } }
-
在Service层中创建接口RoleService,在impl目录下创建实现类RoleServiceImpl
RoleService
package com.frist.service; import com.frist.bean.Role; import java.util.List; public interface RoleService { List<Integer> findRoleId(int userId); }
RoleServiceImpl
package com.frist.service.impl; import com.frist.bean.Role; import com.frist.bean.UserRole; import com.frist.dao.RoleDao; import com.frist.service.RoleService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class RoleServiceImpl implements RoleService { @Autowired RoleDao roleDao; @Override public List<Integer> findRoleId(int userId) { <