一、实现的功能
类似于下面功能的拦截器
自定义拦截器的步骤
第一步:自定义一个实现了Interceptor接口的类,或者继承抽象类AbstractInterceptor。
第二步:在配置文件中注册定义的拦截器。
第三步:在需要使用Action中引用上述定义的拦截器,为了方便也可以将拦截器定义为默认的拦截器,这样在不加特殊说明的情况下,所有的
Action都被这个拦截器拦截。
二、写我们的项目功能
controller层
UserController.java
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
@Autowired
private HttpSession session;
@RequestMapping(value = "login")
public Map<String,Object> login(User user){
Map<String,Object> map = new HashMap<>();
map.put("code",0);
if(StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword()) ){
map.put("msg","用户或者密码为空!");
return map;
}
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("username",user.getUsername())
.eq("password",user.getPassword());
User userDb = userService.getOne(queryWrapper);
if(userDb != null){
map.put("code",1);
map.put("data",userDb);
session.setAttribute("username",userDb.getUsername());
}else{
map.put("msg","用户名或密码错误!");
}
return map;
}
}
UserService.java
public interface UserService extends IService<User> {
List<User> findAll();
void transfer();
List<User> listUser(String username);
List<User> listUser(String username,String password);
}
UserServiceImpl.java
@Transactional
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
@Resource
private UserMapper userMapper;
@Override
public List<User> findAll(){
return userMapper.findAll();
}
@Override
public List<User> listUser(String username) {
return userMapper.listUser(username);
}
@Override
public List<User> listUser(String username, String password) {
return userMapper.listUser(username,password);
}
}
UserMapper.java
public interface UserMapper extends BaseMapper<User> {
List<User> findAll();
List<User> listUser(@Param("username")String username);
List<User> listUser(@Param("username")String username,@Param("password")String password);
}
UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--名称空间:对应mapper层某个接口的包的全名称-->
<!-- namespace + 标签Id :
key: com.example.demo.mapper.UserMapper.listUser value: MappedStatement实例-->
<mapper namespace="com.example.demo.mapper.UserMapper">
<select id="findAll" resultType="com.example.demo.entity.User">
select * from user
</select>
<select id="listUser" resultType="com.example.demo.entity.User">
select * from user where username = #{username} and password = #{password}
</select>
</mapper>
_____________________________________________________________________________
重点来了
interceptor包下面的LoginInterceptor.java
public class LoginInterceptor implements HandlerInterceptor {
@Autowired
private HttpSession httpSession;
//Controller逻辑执行之前
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle....");
String uri = request.getRequestURI();
System.out.println("当前路径:"+uri);
/**
* HandlerMethod=>Controller中标注@RequestMapping的方法
* 需要配置静态资源不拦截时,添加这块逻辑 => 前后端分离项目
*
*/
// 是我们的conrtoller中的方法
if (!(handler instanceof HandlerMethod)) {
return true;
}
if (httpSession.getAttribute("username") == null) {
// 未登录跳转到登录界面
throw new RuntimeException("no login!");
} else {
return true;
}
}
//Controller逻辑执行完毕但是视图解析器还未进行解析之前
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle....");
}
//Controller逻辑和视图解析器执行完毕
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
System.out.println("afterCompletion....");
}
}
在config包下面建立 WebMvcConfig .java
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginInterceptor())
// 拦截规则 ,拦截那些路径
.addPathPatterns("/**")
// 那些路径不拦截
.excludePathPatterns("/user/login","/error");
}
@Bean
public LoginInterceptor loginInterceptor(){
return new LoginInterceptor();
}
}
这就是集成好的拦截器
三、运行
咱们的http://localhost:8080/访问不进去(拦截器拦截成功)
http://localhost:8080/user/login?username=admin&password=123456可以访问进去
这是因为有个我在HelloWordController.java下面有个没有路径的输出hello word
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/80126.html