SpringSecurity添加圖形驗(yàn)證碼認(rèn)證實(shí)現(xiàn)
第一步:圖形驗(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)文章希望大家以后多多支持腳本之家!
- springsecurity實(shí)現(xiàn)登錄驗(yàn)證以及根據(jù)用戶身份跳轉(zhuǎn)不同頁面
- SpringBoot整合SpringSecurity實(shí)現(xiàn)圖形驗(yàn)證碼功能
- SpringSecurity集成圖片驗(yàn)證碼的詳細(xì)過程
- SpringBoot?SpringSecurity?詳細(xì)介紹(基于內(nèi)存的驗(yàn)證)
- SpringBoot+SpringSecurity+jwt實(shí)現(xiàn)驗(yàn)證
- Springboot+SpringSecurity實(shí)現(xiàn)圖片驗(yàn)證碼登錄的示例
- SpringSecurity從數(shù)據(jù)庫中獲取用戶信息進(jìn)行驗(yàn)證的案例詳解
- SpringSecurity實(shí)現(xiàn)圖形驗(yàn)證碼功能的實(shí)例代碼
- SpringBoot + SpringSecurity 短信驗(yàn)證碼登錄功能實(shí)現(xiàn)
- SpringSecurity實(shí)現(xiàn)多種身份驗(yàn)證方式
相關(guān)文章
java 實(shí)現(xiàn)websocket的兩種方式實(shí)例詳解
這篇文章主要介紹了java 實(shí)現(xiàn)websocket的兩種方式實(shí)例詳解,一種使用tomcat的websocket實(shí)現(xiàn),一種使用spring的websocket,本文通過代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-07-07Jvm調(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相互影響詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07如何解決redis的NOAUTH Authentication required異常
這篇文章主要介紹了Jedis異常解決:NOAUTH Authentication required,,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值2019-07-07Java中@Autowired和@Resource區(qū)別
本文主要介紹了Java中@Autowired和@Resource區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06