拦截器没有反应

拦截器没有反应

# 具体遇到的问题
最开始我自己写了拦截器,但没有任何作用。后来在使用了老师的原版文件之后,拦截器也没有任何反应。启动服务器之后直接就可以使用delete接口,不用登录。

由于没有报错信息,直接猜测错误应该会比较困难,想问问老师有可能是什么问题
# 报错信息的截图
http://img1.sycdn.imooc.com//climg/5fe1caeb090ad78616240932.jpg

# 相关课程内容截图

# 尝试过的解决思路和结果

# 粘贴全部相关代码,切记添加代码注释(请勿截图)

package com.imooc.mall.config;

import com.imooc.mall.filter.AdminFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* 描述: Admin过滤器的配置
*/
@Configuration
public class AdminFilterConfig {

@Bean
public AdminFilter adminFilter() {
return new AdminFilter();
}

@Bean(name = "adminFilterConf")
public FilterRegistrationBean adminFilterConfig() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(adminFilter());
filterRegistrationBean.addUrlPatterns("/admin/category/*");
filterRegistrationBean.addUrlPatterns("/admin/product/*");
filterRegistrationBean.addUrlPatterns("/admin/order/*");
filterRegistrationBean.setName("adminFilterConf");
return filterRegistrationBean;
}
}
package com.imooc.mall.filter;

import com.imooc.mall.common.ApiRestResponse;
import com.imooc.mall.common.Constant;
import com.imooc.mall.exception.ImoocMallExceptionEnum;
import com.imooc.mall.model.pojo.Category;
import com.imooc.mall.model.pojo.User;
import com.imooc.mall.service.UserService;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.servlet.http.HttpSession;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
* 描述: 管理员校验过滤器
*/
public class AdminFilter implements Filter {

@Autowired
UserService userService;

@Override
public void init(FilterConfig filterConfig) throws ServletException {

}

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpSession session = request.getSession();
User currentUser = (User) session.getAttribute(Constant.IMOOC_MALL_USER);
if (currentUser == null) {
PrintWriter out = new HttpServletResponseWrapper(
(HttpServletResponse) servletResponse).getWriter();
out.write("{\n"
+ " \"status\": 10007,\n"
+ " \"msg\": \"NEED_LOGIN\",\n"
+ " \"data\": null\n"
+ "}");
out.flush();
out.close();
return;
}
//校验是否是管理员
boolean adminRole = userService.checkAdminRole(currentUser);
if (adminRole) {
filterChain.doFilter(servletRequest, servletResponse);
} else {
PrintWriter out = new HttpServletResponseWrapper(
(HttpServletResponse) servletResponse).getWriter();
out.write("{\n"
+ " \"status\": 10009,\n"
+ " \"msg\": \"NEED_ADMIN\",\n"
+ " \"data\": null\n"
+ "}");
out.flush();
out.close();
}
}

@Override
public void destroy() {

}
}
package com.imooc.mall.controller;

import com.imooc.mall.common.ApiRestResponse;
import com.imooc.mall.common.Constant;
import com.imooc.mall.exception.ImoocMallExceptionEnum;
import com.imooc.mall.model.pojo.Category;
import com.imooc.mall.model.pojo.User;
import com.imooc.mall.model.request.AddCategoryReq;
import com.imooc.mall.model.request.UpdateCategoryReq;
import com.imooc.mall.service.CategoryService;
import com.imooc.mall.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

@Controller
public class CategoryController {
@Autowired
UserService userService;
@Autowired
CategoryService categoryService;

@ApiOperation("后台添加商品分类")
@PostMapping("admin/category/add")
@ResponseBody
public ApiRestResponse addCategory(HttpSession session, @Valid @RequestBody AddCategoryReq addCategoryReq) {
if (addCategoryReq.getName() == null) {
return ApiRestResponse.error(ImoocMallExceptionEnum.NAME_NOTNULL);
}
User currentUser = (User) session.getAttribute(Constant.IMOOC_MALL_USER);
if (currentUser == null) {
return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_LOGIN);
}
boolean adminRole = userService.checkAdminRole(currentUser);
if (adminRole) {
categoryService.add(addCategoryReq);
return ApiRestResponse.success();
}else{
return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_ADMIN);
}
}

@PostMapping("admin/category/update")
@ResponseBody
public ApiRestResponse updateCategory(@Valid @RequestBody UpdateCategoryReq updateCategoryReq, HttpSession session) {
User currentUser = (User) session.getAttribute(Constant.IMOOC_MALL_USER);
if (currentUser == null) {
return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_LOGIN);
}
boolean adminRole = userService.checkAdminRole(currentUser);
if (adminRole) {
Category category = new Category();
BeanUtils.copyProperties(updateCategoryReq, category);
categoryService.update(category);
return ApiRestResponse.success();
}else{
return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_ADMIN);
}
}

@ApiOperation("后台删除目录")
@PostMapping("admin/category/delete")
@ResponseBody
public ApiRestResponse deleteCategory(){
return null;
}
}


正在回答

登陆购买课程后可参与讨论,去登陆

2回答

同学你好,测试同学的代码,执行delete()方法时,会被拦截,提示需要登录。如果同学添加和修改方法执行时会被拦截,说明拦截器没有问题。这里建议同学编译一下,重启再试试,如:

http://img1.sycdn.imooc.com//climg/5fe2a51e09db4df803900154.jpg

如果同学打了断点,需要使用debug执行,同学是否使用的debug执行?

另外问一下同学执行delete()方法时,是否已经使用了管理员账号登录,如果使用管理员账号登录,将不会被拦截,而是继续执行相应的方法。

祝学习愉快~


好帮手慕阿满 2020-12-22 19:17:07

同学你好,问一下同学添加和修改时,不登录可以执行对应的add()和update()方法吗?

建议同学在AdminFilter 类中打上断点,查看是否执行过滤器,如果执行过滤器,查看获取当前登录对象是否为null。

祝学习愉快~

  • 提问者 慕仰7036876 #1

    有作用是因为add方法写了校验代码,在拦截器中打了断点,但感觉拦截器跟没写一样不知道为什么没有作用。

    2020-12-22 20:10:43
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师