基于SpringBoot简单实现SpringAop+Redis+cookie 单点登录 和 用户登录检测

一、新建一个Ascept(class )

/**
 * 测试: Spring Aop 切面编程
 * @author Administrator
 *
 */
@Aspect
@EnableAspectJAutoProxy
@Configuration
public class TestWebAspectConfig {
	private CookieUtil cookieUtil = new CookieUtil();
    //自定义切面规则
	@Pointcut("execution(* com.qingsong.controler..*.user*(..))")
    /**
        第一个*是切点方法的返回值类型、第二个*是controller包下所有的controller、后面是controller里以user开头的所有方法(无论方法里面有没有参数)
    **/
	public void aspectService(){
		
	}
	@Before(value="aspectService()")
	public void before()throws ServletException, IOException{
	//获取request、response、session
		ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = requestAttributes.getRequest();
		HttpServletResponse response = requestAttributes.getResponse();
		boolean ok = CookieUtil.checkCookie(request);
		System.out.println(ok);
		if (!ok) {
			response.sendRedirect("/redis/index");//转发到登录界面
		}
		System.out.println("before");
	}
	@AfterReturning(returning="result",pointcut="aspectService()")
	public void after(Object result)  {
	
		System.out.println("result="+result);
		System.out.println("after");
	}
}

上面基本上是实现了用户登录检测功能,原理就是在用户访问的时候过aop,aop检测时需要用到用户请求时的request,这时使用  RequestContextHolder 获取   RequestContextHolder.getRequestAttributes().getRequest();(想要了解RequestContextHolder,建议了解一下SpringMVC架构)

下面是我自己定义的Cookie工具类

/**
 * cookie 工具类  功能: 减少重复代码,获取cookie、删除cookie
 * @author Administrator
 *
 */
public class CookieUtil {
	/**
	 * 返回一个可用的 cookie
	 * @param request
	 * @param response
	 * @return
	 */
	public static Cookie getCookie(HttpServletRequest request,HttpServletResponse response) {
		Cookie cookie = null;
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for(Cookie ck : cookies){
				if (ck.getName().equals("JSESSIONID2")) {
					cookie = ck;
				}
			}
		}
		if (cookie == null) {
			HttpSession session = request.getSession();
			if (session == null) {
				session = request.getSession(true);
			}
			String id = session.getId();
			cookie =new Cookie("JSESSIONID2", id);
		}
		cookie.setPath("/");
		cookie.setMaxAge(3600*1);
		cookie.setDomain("niao.com");
		cookie.setHttpOnly(true);
		return cookie;
	}
	/**
	 * 删除cookie
	 * @param request
	 * @param response
	 * @return
	 */
	public static boolean delCookie(HttpServletRequest request,HttpServletResponse response) {
		boolean DELCOOKIEOK = false; 
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals("JSESSIONID2")) { //删除cookie
					cookie.setPath("/");
					cookie.setMaxAge(0);
					cookie.setDomain("niao.com");
					cookie.setHttpOnly(true);
					response.addCookie(cookie);
					//删除redis中 键值对
					System.out.println("注销成功");
					DELCOOKIEOK = true;
				}
			}
		}else {
			DELCOOKIEOK = true;
		}
		return DELCOOKIEOK;
	}
	public static boolean checkCookie(HttpServletRequest request){
		boolean CHECKCOOKIE = false;
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals("JSESSIONID2")) { //删除cookie
					CHECKCOOKIE = true;
				}
			}
		}
		return CHECKCOOKIE;
	}
}

我这里没有使用基于 SpringBoot 的 redis 自己写了一个简易的

/**
 * 单机部署Redis 在用户登录界面
 * @author Administrator
 *
 */
public class RedisClientUtil {
	/**
	 * 
	 */
	private static JedisPool pool = null; //并发
	private static int REDIS_CLIENT_COUNT = 0;
	private static Jedis jedis = null; //单点
	static{
		jedis = new Jedis("127.0.0.1", 6379);
	}
	/**
	 * 设置key值
	 */
	public static void setRedis(String jsessionId,String value) {
		if (jedis != null) {
			
			String a=jedis.set(jsessionId,value );
			
			System.out.println("REDIS_CLIENT_COUNT = "+a);
		}
	}
	/**
	 * 获取key值
	 * @param key
	 * @return
	 */
	public static String getRedisValue(String key){
		String value = null;
		if (jedis != null) {
			value = jedis.get(key);
		}
		return value;
	}
	public static void delRedisValue(String key){
		if (jedis != null) {
			int a = Integer.parseInt(jedis.del(key).toString());
			System.out.println(a);
		}
	}
}

上面的功能有好多实现方法(shiro、CAS、redis+filter ...),希望这篇对大家有帮助

你可能感兴趣的:(基于SpringBoot简单实现SpringAop+Redis+cookie 单点登录 和 用户登录检测)