提示:过滤器在未登录前拦截主页里面内容
过滤器+新增+分页+状态+编辑
例如:
filter.LoginCheckFilter.java
@Slf4j
@WebFilter(filterName = "LoginCheckFilter",urlPatterns = "/*")
public class LoginCheckFilter implements Filter {
//路径通配符
public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response =(HttpServletResponse) servletResponse;
//过滤器具体的处理逻辑如下:
//1、获取本次请求的URI
String requestUrl = request.getRequestURI();
log.info("本次请求的URI{}",requestUrl);
//写出不用处理的请求
String[] urls = new String[]{
"/employee/login",
"/employee/logout",
"/backend/**",
"/front/**"
};
//2、判断本次请求是否需要处理
boolean check = check(urls,requestUrl);
//3、如果不需要处理,则直接放行
if (check){
log.info("拦截到请求{},不需要处理",request.getRequestURI());
filterChain.doFilter(request,response);
return;
}
//4、判断登录状态,如果已登录,则直接放行
if (request.getSession().getAttribute("employee")!=null){
log.info("用户已登录,用户id为{}",request.getSession().getAttribute("employee"));
filterChain.doFilter(request,response);
return;
}
//5、如果未登录则返回未登录结果
log.info("用户未登录");
response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
}
//路径匹配
public boolean check(String[] urls,String requestUrl){
for (String url: urls) {
boolean match = PATH_MATCHER.match(url,requestUrl);
if (match==true){
return true;
}
}
return false;
}
}
在里面穿插日志进行测试
//新增员工
@PostMapping
public R<String> save(@RequestBody Employee employee,HttpServletRequest request){
//1接收到前端页面传送过来的数据
log.info("1接收到前端页面传送过来的数据{}",employee);
//2 密码默认为123456用MD5算法加密
employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
log.info(" 密码===>{}",employee.getPassword());
//3 创建时间和更新时间自动生成
employee.setCreateTime(LocalDateTime.now());
employee.setUpdateTime(LocalDateTime.now());
log.info(" 创建时间===>{}",employee.getCreateTime());
log.info(" 更新时间===>{}",employee.getUpdateTime());
//4 添加人和更新人从缓存里面取id
log.info("---------------->{}",request.getSession().getAttribute("employee"));
Long empId = (Long) request.getSession().getAttribute("employee");
log.info("empId=====>{}", empId);
employee.setCreateUser(empId);
employee.setUpdateUser(empId);
employeeService.save(employee);
return R.success("新增员工成功");
}
此处错误在于登录时存入缓存的数据是employee对象,没有办法强制转化成long类型,在登录出直接将id存入缓存
//6、登录成功,将用户id存入Session并返回成功结果
request.getSession().setAttribute("employee",emp.getId());
数据库中对username做了唯一处理,在前台输入用户名和后面一样时要抛出异常,两种方式
1 try catch
2用异常处理器进行全局异常捕获
1在common包中创建异常处理类GlobalExceptionHandler
@Slf4j
@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody
public class GlobalExceptionHandler {
@ExceptionHandler(SQLIntegrityConstraintViolationException.class)
public R<String> exceptionHandler(SQLIntegrityConstraintViolationException ex) {
log.info("报错信息{}",ex.getMessage());
if(ex.getMessage().contains("Duplicate entry")){
String [] split = ex.getMessage().split(" ");
String msg = split[2]+"已存在";
return R.error(msg);
}
return R.error("未知错误");
}
}
结果--------------------------------------
@Configuration
public class MyBatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
//2配置分页插件
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mybatisPlusInterceptor;
}
}
@GetMapping("/page")
public R<Page> page(int page,int pageSize,String name){
log.info("取到前端页面数据page-->{},pageSize--->{},name--->{}",page,pageSize,name);
//构造分页构造器
Page<Employee> pageInfo = new Page<>(page,pageSize);
//构造条件构造器
LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
//添加过滤条件,如果name=123并且name!=null时
wrapper.like(!StringUtils.isEmpty(name),Employee::getName,name);
//添加排序条件,按照更新时间进行排序
wrapper.orderByDesc(Employee::getUpdateTime);
//执行查询
employeeService.page(pageInfo,wrapper);
return R.success(pageInfo);
}
@PutMapping
public R<String> update(@RequestBody Employee employee,HttpServletRequest request){
log.info("前台取到的数据--------》{}",employee.toString());
//前台数据包含选择用户的id和状态
//根据id修改他的状态
//更新人要从缓存里面获取
Long empId =(Long)request.getSession().getAttribute("employee");
employee.setUpdateUser(empId);
//修改时间实时更新
employee.setUpdateTime(LocalDateTime.now());
employeeService.updateById(employee);
return R.success("更新用户状态成功");
}
我们可以在服务端给页面响应json数据时进行处理,将long型数据统一转为String字符串。
/**
* 对象映射器:基于jackson将Java对象转为json,或者将json转为Java对象
* 将JSON解析为Java对象的过程称为 [从JSON反序列化Java对象]
* 从Java对象生成JSON的过程称为 [序列化Java对象到JSON]
*/
public class JacksonObjectMapper extends ObjectMapper {
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
public JacksonObjectMapper() {
super();
//收到未知属性时不报异常
this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
//反序列化时,属性不存在的兼容处理
this.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
SimpleModule simpleModule = new SimpleModule()
.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)))
.addSerializer(BigInteger.class, ToStringSerializer.instance)
.addSerializer(Long.class, ToStringSerializer.instance)
.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
//注册功能模块 例如,可以添加自定义序列化器和反序列化器
this.registerModule(simpleModule);
}
}
@Override
protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
//创建消息转化器
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
//设置对象转化器,底层使用Jackson将Java转化成json
messageConverter.setObjectMapper(new JacksonObjectMapper());
//将上面的东西加到mvc框架的转换集中
converters.add(0,messageConverter);
super.extendMessageConverters(converters);
}
@GetMapping("/{id}")
public R<Employee> getById(@PathVariable Long id) {
log.info("获取的id---------->{}",id);
Employee employee = employeeService.getById(id);
if(employee!=null){
return R.success(employee);
}
return R.error("查询失败");
}