文章目录
写在前面~~~之前项目的登陆管理系统做的比较简陋,只是自定义一个Auth注解,然后在需要登陆的接口上添加注解,通过拦截器判断cookie来判断是否登陆,权限的判断需要在Controller内自己code判断,大概示例如下:
- Auth注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Auth {
}
- 拦截器
import java.lang.reflect.Method;
import java.net.URI;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@Aspect
@Component
public class MyInterceptor {
public static final String EXEC = "execution(* com.demo.controller.*.*(..))";
@Pointcut(EXEC)
public void interceptor() {
}
@Around("interceptor()")
public Object doAround(ProceedingJoinPoint point) throws Throwable {
logger.info("===============进入权限拦截器===============");
// 配置文件自己配置的打开首页的ip
String bastPath = sysProp.getBasePath();
// 判断请求的方法上是否有Auth注解,如果没有,此类请求无需拦截,直接放行
Method method = ((MethodSignature) point.getSignature()).getMethod();
Auth auth = method.getAnnotation(Auth.class);
if (auth == null) {
logger.debug("{} 未配置LoginAuth Annotation", method.getName());
return point.proceed();
}
//后续既然有注解,最低的都需要做个登录验证,是否有cookie,以及referer校验
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes()).getRequest();
HttpSession session = request.getSession();
String referer = request.getHeader("referer");
String userAgent = request.getHeader("User-Agent");
logger.debug("浏览器标识={}", userAgent);
if (userAgent.contains("Mozilla/4.0"