关于响应式布局(pc端访问自适应 手机端访问自适应)

不喜勿喷!!!

第一步 :

package com.sso.util;

public class HttpAgentUtil {
private final static String[] agent = { "Android", "iPhone", "iPod","iPad", "Windows Phone", "MQQBrowser" }; //定义移动端请求的所有可能类型
/**
* 判断User-Agent 是不是来自于手机
* @param ua
* @return
*/
public static boolean checkAgentIsMobile(String ua) {
boolean flag = false;
if (!ua.contains("Windows NT") || (ua.contains("Windows NT") && ua.contains("compatible; MSIE 9.0;"))) {
// 排除 苹果桌面系统
if (!ua.contains("Windows NT") && !ua.contains("Macintosh")) {
for (String item : agent) {
if (ua.contains(item)) {
flag = true;
break;
}
}
}
}
return flag;
}
}

 

 

第二步:

 

package com.sso.config;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;

import com.sso.entity.User;
import com.sso.util.HttpAgentUtil;

 

@Component
@WebFilter("/*")
public class LoginFiter implements Filter{
/*实例化一个静态的集合(这里可以改用map集合,提高性能,每次过滤可以不用for匹配)*/
private static List<String> urls = new ArrayList<>();
//静态代码块中向集合中存放所有可以放行的请求或网页地址(不用账号密码即可访问)
static {
urls.add("/login");
urls.add("/login.html");
urls.add("/css");
urls.add("/js");
urls.add("/register");
urls.add("/userRegister.html");
urls.add("forgetPassword.html");
urls.add("findpwd.jsp");
}

@Override
public void destroy() {
// TODO Auto-generated method stub

}

/**
* 拦截请求,看看用户是否登陆,如果没有登陆,
* 则跳转回登录页面,否则就放行
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
/*注意:ServletRequest是HttpServletRequest父接口,因此需要强转
     同样,ServletResponse是HttpServletResponse父接口,因此需要强转*/
HttpServletRequest req=(HttpServletRequest)request;
HttpServletResponse res=(HttpServletResponse)response;
String ua = req.getHeader("User-Agent");
/*得到当前页面所在目录下全名称*/
String urlPattern=req.getServletPath();
/*得到页面所在服务器的绝对路径*/
String path = req.getRequestURI();
for (String url : urls) {
if(url.equals(urlPattern) || path.contains(url)) {
chain.doFilter(request, response);
//防止重复响应
return;
}
}
//如果username为null,表示没有登录
if(req.getSession().getAttribute("username")==null) {
if(HttpAgentUtil.checkAgentIsMobile(ua)){
res.sendRedirect("/sso-client/mobile/public/login.html");
}else {
res.sendRedirect("/sso-client/login.jsp");
}
}else {
//放行
chain.doFilter(request, response);
}
}

@Override
public void init(FilterConfig arg0) throws ServletException {
}

}

第三步:

剩下的手机端页面直接请求后台功能即可

关于响应式布局(pc端访问自适应 手机端访问自适应)

上一篇:fiddle手机抓包


下一篇:09 jQuery事件委托&小米购物车