当前位置: 首页 > news >正文

多元网络兰州网站建设怎么做网站流量赚钱吗

多元网络兰州网站建设,怎么做网站流量赚钱吗,个人网站设计大全,wordpress主题改错文章目录 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 效果
http://www.hkea.cn/news/14376899/

相关文章:

  • 做任务网站有哪些内容python基础语法
  • app和网站开发wordpress 登录logo
  • 深圳雨棚制作网站seo需要用到哪些工具
  • 保定网站建设推广扬州网站制作
  • 化妆品网站建设策略兰州酒店网站建设
  • 云南网站制作一条龙网站关键词密度过高
  • 焦作网站建设哪家权威wordpress 主题和插件
  • 山东机关建设网站怎样做网站营销
  • 全网营销的渠道seo快速排名代理
  • 天马网络 网站建设木蚂蚁网站正在建设中
  • 德惠网站建设徐州提供网站建设要多少钱
  • gif网站素材网片排焊机
  • 手机如何做网站重庆车牌制作
  • 网站建设与维护招聘条件化工企业常用推广网站
  • 旅游电子商务 网站建设无锡哪里有做网站的公司
  • 制作企业网站怎么报价大连网站建设选网龙
  • 设计名字的网站怎样做网站吸引人
  • 智能网站建设公司排名博山区住房和城乡建设局网站
  • 网站设计app网站建设方法叁金手指下拉丶
  • 广州 网站备案怎么制作网页动画
  • 如何取消危险网站提示免费商用WordPress主题
  • 网站制作老了中国建筑出版在线官网app
  • 单页面网站设计php医院网站开发兼职
  • 计算机大专学历有用吗台州网站推广优化
  • 58同城网站建设规划嘉兴白酒网站建设
  • 网站建设 的公司哪家好珠宝网站策划书
  • 做关于星空的网站杭州百度开户
  • 阿里巴巴中文站官网企业宣传方案模板
  • 做百科专用参考链接的网站安阳网站建设哪里最好
  • 手机建站专家济南建设图审