结合:https://blog.csdn.net/Anenan/article/details/89354244
定义不需要登陆注解
/**
* 不需登录
*
* @author 向振华
* @date 2019/04/17 14:22
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.METHOD, ElementType.TYPE})
@Documented
public @interface Unlogin {
}
改造登录拦截器
/**
* 登录拦截器
*
* @author 向振华
* @date 2019/04/17 13:42
*/
public class LoginInterceptor extends BaseInterceptor {
@Resource
private LoginService loginService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
// 获取出方法上的Unlogin 注解
Unlogin unlogin = method.getAnnotation(Unlogin.class);
if (unlogin != null) {
// 如果注解不为null, 说明不需要登录, 直接放过
return true;
}
//获取当前用户
User user = loginService.currentUser();
if (user == null) {
//前端固定格式
ResponseMessage responseMessage = new ResponseMessage<>(-1, "请登录!");
String jsonString = JSON.toJSONString(responseMessage);
sendMsg(response, jsonString);
return false;
}
return true;
}
}
拦截器配置
/**
* 拦截器配置
*
* @author 向振华
* @date 2019/04/17 13:42
*/
@Configuration
@EnableTransactionManagement
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter {
@Bean
LoginInterceptor loginInterceptor() {
return new LoginInterceptor();
}
/**
* 拦截器配置
*
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
//配置登录拦截器
registry.addInterceptor(loginInterceptor()).addPathPatterns("/**");
super.addInterceptors(registry);
}
}
在Controller上的方法加入注解即可不需登录访问
@Unlogin
@Override
public ResponseMessage<Boolean> isRegistered(MobileReq req) {
return loginService.isRegistered(req);
}
定义会员注解
/**
* 会员
*
* @author 向振华
* @date 2019/04/17 13:56
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.METHOD, ElementType.TYPE})
@Documented
public @interface Member {
}
会员拦截器
/**
* 会员拦截器
*
* @author 向振华
* @date 2019/04/17 13:58
*/
public class MemberInterceptor extends BaseInterceptor {
@Resource
private LoginService loginService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
// 获取出方法上的Member注解
Member member = method.getAnnotation(Member.class);
if (member == null) {
// 如果注解为null, 说明不需要拦截, 直接放过
return true;
}
//获取当前用户
User user = loginService.currentUser();
//判断是否是会员
if (user.getIsMember() == false) {
//前端固定格式
ResponseMessage responseMessage = new ResponseMessage<>(-1, "请开通会员!");
String jsonString = JSON.toJSONString(responseMessage);
sendMsg(response, jsonString);
return false;
}
return true;
}
}
拦截器配置
/**
* 拦截器配置
*
* @author 向振华
* @date 2019/04/17 13:42
*/
@Configuration
@EnableTransactionManagement
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter {
@Bean
LoginInterceptor loginInterceptor() {
return new LoginInterceptor();
}
@Bean
MemberInterceptor memberInterceptor(){
return new MemberInterceptor();
}
/**
* 拦截器配置
*
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
//配置登录拦截器
registry.addInterceptor(loginInterceptor()).addPathPatterns("/**");
//配置会员拦截器
registry.addInterceptor(memberInterceptor()).addPathPatterns("/**");
super.addInterceptors(registry);
}
}
在Controller上的方法加入注解即可限制非会员访问