日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

spring boot security驗(yàn)證碼登錄示例

作者:不識(shí)君的荒漠 更新時(shí)間: 2023-07-04 編程語言

前言

在spring boot security自定義認(rèn)證一文,基本給出了一個(gè)完整的自定義的用戶登錄認(rèn)證的示例,但是未涉及到驗(yàn)證的使用,本文介紹登錄的時(shí)候如何使用驗(yàn)證碼。

本文介紹一個(gè)驗(yàn)證碼生成工具,比較老的一個(gè)庫了,僅作demo使用,不太建議生產(chǎn)用了,因?yàn)槿绻愕拇a需要進(jìn)行安全掃描,這個(gè)庫已經(jīng)不再維護(hù)了,如果掃出漏洞,也沒法升級(jí)修復(fù)了。

但是如果沒有安全掃描的要求,還是可以用的。
github: https://github.com/penggle/kaptcha

代碼示例

引入依賴

		<dependency>
			<groupId>com.github.penggle</groupId>
			<artifactId>kaptcha</artifactId>
			<version>2.3.2</version>
		</dependency>

定義驗(yàn)證碼生成器

@Configuration
public class CaptchaConfiguration {


    @Bean
    public Producer defaultKaptcha() {
        Properties properties = new Properties();
        // 還有一些其它屬性,可以進(jìn)行源碼自己看相關(guān)配置,比較清楚了,根據(jù)變量名也能猜出來什么意思了
        properties.setProperty(Constants.KAPTCHA_IMAGE_WIDTH, "150");
        properties.setProperty(Constants.KAPTCHA_IMAGE_HEIGHT, "50");
        properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_STRING, "0123456789abcdefghigklmnopqrstuvwxyz");
        properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");
        Config config = new Config(properties);
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
}

示例是作為spring 的bean注冊(cè)到spring 容器了,當(dāng)然也可以作為一個(gè)單例對(duì)象放到一個(gè)靜態(tài)類里。

定義獲取驗(yàn)證碼及認(rèn)證接口

這個(gè)接口在前面的文章里已經(jīng)提到過了,這里只是完善驗(yàn)證碼的部分.

@RequestMapping("/login")
@RestController
public class LoginController {

    private final AuthenticationManager authenticationManager;

    private final Producer producer;

    public LoginController(AuthenticationManager authenticationManager, Producer producer) {
        this.authenticationManager = authenticationManager;
        this.producer = producer;
    }

    @PostMapping()
    public Object login(@RequestBody User user, HttpSession session) {
        Object captcha = session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
        if (captcha == null || !captcha.toString().equalsIgnoreCase(user.getCaptcha())) {
            return "captcha is not correct.";
        }
        try {
            // 使用定義的AuthenticationManager進(jìn)行認(rèn)證處理
            Authentication authenticate = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword()));
            // 認(rèn)證通過,設(shè)置到當(dāng)前上下文,如果當(dāng)前認(rèn)證過程后續(xù)還有處理的邏輯需要的話。這個(gè)示例是沒有必要了
            SecurityContextHolder.getContext().setAuthentication(authenticate);
            return "login success";
        } catch (Exception e) {
            return "login failed";
        }
    }

    /**
     * 獲取驗(yàn)證碼,需要的話,可以提供一個(gè)驗(yàn)證碼獲取的接口,在上面的login里把驗(yàn)證碼傳進(jìn)來進(jìn)行比對(duì)
     */
    @GetMapping("/captcha")
    public void captcha(HttpServletResponse response, HttpSession session) throws IOException {
        response.setContentType("image/jpeg");
        String text = producer.createText();
        session.setAttribute(Constants.KAPTCHA_SESSION_KEY, text);
        BufferedImage image = producer.createImage(text);
        try (ServletOutputStream out = response.getOutputStream()) {
            ImageIO.write(image, "jpg", out);
        }
    }
}

測試

看一下效果,

獲取驗(yàn)證碼

在這里插入圖片描述

登錄

在這里插入圖片描述

原文鏈接:https://blog.csdn.net/x763795151/article/details/131503909

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新