SpringSecurity-登录失败后才显示验证码

时间 2019/3/14 19:07:31 加载中...

思路

我的思路简单实现,登录失败信息并没有存放在后台。因此对专业程序员而已,其实是可以绕过验证码认证的。但可在此基础上可实现失败信息存放在后台的形式。

客户浏览器存放一个名为showcode的cookie。当cookie有值且为1时,才显示验证码。
同样后台根据cookie判断是否需要验证验证码。
注意:登录成功后,要重置showcode

登录页面

页面通过showcode控制展示

  1. gl><el-form-item label="验证码" v-show="showcode">
  2. <svg class="icon" aria-hidden="true">
  3. <use xlink:href="#icon-key"></use>
  4. </svg>
  5. <img src="" id="ImageCatpcha" alt="正在加载" class="yzm"/>
  6. <el-input placeholder="验证码" class="dis-table" name="code">
  7. </el-input>
  8. </el-form-item>

从cookie中获取showcode

  1. function getCookie(name)
  2. {
  3. var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
  4. if(arr=document.cookie.match(reg))
  5. return unescape(arr[2]);
  6. else
  7. return null;
  8. }
  9. var showcode = getCookie("showcode");

添加showcode属性

  1. new Vue({
  2. el: '#app',
  3. data: function() {
  4. return {
  5. activeName: 'first',
  6. gl>showcode:showcode==1
  7. }
  8. },
  9. methods:{
  10. handleClick:handleClick,
  11. submitForm:submitForm
  12. }
  13. })

后台修改

  1. http.addFilterBefore(validateCodeFilter,UsernamePasswordAuthenticationFilter.class);
  2. http.formLogin().loginPage("/login")
  3. gl>.successHandler(new MyAuthenticationSuccessHandler())
  4. gl>.failureHandler(new MyAuthenticationFailureHandler())
  5. ;

validateCodeFilter

  1. private void validate(HttpServletRequest request) {
  2. gl>if(!needValidate(request))
  3. gl>return;
  4. String code = request.getParameter("code");
  5. String time = request.getParameter("time");
  6. String md5 = request.getParameter("md5");
  7. String username = request.getParameter("username");
  8. boolean isOk = codeService.checkCode(code, time, md5);
  9. if(!isOk)
  10. throw new ValidateCodeException("验证码错误");
  11. }
  12. gl>private boolean needValidate(HttpServletRequest request){
  13. gl>if(getCookie(request,"showcode").equals("1"))
  14. gl>.return true;
  15. gl>else
  16. gl>.return false;
  17. gl>}
  18. gl>private String getCookie(HttpServletRequest request,String name){
  19. gl>Cookie[] cookies = request.getCookies();
  20. gl>if (cookies==null||cookies.length<1) {
  21. gl>.return null;
  22. gl>.}
  23. gl>Cookie cookie = null;
  24. gl>for (Cookie c : cookies) {
  25. gl>if (name.equals(c.getName())) {
  26. gl>cookie = c;
  27. gl>break;
  28. gl>}
  29. gl>}
  30. gl>if(cookie==null)
  31. gl>.return "";
  32. gl>else
  33. gl>return cookie.getValue();
  34. gl>}

MyAuthenticationSuccessHandler.java
登录成功,清除showcode

  1. package cnki.bdms.web.ui.security;
  2. import org.springframework.security.core.Authentication;
  3. import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.Cookie;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. public class MyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
  10. @Override
  11. public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
  12. gl>response.addCookie(new Cookie("showcode",""));
  13. super.onAuthenticationSuccess(request, response, authentication);
  14. }
  15. }

MyAuthenticationFailureHandler.java
登录失败,设置showcode的值为1

  1. package cnki.bdms.web.ui.security;
  2. import org.springframework.security.core.AuthenticationException;
  3. import org.springframework.security.web.DefaultRedirectStrategy;
  4. import org.springframework.security.web.authentication.AuthenticationFailureHandler;
  5. import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.Cookie;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import java.io.IOException;
  11. public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
  12. @Override
  13. public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
  14. gl>httpServletResponse.addCookie(new Cookie("showcode","1"));
  15. SimpleUrlAuthenticationFailureHandler handler = new SimpleUrlAuthenticationFailureHandler();
  16. handler.setDefaultFailureUrl("/login?error=true");
  17. handler.onAuthenticationFailure(httpServletRequest, httpServletResponse, e);
  18. }
  19. }

完。

扫码分享
版权说明
作者:SQBER
文章来源:http://www.sqber.com/articles/SpringSecurity-login-failure-show-code.html
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。