springboot integrates extreme experience to achieve login

JiExperience is a third-party library that provides verification services, which can effectively prevent robot attacks and improve website security. The integration of Spring Boot with JiExperience can provide users with better protection for sensitive operations such as login and registration.

Here are the steps to integrate the best experience:

1. First, you need to register an account on the Jiyan official website, create an application, and obtain the Jiyan ID and Key.

2. Add JiExperience dependency to the Spring Boot project.

<dependency>
    <groupId>com.geetest.sdk</groupId>
    <artifactId>gt3-sdk</artifactId>
    <version>4.0.7-p3</version>
</dependency>

3. Configure the ID and Key of Jiexin in application.yml.

gt:
  captcha:
    id: [your_id]
    key: [your_key]

4. Write the Controller for verification code.

@RestController
public class CaptchaController {
    @Autowired
    private CaptchaService captchaService;

    @GetMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) {
        captchaService.generateCaptcha(request, response);
    }
}

5. Write CaptchaService.

@Service
public class CaptchaService {
    @Autowired
    private GeetestLib geetestLib;

    public void generateCaptcha(HttpServletRequest request, HttpServletResponse response) {
        String userid = UUID.randomUUID().toString();
        int gtServerStatus = geetestLib.preProcess(userid);
        request.getSession().setAttribute(GeetestLib.gtServerStatusSessionKey, gtServerStatus);
        request.getSession().setAttribute(GeetestLib.useridSessionKey, userid);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put(GeetestLib.gtServerStatusSessionKey, gtServerStatus);
        jsonObject.put(GeetestLib.gtUserIdSessionKey, userid);
        response.setContentType("application/json;charset=UTF-8");
        try {
            response.getWriter().write(jsonObject.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public boolean validateCaptcha(HttpServletRequest request) {
        String challenge = request.getParameter(GeetestLib.geetestChallenge);
        String validate = request.getParameter(GeetestLib.geetestValidate);
        String seccode = request.getParameter(GeetestLib.geetestSeccode);
        String userid = (String) request.getSession().getAttribute(GeetestLib.useridSessionKey);

        int gtServerStatus = (int) request.getSession().getAttribute(GeetestLib.gtServerStatusSessionKey);

        int gtResult = 0;
        if (gtServerStatus == 1) {
            gtResult = geetestLib.enhencedValidateRequest(challenge, validate, seccode, userid);
        } else {
            gtResult = geetestLib.failbackValidateRequest(challenge, validate, seccode);
        }

        return gtResult == 1;
    }
}

6. In the logged in or registered Controller, call the validateCaptcha method of CaptchaService to verify the verification code.

@PostMapping("/login")
public String login(String username, String password, HttpServletRequest request) {
    boolean captchaPassed = captchaService.validateCaptcha(request);
    if (captchaPassed) {
        // 验证码验证通过
        // 进行登录操作
    } else {
        // 验证码验证失败
    }
}

@PostMapping("/register")
public String register(String username, String password, HttpServletRequest request) {
    boolean captchaPassed = captchaService.validateCaptcha(request);
    if (captchaPassed) {
        // 验证码验证通过
        // 进行注册操作
    } else {
        // 验证码验证失败
    }
}

Through the above steps, you can achieve Spring Boot integration experience and provide users with better verification services.

Guess you like

Origin blog.csdn.net/qq_36151389/article/details/132857407