欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringSecurity添加圖形驗(yàn)證碼認(rèn)證實(shí)現(xiàn)

 更新時(shí)間:2022年08月15日 11:14:41   作者:卑微小鐘  
本文主要介紹了SpringSecurity添加圖形驗(yàn)證碼認(rèn)證實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

第一步:圖形驗(yàn)證碼接口

1.使用第三方的驗(yàn)證碼生成工具Kaptcha

https://github.com/penggle/kaptcha

@Configuration
public class KaptchaImageCodeConfig {
    @Bean
    public DefaultKaptcha getDefaultKaptcha(){
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Properties properties = new Properties();
        properties.setProperty(Constants.KAPTCHA_BORDER, "yes");
        properties.setProperty(Constants.KAPTCHA_BORDER_COLOR, "192,192,192");
        properties.setProperty(Constants.KAPTCHA_IMAGE_WIDTH, "110");
        properties.setProperty(Constants.KAPTCHA_IMAGE_HEIGHT, "36");
        properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR, "blue");
        properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_SIZE, "28");
        properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_NAMES, "宋體");
        properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");
        // 圖片效果
        properties.setProperty(Constants.KAPTCHA_OBSCURIFICATOR_IMPL,
                "com.google.code.kaptcha.impl.ShadowGimpy");
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
}

2.設(shè)置驗(yàn)證接口

Logger logger = LoggerFactory.getLogger(getClass());
public static final String SESSION_KEY = "SESSION_KEY_IMAGE_CODE";
@GetMapping("/code/image")
public void codeImage(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // 獲得隨機(jī)驗(yàn)證碼
    String code = defaultKaptcha.createText();
    logger.info("驗(yàn)證碼:{}",code);
    // 將驗(yàn)證碼存入session
    request.getSession().setAttribute(SESSION_KEY,code);
    // 繪制驗(yàn)證碼
    BufferedImage image = defaultKaptcha.createImage(code);
    // 輸出驗(yàn)證碼
    ServletOutputStream out = response.getOutputStream();
    ImageIO.write(image, "jpg", out);
}

3.模板表單設(shè)置

<div class="form-group">
    <label>驗(yàn)證碼:</label>
    <input type="text" class="form-control" placeholder="驗(yàn)證碼" name="code">
    <img src="/code/image" th:src="@{/code/image}" onclick="this.src='/code/image?'+Math.random()">
</div>

第二步:設(shè)置圖像驗(yàn)證過濾器

1.過濾器

