1.过滤器用的Interceptor因为idea中自带的拦截器
ReaderInterceptor
import request from '@/utils/request'
export function fetchList(query) {
return request({
url: '/vue-element-admin/article/list',
method: 'get',
params: query
})
}
export function fetchArticle(id) {
return request({
url: '/vue-element-admin/article/detail',
method: 'get',
params: { id }
})
}
export function fetchPv(pv) {
return request({
url: '/vue-element-admin/article/pv',
method: 'get',
params: { pv }
})
}
export function createArticle(data) {
return request({
url: '/vue-element-admin/article/create',
method: 'post',
data
})
}
export function updateArticle(data) {
return request({
url: '/vue-element-admin/article/update',
method: 'post',
data
})
}
UserInterceptor
package com.rabbiter.bms.interceptor;
import com.rabbiter.bms.model.User;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// 用户拦截器,拦截未登录不能访问的请求
public class UserInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
User user = (User) request.getSession().getAttribute("userObj");
if(user == null) { //没有登录
System.out.println("没有登录!不能访问!");
// 重定向到登录界面
response.sendRedirect(request.getContextPath() + "/index.html");
return false;
}
return true; //放行
}
}
接口
用户管理接口:
@RequestMapping(value = "/info")
public Map<String, Object> info(String token) {
// 从redis中取用户
User user = userService.getUser(token);
if(user == null) { // 获取失败
return MyResult.getResultMap(420, "获取用户信息失败");
} else { // 获取成功
return MyResult.getResultMap(200, "获取用户信息成功", user);
}
}
// 退出登录
@RequestMapping(value = "/logout")
public Map<String, Object> logout(String token) {
// 从redis中移除用户
userService.removeUser(token);
return MyResult.getResultMap(200, "退出登录成功" );
}
// 修改密码
@RequestMapping(value = {"/alterPassword", "reader/alterPassword"})
public Integer alterPassword(Integer userid, String username, Byte isadmin, String oldPassword, String newPassword){
//检查旧密码是否正确
User userObj = new User();
userObj.setUserid(userid);
userObj.setUsername(username);
userObj.setUserpassword(oldPassword);
userObj.setIsadmin(isadmin);
User user = userService.login(userObj);
if(user == null) { //旧密码不正确
return 0;
} else { //旧密码正确,设置新密码
userService.setPassword(userObj.getUserid(), newPassword);
return 1;
}
}
// 获得数量
@GetMapping(value = "/getCount")
public Integer getCount(){
return userService.getCount();
}
// 查询所有用户
@GetMapping(value = "/queryUsers")
public List<User> queryUsers(){
return userService.queryUsers();
}
// 分页查询用户 params: {page, limit, username}
@GetMapping(value = "/queryUsersByPage")
public Map<String, Object> queryUsersByPage(@RequestParam Map<String, Object> params){
MyUtils.parsePageParams(params);
int count = userService.getSearchCount(params);
List<User> users = userService.searchUsersByPage(params);
return MyResult.getListResultMap(0, "success", count, users);
}
// 添加用户
@PostMapping(value = "/addUser")
public Integer addUser(@RequestBody User user){
return userService.addUser(user);
}
// 删除用户
@DeleteMapping(value = "/deleteUser")
public Integer deleteUser(@RequestBody User user){
return userService.deleteUser(user);
}
// 删除一些用户
@DeleteMapping(value = "/deleteUsers")
public Integer deleteUsers(@RequestBody List<User> users){
return userService.deleteUsers(users);
}
// 更新用户
@RequestMapping(value = "/updateUser")
public Integer updateUser(@RequestBody User user){
return userService.updateUser(user);
}
拦截作用:
没用登录不可直接打开后续界面