Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

This paper has participated inProject DigginTo win the creative gift package and challenge the creative incentive money.

📖 the


Good attitude, not so tired. In a good mood, all you see is beautiful scenery.Copy the code

"If you can't solve a problem for a while, use this opportunity to see your limitations and put yourself out of order." As the old saying goes, it's easy to let go. If you are distracted by something, learn to disconnect. Cut out the paranoia, the trash in the community, and get rid of the negative energy. Good attitude, not so tired. In a good mood, all you see is beautiful scenery.

In a production environment, we need to turn off the Swagger configuration to avoid exposing the interface to this dangerous behavior.


User AgentThe meaning of

User AgentIt is a special string header that enables the server to identify the operating system and version used by the customer, CPU type, browser and version, browser rendering engine, browser language, browser plug-in, etc.

Some websites often send different pages to different operating systems and browsers by judging UA. Therefore, some pages may not be displayed properly in a browser, but UA can be disguised to bypass detection.


UA string for the browser

The standard format is: browser identifier (OPERATING system identifier; Identification of encryption level; The rendering engine identifies version information

Browser identity

Many websites ignore the two-digit version number during UA detection, which may result in bad pages received by browsers and later versions. Therefore, in browser versions after browser 10, the browser identifier is fixed as browser, and the real version information is added at the end of UA string.

Note: From Baidu Baike


UserAgentUtils.jar

UserAgentUtils.jar 是 UserAgentThe utility class.

Maven is as follows:

<! -- https://mvnrepository.com/artifact/eu.bitwalker/UserAgentUtils --> 
<dependency>
	 <groupId>eu.bitwalker</groupId>
 	<artifactId>UserAgentUtils</artifactId> 
	 <version>1.20</version> 
 </dependency>
Copy the code

The Java code is as follows:

UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));  
Browser browser = userAgent.getBrowser(); 
OperatingSystem os = userAgent.getOperatingSystem();
Copy the code

methods

package com.cyj.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;

/ * * * *@Description: Obtain the IP controller *@ClassName: IpController.java
 * @author ChenYongJia
 * @Date20 April 2019 20:25 PM *@Email [email protected]
 */
@RestController
public class IpController {
	
	private static final Logger log = LoggerFactory.getLogger(IpController.class);
	
	/** * Get OS and browser information *@param request
     * @return* /
    @RequestMapping(value="/browser",method = RequestMethod.GET)
    public void getBrowser(HttpServletRequest request){
    	String ua = request.getHeader("User-Agent");
        log.info("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
        log.info("Operating system and browser information:"+ua);
        // To the UserAgent object
        UserAgent userAgent = UserAgent.parseUserAgentString(ua);
        // Get browser information
        Browser browser = userAgent.getBrowser();
        log.info("Browser info:"+browser);
        // Obtain system information
        OperatingSystem os = userAgent.getOperatingSystem();
        log.info("System Information:"+os);
        // System name
        String system = os.getName();
        log.info("System name:"+system);
        // Browser name
        String browserName = browser.getName();
        log.info("Browser name:"+browserName);
        log.info("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"); }}Copy the code

​

The results are as follows:

* * * * * * * * * * * * * * firefox * * * * * * * * * * * * * * * * operating system and browser information: Mozilla /5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0Browser information: FIREFOX System information: WINDOWS_7 System name: Windows7Name: browser Firefox * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Google * * * * * * * * * * * * * * * * operating system and browser information: Mozilla /5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.03239.108. Safari/537.36Browser information: CHROME System information: WINDOWS_7 System name: Windows7The browser name: Chrome * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * IE * * * * * * * * * * * * * operating system and browser information: Mozilla /5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko Browser information: MOZILLA System information: WINDOWS_7 System name: Windows7Browser name: Mozilla ******************************Copy the code

Obtain the browser type, OPERATING system type, and mobile phone model by using the user-agent command

In the browser request headerUser-Agent

String ua = request.getHeader("User-Agent")
Copy the code

Get the browser type, operating system type, see aboveJava codeThe following title content

Get phone type:

package com.cyj.controller;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;

/ * * * *@Description: Obtain the IP controller *@ClassName: IpController.java
 * @author ChenYongJia
 * @Date20 April 2019 20:25 PM *@Email [email protected]
 */
@RestController
public class IpController {
	
	private static final Logger log = LoggerFactory.getLogger(IpController.class);
	
	/** * Get OS and browser information *@param request
     * @return* /
    @RequestMapping(value="/browser",method = RequestMethod.GET)
    public void getBrowser(HttpServletRequest request){
    	UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));  
        Browser browser = userAgent.getBrowser();  
        OperatingSystem os = userAgent.getOperatingSystem();
    	
    	Pattern pattern = Pattern.compile("; \\s? (\\S*? \\s? \\S*?) \\s? (Build)? /");  
        Matcher matcher = pattern.matcher((CharSequence) userAgent);  
        String model = null;  
        if (matcher.find()) {  
            model = matcher.group(1).trim();  
            log.debug("Resolve model by userAgent:"+ model); }}}Copy the code

​


Give me one more:

 package com.cyj.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import eu.bitwalker.useragentutils.Version;

/ * * * *@Description: Obtain the IP controller *@ClassName: IpController.java
 * @author ChenYongJia
 * @Date20 April 2019 20:25 PM *@Email [email protected]
 */
@RestController
public class IpController {

	private static final Logger log = LoggerFactory.getLogger(IpController.class);

	/** * Get OS and browser information **@param request
	 * @return* /
	@RequestMapping(value = "/browser", method = RequestMethod.GET)
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String agentStr = request.getHeader("user-agent");
		System.out.println(agentStr);
		UserAgent agent = UserAgent.parseUserAgentString(agentStr);
		/ / the browser
		Browser browser = agent.getBrowser();
		System.out.println("Type:" + browser.getBrowserType() + "\n Name:" + browser.getName() + "\n Manufacturer:"
				+ browser.getManufacturer() + "\n Product Series:" + browser.getGroup() + "\n Engine:" + browser.getRenderingEngine());

		// Browser version
		Version version = agent.getBrowserVersion();
		System.out.println("= = = = = = = = = = = = = = = = = = = = = = = =");
		System.out.println("Main version:" + version.getMajorVersion() + "\n Smaller version:" + version.getMinorVersion() + "\n Full version:"
				+ version.getVersion());
		// Operating system
		System.out.println("= = = = = = = = = = = = = = = = = = = = = = = =");
		OperatingSystem os = agent.getOperatingSystem();
		System.out.println("Name:" + os.getName() + "\n Device type:" + os.getDeviceType() + "\n Product Series:" + os.getGroup() + "\n Producer:"+ os.getManufacturer()); }}Copy the code

Test the result by yourself!!

Finally, thank you for your patience to watch the end, the original is not easy, leave a point like collection is your biggest encouragement to me!


🎉 summary:

  • For more references, see here:The Blog of Chan Wing Kai

  • Like the small partner of the blogger can add a concern, a thumbs-up oh, continue to update hey hey!