多元网络兰州网站建设网站视频做背景
- 作者: 五速梦信息网
- 时间: 2026年03月21日 11:20
当前位置: 首页 > news >正文
多元网络兰州网站建设,网站视频做背景,九江做网站公司,杭州建设工程信息网站文章目录 1、生成图片验证码2、创建验证码过滤器3、将过滤器加入SpringSecurity过滤链4、修改登录页 SpringSecurity是通过过滤器链来完成的#xff0c;接下来的验证码#xff0c;可以尝试创建一个过滤器放到Security的过滤器链中#xff0c;在自定义的过滤器中比较验证码。… 文章目录 1、生成图片验证码2、创建验证码过滤器3、将过滤器加入SpringSecurity过滤链4、修改登录页 SpringSecurity是通过过滤器链来完成的接下来的验证码可以尝试创建一个过滤器放到Security的过滤器链中在自定义的过滤器中比较验证码。 1、生成图片验证码 引入hutool依赖用于生成验证码。这个单词怎么读糊涂 !–引入hutool– dependencygroupIdcn.hutool/groupIdartifactIdhutool-all/artifactIdversion5.3.9/version /dependency 写个用于生成验证码的接口 //HttpServletRequest和HttpServletResponse对象使用自动注入或者写在controller方法的形参都行 Controller Slf4j public class CaptchaController {GetMapping(/code/image)public void getCaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {//创建一个验证码CircleCaptcha circleCaptcha CaptchaUtil.createCircleCaptcha(200, 100, 2, 20);//获取生成的图片二维码中的值并放到session中String captchaCodecircleCaptcha.getCode();log.info(生成的验证码为{},captchaCode);request.getSession().setAttribute(LOGIN_CAPTCHA_CODE,captchaCode);//将图片写到响应流中参数一是图片。参数二是图片格式参数三是响应流ImageIO.write(circleCaptcha.getImage(),JPEG,response.getOutputStream());} } 此时调用这个接口会在响应里返回一个图片。调用下接口看看效果 2、创建验证码过滤器 很明显校验验证码要先于校验用户名密码验证码都不对就不用往下验证用户名和密码了。新建自定义过滤器类ValidateCodeFilter继承抽象类OncePerRequestFilter 。右键看下继承关系 可以看到最终是实现了Filter接口。但这里别直接实现Filter继承OncePerRequestFilter里面的好多东西直接用能省一点是一点。过滤器的实现思路为 从前端获取验证码从session中获取验证码生成验证码的时候塞session里了判断是否相等 Component Slf4j public class ValidateCodeFilter extends OncePerRequestFilter {Overrideprotected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {//因为最后是Filter接口所以所有请求都过这个过滤器//这里要先判断接口是不是登录接口不是就别对比session和前端传来的验证码了String requestURI request.getRequestURI(); //URI即去掉IP、PORT那串log.info(请求的URI为{}, requestURI);if (!requestURI.equals(/login/doLogin)) {doFilter(request, response, filterChain); //不是登录接口直接放行} else {validateCode(request, response,filterChain); //是登录接口则校验}}private void validateCode(HttpServletRequest request, HttpServletResponse response,FilterChain filterChain) throws IOException, ServletException {String enterCaptchaCode request.getParameter(code); //从请求中拿传过来的验证码的值dto好些HttpSession session request.getSession();String captchaCodeInSession (String) session.getAttribute(LOGIN_CAPTCHA_CODE); //保存在session中的验证码log.info(用户输入的验证码为{},session中的验证码为{},enterCaptchaCode,captchaCodeInSession);//移除session中之前可能存在的错误信息session.removeAttribute(captchaCodeErrorMsg);if (!StringUtils.hasText(captchaCodeInSession)) {session.removeAttribute(LOGIN_CAPTCHA_CODE);}if (!StringUtils.hasText(enterCaptchaCode) || !StringUtils.hasText(captchaCodeInSession) || !enterCaptchaCode.equalsIgnoreCase(captchaCodeInSession)) {//说明验证码不正确返回登陆页面session.setAttribute(captchaCodeErrorMsg, 验证码不正确);//验证失败重定向到登录页面response.sendRedirect(/login/toLogin);}else{filterChain.doFilter(request,response); //验证成功放行}} } 关于requset.getParameter(code) 关于在controller中接收前端数据的方式 关于直接在请求中获取参数的建议 3、将过滤器加入SpringSecurity过滤链 修改WebSecurityConfig EnableGlobalMethodSecurity(prePostEnabled true) Slf4j public class WebSecurityConfig extends WebSecurityConfigurerAdapter {Resourceprivate ValidateCodeFilter validateCodeFilter; //自动注入我定义的验证码过滤器Override/*** Security的http请求配置** param http* throws Exception/Overrideprotected void configure(HttpSecurity http) throws Exception {//设置登陆方式http.formLogin()//使用用户名和密码的登陆方式.usernameParameter(uname) //页面表单的用户名的name.passwordParameter(pwd)//页面表单的密码的name.loginPage(/login/toLogin) //自己定义登陆页面的地址.loginProcessingUrl(/login/doLogin)//配置登陆的url.successForwardUrl(/index/toIndex) //登陆成功跳转的页面.failureForwardUrl(/login/toLogin)//登陆失败跳转的页面.permitAll();//配置退出方式http.logout().logoutUrl(/logout).logoutSuccessUrl(/login/toLogin).permitAll();//配置路径拦截 的url的匹配规则http.authorizeRequests().antMatchers(/code/image).permitAll()//任何路径要求必须认证之后才能访问.anyRequest().authenticated();// 禁用csrf跨站请求注意不要写错了http.csrf().disable();// 配置登录之前添加一个验证码的过滤器 http.addFilterBefore(validateCodeFilter,UsernamePasswordAuthenticationFilter.class);}/** 资源服务匹配放行【静态资源文件】** param web* throws Exception*/// Override//public void configure(WebSecurity web) throws Exception {// web.ignoring().mvcMatchers(/resources/**);//}Beanpublic PasswordEncoder passwordEncoder() {return new BCryptPasswordEncoder();} } 以上注意两点一是别忘了放行生成二维码的接口这个不需要登录鉴权。 http.authorizeRequests().antMatchers(/code/image).permitAll()//任何路径要求必须认证之后才能访问.anyRequest().authenticated();而是在用户名密码验证过滤器前加一个自定义的验证码过滤器addFilter方法 http.addFilterBefore(validateCodeFilter,UsernamePasswordAuthenticationFilter.class);4、修改登录页 修改login.html !DOCTYPE html html xmlns:thhttp://www.thymeleaf.org headmeta charsetUTF-8title用户登陆/title /head body h2登录页面/h2 !–\({param.error}这个如果有值就显示帐号或密码错误-- h4 th:if\){param.error} stylecolor: #FF0000;帐号或密码错误,请重新输入/h4 form action/login/doLogin methodposttabletrtd用户名:/tdtdinput typetext nameuname valuezhangsan/td/trtrtd密码:/tdtdinput typepassword namepwd/td/trtrtd验证码:/tdtdinput typetext namecode img src/code/image styleheight:33px;cursor:pointer; onclickthis.srcthis.srcspan th:text${session.captchaCodeErrorMsg} stylecolor: #FF0000; username/span/td/trtrtd colspan2button typesubmit登录/button/td/tr/table /form /body 效果
- 上一篇: 多语种网站开发什么是营销型网站
- 下一篇: 俄罗斯服务器网站做文案策划有些网站可看
相关文章
-
多语种网站开发什么是营销型网站
多语种网站开发什么是营销型网站
- 技术栈
- 2026年03月21日
-
多语种 小语种网站推广方法济南百度公司
多语种 小语种网站推广方法济南百度公司
- 技术栈
- 2026年03月21日
-
多语言网站开发公司佛山网络
多语言网站开发公司佛山网络
- 技术栈
- 2026年03月21日
-
俄罗斯服务器网站做文案策划有些网站可看
俄罗斯服务器网站做文案策划有些网站可看
- 技术栈
- 2026年03月21日
-
俄罗斯网站域名wordpress文章列表获取文章摘要
俄罗斯网站域名wordpress文章列表获取文章摘要
- 技术栈
- 2026年03月21日
-
俄罗斯最新新闻消息seo搜索引擎优化排名哪家更专业
俄罗斯最新新闻消息seo搜索引擎优化排名哪家更专业
- 技术栈
- 2026年03月21日