@Component
public class ImageCodeValidateFilter extends OncePerRequestFilter {
? ? private MyAuthenticationFailureHandler myAuthenticationFailureHandler;
? ? // 失敗處理器
? ? @Resource
? ? public void setMyAuthenticationFailureHandler(MyAuthenticationFailureHandler myAuthenticationFailureHandler) {
? ? ? ? this.myAuthenticationFailureHandler = myAuthenticationFailureHandler;
? ? }

? ? @Override
? ? protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
? ? ? ? try {
? ? ? ? ? ? if ("/login/form".equals(request.getRequestURI()) &&
? ? ? ? ? ? ? ? ? ? request.getMethod().equalsIgnoreCase("post")) {
? ? ? ? ? ? ? ? // 獲取session的驗(yàn)證碼
? ? ? ? ? ? ? ? String sessionCode = (String) request.getSession().getAttribute(PageController.SESSION_KEY);
? ? ? ? ? ? ? ? // 獲取用戶輸入的驗(yàn)證碼
? ? ? ? ? ? ? ? String inputCode = request.getParameter("code");
? ? ? ? ? ? ? ? // 判斷是否正確
? ? ? ? ? ? ? ? if(sessionCode == null||!sessionCode.equals(inputCode)){
? ? ? ? ? ? ? ? ? ? throw new ValidateCodeException("驗(yàn)證碼錯(cuò)誤");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }catch (AuthenticationException e){
? ? ? ? ? ? myAuthenticationFailureHandler.onAuthenticationFailure(request,response,e);
? ? ? ? ? ? //e.printStackTrace();
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? filterChain.doFilter(request, response);
? ? }
}

異常類

public class ValidateCodeException extends AuthenticationException {
    public ValidateCodeException(String msg) {
        super(msg);
    }
}

注意:一定是繼承AuthenticationException

第三步:將圖像驗(yàn)證過濾器添加到springsecurity過濾器鏈中

1.添加到過濾器鏈中,并設(shè)置在用戶認(rèn)證過濾器(UsernamePasswordAuthenticationFilter)前

@Resource
private ImageCodeValidateFilter imageCodeValidateFilter;
@Override
protected void configure(HttpSecurity http) throws Exception {
    // 前后代碼略
    // 添加圖形驗(yàn)證碼過濾器鏈
    http.addFilterBefore(imageCodeValidateFilter, UsernamePasswordAuthenticationFilter.class)
}

2.一定不要忘記放行驗(yàn)證碼接口

// 攔截設(shè)置
http
    .authorizeHttpRequests()
    //排除/login
    .antMatchers("/login","/code/image").permitAll();

到此這篇關(guān)于SpringSecurity添加圖形驗(yàn)證碼認(rèn)證實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringSecurity 圖形驗(yàn)證碼認(rèn)證內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java web手寫實(shí)現(xiàn)分頁功能

    java web手寫實(shí)現(xiàn)分頁功能

    這篇文章主要為大家詳細(xì)介紹了java web手寫實(shí)現(xiàn)分頁功能的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • java 實(shí)現(xiàn)websocket的兩種方式實(shí)例詳解

    java 實(shí)現(xiàn)websocket的兩種方式實(shí)例詳解

    這篇文章主要介紹了java 實(shí)現(xiàn)websocket的兩種方式實(shí)例詳解,一種使用tomcat的websocket實(shí)現(xiàn),一種使用spring的websocket,本文通過代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2018-07-07
  • maven 刪除下載失敗的包的方法

    maven 刪除下載失敗的包的方法

    本文介紹了當(dāng)Maven包報(bào)紅時(shí),使用刪除相關(guān)文件的方法來解決該問題,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-09-09
  • Jvm調(diào)優(yōu)和SpringBoot項(xiàng)目優(yōu)化的詳細(xì)教程

    Jvm調(diào)優(yōu)和SpringBoot項(xiàng)目優(yōu)化的詳細(xì)教程

    這篇文章主要介紹了Jvm調(diào)優(yōu)和SpringBoot項(xiàng)目優(yōu)化,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • @Scheduled定時(shí)器原理及@RefreshScope相互影響

    @Scheduled定時(shí)器原理及@RefreshScope相互影響

    這篇文章主要為大家介紹了@Scheduled定時(shí)器原理及@RefreshScope相互影響詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • 如何解決redis的NOAUTH Authentication required異常

    如何解決redis的NOAUTH Authentication required異常

    這篇文章主要介紹了Jedis異常解決:NOAUTH Authentication required,,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值
    2019-07-07
  • java使用swt顯示圖片示例分享

    java使用swt顯示圖片示例分享

    這篇文章主要介紹了java使用swt顯示圖片示例,修改后就可變?yōu)閳D片瀏覽器,需要的朋友可以參考下
    2014-02-02
  • slf4j與log4j全面了解

    slf4j與log4j全面了解

    下面小編就為大家?guī)硪黄猻lf4j與log4j全面了解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-07-07
  • Java中@Autowired和@Resource區(qū)別

    Java中@Autowired和@Resource區(qū)別

    本文主要介紹了Java中@Autowired和@Resource區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • idea如何配置springboot熱部署

    idea如何配置springboot熱部署

    這篇文章主要介紹了idea如何配置springboot熱部署問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12

最新評(píng)論