SpringSecurity添加圖形驗證碼認(rèn)證實現(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è)置在用戶認(rèn)證過濾器(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添加圖形驗證碼認(rèn)證實現(xiàn)的文章就介紹到這了,更多相關(guān)SpringSecurity 圖形驗證碼認(rèn)證內(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中的線程安全集合CopyOnWriteArrayList解析
這篇文章主要介紹了Java中的線程安全CopyOnWriteArrayList解析,CopyOnWriteArrayList是ArrayList的線程安全版本,從他的名字可以推測,CopyOnWriteArrayList是在有寫操作的時候會copy一份數(shù)據(jù),然后寫完再設(shè)置成新的數(shù)據(jù),需要的朋友可以參考下2023-12-12
java如何獲取request中json數(shù)據(jù)
這篇文章主要給大家介紹了關(guān)于java如何獲取request中json數(shù)據(jù)的相關(guān)資料,文中通過代碼示例以及圖文將獲取的方法介紹的非常詳細,對大家學(xué)習(xí)或者使用java具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08
mybatis和mybatis-plus設(shè)置值為null不起作用問題及解決
Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查詢時對空值的處理策略,通過配置不同的策略類型,可以靈活地處理實體對象的空值問題2025-02-02
Mybatis實現(xiàn)自定義類型轉(zhuǎn)換器TypeHandler的方法
Mybatis實現(xiàn)自定義的轉(zhuǎn)換器非常的簡單,只需要三步就可以實現(xiàn)自定義類型轉(zhuǎn)換器TypeHandler,非常不錯,具有參考借鑒價值,感興趣的朋友一起看下吧2016-07-07
Idea中如何調(diào)出Run dashboard 或services窗口
這篇文章主要介紹了Idea中如何調(diào)出Run dashboard 或services窗口問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
Java如何實現(xiàn)多個線程之間共享數(shù)據(jù)
這篇文章主要介紹了Java如何實現(xiàn)多個線程之間共享數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11

