spring boot 判断请求是不是android

小唯快跑啊
浏览 1348回答 1
1回答

翻翻过去那场雪

public class LiteDeviceResolver implements DeviceResolver {&nbsp;&nbsp; &nbsp; private final List<String> mobileUserAgentPrefixes = new ArrayList<String>();&nbsp;&nbsp; &nbsp; private final List<String> mobileUserAgentKeywords = new ArrayList<String>();&nbsp;&nbsp; &nbsp; private final List<String> tabletUserAgentKeywords = new ArrayList<String>();&nbsp;&nbsp; &nbsp; private final List<String> normalUserAgentKeywords = new ArrayList<String>();&nbsp;&nbsp; &nbsp; public LiteDeviceResolver() {&nbsp; &nbsp; &nbsp; &nbsp; init();&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; public LiteDeviceResolver(List<String> normalUserAgentKeywords) {&nbsp; &nbsp; &nbsp; &nbsp; init();&nbsp; &nbsp; &nbsp; &nbsp; this.normalUserAgentKeywords.addAll(normalUserAgentKeywords);&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; public Device resolveDevice(HttpServletRequest request) {&nbsp; &nbsp; &nbsp; &nbsp; String userAgent = request.getHeader("User-Agent");&nbsp; &nbsp; &nbsp; &nbsp; // UserAgent keyword detection of Normal devices&nbsp; &nbsp; &nbsp; &nbsp; if (userAgent != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; userAgent = userAgent.toLowerCase();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (String keyword : normalUserAgentKeywords) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (userAgent.contains(keyword)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return resolveFallback(request);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // UAProf detection&nbsp; &nbsp; &nbsp; &nbsp; if (request.getHeader("x-wap-profile") != null || request.getHeader("Profile") != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return LiteDevice.MOBILE_INSTANCE;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // User-Agent prefix detection&nbsp; &nbsp; &nbsp; &nbsp; if (userAgent != null && userAgent.length() >= 4) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String prefix = userAgent.substring(0, 4).toLowerCase();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (mobileUserAgentPrefixes.contains(prefix)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return LiteDevice.MOBILE_INSTANCE;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Accept-header based detection&nbsp; &nbsp; &nbsp; &nbsp; String accept = request.getHeader("Accept");&nbsp; &nbsp; &nbsp; &nbsp; if (accept != null && accept.contains("wap")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return LiteDevice.MOBILE_INSTANCE;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // UserAgent keyword detection for Mobile and Tablet devices&nbsp; &nbsp; &nbsp; &nbsp; if (userAgent != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; userAgent = userAgent.toLowerCase();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Android special case&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (userAgent.contains("android") && !userAgent.contains("mobile")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return LiteDevice.TABLET_INSTANCE;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Kindle Fire special case&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (userAgent.contains("silk") && !userAgent.contains("mobile")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return LiteDevice.TABLET_INSTANCE;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (String keyword : tabletUserAgentKeywords) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (userAgent.contains(keyword)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return LiteDevice.TABLET_INSTANCE;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (String keyword : mobileUserAgentKeywords) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (userAgent.contains(keyword)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return LiteDevice.MOBILE_INSTANCE;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // OperaMini special case&nbsp; &nbsp; &nbsp; &nbsp; @SuppressWarnings("rawtypes")&nbsp; &nbsp; &nbsp; &nbsp; Enumeration headers = request.getHeaderNames();&nbsp; &nbsp; &nbsp; &nbsp; while (headers.hasMoreElements()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String header = (String) headers.nextElement();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (header.contains("OperaMini")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return LiteDevice.MOBILE_INSTANCE;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return resolveFallback(request);&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; // subclassing hooks&nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* List of user agent prefixes that identify mobile devices.&nbsp; &nbsp; &nbsp;* Used primarily to match by operator or handset manufacturer.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected List<String> getMobileUserAgentPrefixes() {&nbsp; &nbsp; &nbsp; &nbsp; return mobileUserAgentPrefixes;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* List of user agent keywords that identify mobile devices.&nbsp; &nbsp; &nbsp;* Used primarily to match by mobile platform or operating system.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected List<String> getMobileUserAgentKeywords() {&nbsp; &nbsp; &nbsp; &nbsp; return mobileUserAgentKeywords;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* List of user agent keywords that identify tablet devices.&nbsp; &nbsp; &nbsp;* Used primarily to match by tablet platform or operating system.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected List<String> getTabletUserAgentKeywords() {&nbsp; &nbsp; &nbsp; &nbsp; return tabletUserAgentKeywords;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* List of user agent keywords that identify normal devices.&nbsp; &nbsp; &nbsp;* Any items in this list take precedence over the mobile and&nbsp; &nbsp; &nbsp;* tablet user agent keywords, effectively overriding those.&nbsp;&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected List<String> getNormalUserAgentKeywords() {&nbsp; &nbsp; &nbsp; &nbsp; return normalUserAgentKeywords;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Initialize this device resolver implementation.&nbsp; &nbsp; &nbsp;* Registers the known set of device signature strings.&nbsp; &nbsp; &nbsp;* Subclasses may override to register additional strings.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected void init() {&nbsp; &nbsp; &nbsp; &nbsp; getMobileUserAgentPrefixes().addAll(Arrays.asList(KNOWN_MOBILE_USER_AGENT_PREFIXES));&nbsp; &nbsp; &nbsp; &nbsp; getMobileUserAgentKeywords().addAll(Arrays.asList(KNOWN_MOBILE_USER_AGENT_KEYWORDS));&nbsp; &nbsp; &nbsp; &nbsp; getTabletUserAgentKeywords().addAll(Arrays.asList(KNOWN_TABLET_USER_AGENT_KEYWORDS));&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Fallback called if no mobile device is matched by this resolver.&nbsp; &nbsp; &nbsp;* The default implementation of this method returns a "normal" {@link Device} that is neither mobile or a tablet.&nbsp; &nbsp; &nbsp;* Subclasses may override to try additional mobile or tablet device matching before falling back to a "normal" device.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected Device resolveFallback(HttpServletRequest request) {&nbsp; &nbsp; &nbsp; &nbsp; return LiteDevice.NORMAL_INSTANCE;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; // internal helpers&nbsp;&nbsp; &nbsp; private static final String[] KNOWN_MOBILE_USER_AGENT_PREFIXES = new String[] { "w3c ", "w3c-", "acs-", "alav",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "alca", "amoi", "audi", "avan", "benq", "bird", "blac", "blaz", "brew", "cell", "cldc", "cmd-", "dang",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "doco", "eric", "hipt", "htc_", "inno", "ipaq", "ipod", "jigs", "kddi", "keji", "leno", "lg-c", "lg-d",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "lg-g", "lge-", "lg/u", "maui", "maxo", "midp", "mits", "mmef", "mobi", "mot-", "moto", "mwbp", "nec-",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "newt", "noki", "palm", "pana", "pant", "phil", "play", "port", "prox", "qwap", "sage", "sams", "sany",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "sch-", "sec-", "send", "seri", "sgh-", "shar", "sie-", "siem", "smal", "smar", "sony", "sph-", "symb",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "t-mo", "teli", "tim-", "tosh", "tsm-", "upg1", "upsi", "vk-v", "voda", "wap-", "wapa", "wapi", "wapp",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "wapr", "webc", "winw", "winw", "xda ", "xda-" };&nbsp;&nbsp; &nbsp; private static final String[] KNOWN_MOBILE_USER_AGENT_KEYWORDS = new String[] { "blackberry", "webos", "ipod",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "lge vx", "midp", "maemo", "mmp", "mobile", "netfront", "hiptop", "nintendo DS", "novarra", "openweb",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "opera mobi", "opera mini", "palm", "psp", "phone", "smartphone", "symbian", "up.browser", "up.link",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "wap", "windows ce" };&nbsp;&nbsp; &nbsp; private static final String[] KNOWN_TABLET_USER_AGENT_KEYWORDS = new String[] { "ipad", "playbook", "hp-tablet",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "kindle" };&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP