SpringSecurity添加圖形驗證碼認證實現(xiàn)
第一步:圖形驗證碼接口
1.使用第三方的驗證碼生成工具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è)置驗證接口
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 { // 獲得隨機驗證碼 String code = defaultKaptcha.createText(); logger.info("驗證碼:{}",code); // 將驗證碼存入session request.getSession().setAttribute(SESSION_KEY,code); // 繪制驗證碼 BufferedImage image = defaultKaptcha.createImage(code); // 輸出驗證碼 ServletOutputStream out = response.getOutputStream(); ImageIO.write(image, "jpg", out); }
3.模板表單設(shè)置
<div class="form-group"> <label>驗證碼:</label> <input type="text" class="form-control" placeholder="驗證碼" name="code"> <img src="/code/image" th:src="@{/code/image}" onclick="this.src='/code/image?'+Math.random()"> </div>
第二步:設(shè)置圖像驗證過濾器
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的驗證碼 ? ? ? ? ? ? ? ? String sessionCode = (String) request.getSession().getAttribute(PageController.SESSION_KEY); ? ? ? ? ? ? ? ? // 獲取用戶輸入的驗證碼 ? ? ? ? ? ? ? ? String inputCode = request.getParameter("code"); ? ? ? ? ? ? ? ? // 判斷是否正確 ? ? ? ? ? ? ? ? if(sessionCode == null||!sessionCode.equals(inputCode)){ ? ? ? ? ? ? ? ? ? ? throw new ValidateCodeException("驗證碼錯誤"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? }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
第三步:將圖像驗證過濾器添加到springsecurity過濾器鏈中
1.添加到過濾器鏈中,并設(shè)置在用戶認證過濾器(UsernamePasswordAuthenticationFilter)前
@Resource private ImageCodeValidateFilter imageCodeValidateFilter; @Override protected void configure(HttpSecurity http) throws Exception { // 前后代碼略 // 添加圖形驗證碼過濾器鏈 http.addFilterBefore(imageCodeValidateFilter, UsernamePasswordAuthenticationFilter.class) }
2.一定不要忘記放行驗證碼接口
// 攔截設(shè)置 http .authorizeHttpRequests() //排除/login .antMatchers("/login","/code/image").permitAll();
到此這篇關(guān)于SpringSecurity添加圖形驗證碼認證實現(xiàn)的文章就介紹到這了,更多相關(guān)SpringSecurity 圖形驗證碼認證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springsecurity實現(xiàn)登錄驗證以及根據(jù)用戶身份跳轉(zhuǎn)不同頁面
- SpringBoot整合SpringSecurity實現(xiàn)圖形驗證碼功能
- SpringSecurity集成圖片驗證碼的詳細過程
- SpringBoot?SpringSecurity?詳細介紹(基于內(nèi)存的驗證)
- SpringBoot+SpringSecurity+jwt實現(xiàn)驗證
- Springboot+SpringSecurity實現(xiàn)圖片驗證碼登錄的示例
- SpringSecurity從數(shù)據(jù)庫中獲取用戶信息進行驗證的案例詳解
- SpringSecurity實現(xiàn)圖形驗證碼功能的實例代碼
- SpringBoot + SpringSecurity 短信驗證碼登錄功能實現(xiàn)
- SpringSecurity實現(xiàn)多種身份驗證方式
相關(guān)文章
java 實現(xiàn)websocket的兩種方式實例詳解
這篇文章主要介紹了java 實現(xiàn)websocket的兩種方式實例詳解,一種使用tomcat的websocket實現(xiàn),一種使用spring的websocket,本文通過代碼給大家介紹的非常詳細,需要的朋友可以參考下2018-07-07Jvm調(diào)優(yōu)和SpringBoot項目優(yōu)化的詳細教程
這篇文章主要介紹了Jvm調(diào)優(yōu)和SpringBoot項目優(yōu)化,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09@Scheduled定時器原理及@RefreshScope相互影響
這篇文章主要為大家介紹了@Scheduled定時器原理及@RefreshScope相互影響詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07如何解決redis的NOAUTH Authentication required異常
這篇文章主要介紹了Jedis異常解決:NOAUTH Authentication required,,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值2019-07-07Java中@Autowired和@Resource區(qū)別
本文主要介紹了Java中@Autowired和@Resource區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06