登录
将登陆的自定义注解@LoginRquire标注在方法上,表示该方法需要登陆才能够调用。
1、自定义注解
/**
* @Description: 自定义注解-登录
* @Author: yufengpeng
* @CreateDate: 2019/9/16 20:03
* * @Target : 用于描述注解的使用范围
* * 也就是说使用了@Target定义一个注解,那么可以决定定义好的注解可以使用在什么地方
* * @Retention : 用于描述注解的生命周期
* * 也就是说这个注解在什么范围内有效,注解的生命周期和三个阶段有关:源码阶段,CLASS文件有效,运行时有效
* * @Documented : 表示该注解是否可以使用到API文档中
* * 在该注解使用后,如果导出API文档,则会将该注解相关的信息可以被例如javadoc此类的工具文档化。注解:Documented是一个标记注解,没有成员。
* * @Inherited : 表示具备继承性
* * 假设一个注解在定义时,使用了@Inherited,然后该注解在一个类上使用,如果这个类有子类,那么我们可以通过反射从类的子类上获取到同样的注解
*/
@Target(ElementType.METHOD, ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface LoginRequired {
}++
2、拦截器
/**
* 拦截器
*/
@Component
public class AuthWebShopInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
// 如果不是映射到方法直接通过
if (!(handler instanceof HandlerMethod)) {
return true;
}
HandlerMethod myHandlerMethod = (HandlerMethod) handler;
Object bean = myHandlerMethod.getBean();
Method method= myHandlerMethod.getMethod();
// 判断接口是否需要登录
//类上有该标记
Annotation classAnnotation = bean.getClass().getAnnotation(LoginRequired.class);
//方法上有该标记
Annotation methodAnnotation = method.getAnnotation(LoginRequired.class);
// 有 @LoginRequired 注解,需要认证
if (methodAnnotation != null || classAnnotation != null) {
String token = LoginUtils.getToken(request);
if (StringUtils.isBlank(token)) {
//未登录
redirectToLoginPageAjax(request, response);
return false;
}else {
ProfileVO profileVO = ApplicationContextUtil.getBean(LoginService.class).getUser();
if (null == profileVO){
//未登录
redirectToLoginPageAjax(request, response);
return false;
}
}
//已经在登录状态
return true;
}
//不需要登录验证
return true;
}
/**
* 在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)
**/
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
}
private void redirectToLoginPageAjax(HttpServletRequest request, HttpServletResponse response) {
try {
OutputStream outs = response.getOutputStream();
response.setStatus(HttpStatus.SC_UNAUTHORIZED);
response.setContentType("application/json;charset=UTF-8");
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(JsonReturn.fail(ResponseCode.UNAUTHORIZED,null, "请登录"));
outs.write(json.getBytes(StandardCharsets.UTF_8));
outs.flush();
} catch (IOException e) {
LogUtils.error(LogAction.ERROR, e.getMessage());
}
}
}
方法执行成功后添加日志
1、添加日志自定义注解
/**
* @author yufengpeng
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface ManagementLog {
String module() default "";
String content() default "";
}
2、拦截器
/**
* 后台日志管理AOP切面类
*
* @author yufengpeng
*/
@Aspect
@Component
public class ManagementLogAspect {
@Autowired
private UserService userService;
@Autowired
private SettingLogDao settingLogDao;
@Pointcut(value = "@annotation(com.yufengpeng.business.management.aspect.ManagementLog)")
public void pointcut(){
}
/**
* 方法成功执行后新增日志
*
* @param joinPoint
*/
@AfterReturning(pointcut = "pointcut()")
public void afterReturning(JoinPoint joinPoint){
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
ManagementLog logAnnotation = method.getAnnotation(ManagementLog.class);
SettingLog log = new SettingLog();
log.setCreateTime(System.currentTimeMillis());
log.setIp(IpUtils.getIp());
log.setModuleName(logAnnotation.module());
log.setContent(logAnnotation.content());
// 获取当前登录账号
UserVO user = userService.getUser();
if (null != user) {
log.setAccount(user.getAccount());
log.setRoleId(user.getRoleId());
}
settingLogDao.insert(log);
}
}