最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue+SSM實(shí)現(xiàn)驗(yàn)證碼功能

 更新時(shí)間:2018年12月07日 10:56:46   作者:hui灰灰  
這篇文章主要介紹了vue+SSM實(shí)現(xiàn)驗(yàn)證碼功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

 源碼:https://github.com/HannahLihui/StudentManager-SSM/tree/master/SSM-git/StudentManager-SSM-master

1.前端有一個img引入,這里this.src=this.src+'?'就會調(diào)用映射到后臺的checkCode

<el-form-item prop="code">
     <img src="checkCode" alt="" width="100" height="32" class="passcode" style="height:43px;cursor:pointer; float:left;"onclick="this.src=this.src+'?'">>
    <el-input v-model="login.code" placeholder="請輸入驗(yàn)證碼" style="width: 100px; float:center" auto-complete="off"></el-input>
</el-form-item>

2.后臺返回一個圖片

@RequestMapping(value="/checkCode")
 public void checkCode(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
 //設(shè)置相應(yīng)類型,告訴瀏覽器輸出的內(nèi)容為圖片
   response.setContentType("image/jpeg");
   HttpSession session = request.getSession();
   //設(shè)置響應(yīng)頭信息,告訴瀏覽器不要緩存此內(nèi)容
   response.setHeader("pragma", "no-cache");
   response.setHeader("Cache-Control", "no-cache");
   response.setDateHeader("Expire", );
   RandomValidateCode randomValidateCode = new RandomValidateCode();
   try {
    randomValidateCode.getRandcode(request, response);
   } catch (Exception e) {
    e.printStackTrace();
   }
 }

3.是通過RandomValidateCode生成隨機(jī)字符串以及圖片。下面這個代碼可以直接用一下。來自:

http://m.fzitv.net/article/152255.htm

public class RandomValidateCode {
public static final String RANDOMCODEKEY = "randomcode_key";//放到session中的key
 private Random random = new Random();
 private String randString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";//隨機(jī)產(chǎn)生的字符串
 private int width = 80;//圖片寬
 private int height = 26;//圖片高
 private int lineSize = 40;//干擾線數(shù)量
 private int stringNum = 4;//隨機(jī)產(chǎn)生字符數(shù)量
 /**
 * 生成隨機(jī)圖片
 */
 public void getRandcode(HttpServletRequest request,
   HttpServletResponse response) {
  HttpSession session = request.getSession();
  //BufferedImage類是具有緩沖區(qū)的Image類,Image類是用于描述圖像信息的類
  BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
  //產(chǎn)生Image對象的Graphics對象,改對象可以在圖像上進(jìn)行各種繪制操作
  Graphics g = image.getGraphics();
  g.fillRect(0, 0, width, height);
  g.setFont(new Font("Times New Roman",Font.ROMAN_BASELINE,18));
  g.setColor(getRandColor(160, 200));
  //繪制干擾線
  for(int i=0;i<=lineSize;i++){
   drowLine(g);
  }
  //繪制隨機(jī)字符
  String randomString = "";
  for(int i=1;i<=stringNum;i++){
   randomString=drowString(g,randomString,i);
  }
  session.removeAttribute(RANDOMCODEKEY);
  session.setAttribute(RANDOMCODEKEY, randomString);
  g.dispose();
  try {
  //將內(nèi)存中的圖片通過流動形式輸出到客戶端
   ImageIO.write(image, "JPEG", response.getOutputStream());
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 /*
 * 獲得字體
 */
 private Font getFont(){
  return new Font("Fixedsys",Font.CENTER_BASELINE,18);
 }
 /*
 * 獲得顏色
 */
 private Color getRandColor(int fc,int bc){
  if(fc > 255)
   fc = 255;
  if(bc > 255)
   bc = 255;
  int r = fc + random.nextInt(bc-fc-16);
  int g = fc + random.nextInt(bc-fc-14);
  int b = fc + random.nextInt(bc-fc-18);
  return new Color(r,g,b);
 }
 /*
 * 繪制字符串
 */
 private String drowString(Graphics g,String randomString,int i){
  g.setFont(getFont());
  g.setColor(new Color(random.nextInt(101),random.nextInt(111),random.nextInt(121)));
  String rand = String.valueOf(getRandomString(random.nextInt(randString.length())));
  randomString +=rand;
  g.translate(random.nextInt(3), random.nextInt(3));
  g.drawString(rand, 13*i, 16);
  return randomString;
 }
 /*
 * 繪制干擾線
 */
 private void drowLine(Graphics g){
  int x = random.nextInt(width);
  int y = random.nextInt(height);
  int xl = random.nextInt(13);
  int yl = random.nextInt(15);
  g.drawLine(x, y, x+xl, y+yl);
 }
 /*
 * 獲取隨機(jī)的字符
 */
 public String getRandomString(int num){
  return String.valueOf(randString.charAt(num));
 }
}

--------------------------------------------------------------------------------

4.至于驗(yàn)證驗(yàn)證碼就是我弄了半天的東西。。。

因?yàn)槲也惶珪ue 然后寫前端研究了會會才知道它是怎么用的。然后我開始是想從前端拿到后端的session,但是vue這個是html頁面,不能<%@ %>引入java代碼,然后我又試了一下js的ajax引入,但是報(bào)錯,vue框架我也不太懂。。然后還是交給后端驗(yàn)證嘛。

然后就很簡單了,從login那里驗(yàn)證,提交的時(shí)候多了一個驗(yàn)證碼,但是我覺得這樣做其實(shí)是不太好的,因?yàn)轵?yàn)證碼跟登錄一起驗(yàn)證,有點(diǎn)耗時(shí),分開比較好。

submitForm(login) {
   this.$refs[login].validate((valid) => {
    if (valid) {
     this.loadings(); //加載動畫
    // window.alert(this.login.code);
     this.$http.post('/login', {
      username: this.login.username,
      password: this.login.password,
      remember: this.login.remember,
      code:this.login.code
     }).then(result => {
     //window.alert(result);
      // 判斷用戶是否登錄成功,后端返回JSON格式數(shù)據(jù),不然娶不到數(shù)據(jù)
      if (result.body.success) {
      alert("success");
       window.location.href = "/listStudentInfo";
       this.loading.close(); //關(guān)閉動畫加載
      } else {
       // 彈出錯誤信息框
       this.$emit(
        'submit-form',
        this.$message({
         message:result.body.message,
         type: 'warning',
         duration: 6000
        }),
       );
       // 清空表單狀態(tài)
       this.$refs[login].resetFields();
      }
     });
   }
    else {
     this.$emit(
      'submit-form',
      this.$message({
       message: '輸入信息有誤!',
       type: 'warning',
       duration: 6000
      }),
     );
     return false;
    }
   });
  },
@RequestMapping("/login")
public Result Login( @RequestParam(value = "username", required = false) String username,
   @RequestParam(value = "password", required = false) String password,
   @RequestParam(value = "remember", required = false) String remember,
   @RequestParam(value = "code", required = false) String code,
   HttpServletRequest request
   ) {
String error = null;
HttpSession session = request.getSession();
 System.out.println(code);
 //System.out.println(session.getAttribute( RandomValidateCode.RANDOMCODEKEY));
if(username==null||session.getAttribute( RandomValidateCode.RANDOMCODEKEY).equals(code)) {
//System.out.println("code 有問題");
return new Result(false, error);
}
//System.out.println(password);
//System.out.println("調(diào)試");
Subject subject=SecurityUtils.getSubject();
UsernamePasswordToken token=new UsernamePasswordToken(username,password);
if (remember != null) {
   if (remember.equals("true")) {
    //說明選擇了記住我
    token.setRememberMe(true);
   } else {
    token.setRememberMe(false);
   }
  } else {
   token.setRememberMe(false);
  }
System.out.println(token.isRememberMe());
try {
subject.login(token);
Result re=new Result(true, "success");
return new Result(true,error);
} catch (UnknownAccountException e) {
  System.out.println( "登陸出錯");
  error = "用戶賬戶不存在,錯誤信息:" + e.getMessage();
 }catch (IncorrectCredentialsException ex) {
 System.out.println( "用戶名和密碼不匹配");
 error = "用戶名或密碼錯誤,錯誤信息:" + ex.getMessage();
 }catch (AuthenticationException e) {
 System.out.println( "其他的登陸錯誤");
 error = "錯誤信息:" + e.getMessage();
 }
return new Result(false, error);
}

5.session

簡單說一下我理解的session和cookie的區(qū)別吧,session是保存在服務(wù)端的,cookie是保存在客戶端的,就是本地會有一個文件夾專門保存cookie。cookie主要是為了保存用戶狀態(tài)嘛,因?yàn)閔ttp是無狀態(tài)的連接,每次連接完就不會知道下一次是不是同一個用戶。但是保存用戶信息在很多應(yīng)用場景中都是必要的。而session比cookie更加安全,因?yàn)閟ession信息保存在服務(wù)端的,不容易被盜用。所以重要登陸信息還是應(yīng)該保存在session上。而且服務(wù)端能夠保存的session比較大,而單個cookie一般不超過20k.

session是怎么保存用戶信息的呢?就是一個用戶有一個sessionId,通過sessionId保存用戶信息。

session的使用:

session.setAttribute("key","value");
session.getAttribute("key");

6.登陸界面

總結(jié)

以上所述是小編給大家介紹的vue+SSM實(shí)現(xiàn)驗(yàn)證碼功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Vue?privide?和inject?依賴注入的使用詳解

    Vue?privide?和inject?依賴注入的使用詳解

    這篇文章主要介紹了Vue?privide?和inject?依賴注入的用法,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-10-10
  • 淺談vue實(shí)現(xiàn)數(shù)據(jù)監(jiān)聽的函數(shù) Object.defineProperty

    淺談vue實(shí)現(xiàn)數(shù)據(jù)監(jiān)聽的函數(shù) Object.defineProperty

    本篇文章主要介紹了淺談vue實(shí)現(xiàn)數(shù)據(jù)監(jiān)聽的函數(shù) Object.defineProperty,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • vue-router命名視圖的使用講解

    vue-router命名視圖的使用講解

    今天小編就為大家分享一篇關(guān)于vue-router命名視圖的使用講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • vue?路由切換過渡動效滑入滑出效果的實(shí)例代碼

    vue?路由切換過渡動效滑入滑出效果的實(shí)例代碼

    在支付寶賬單頁面有這樣一個特效切換過渡動效滑入滑出效果,非常方便實(shí)用,那么這個功能如何實(shí)現(xiàn)的呢?下面小編通過vue實(shí)現(xiàn)路由切換過渡動效滑入滑出效果,感興趣的朋友一起看看吧
    2022-03-03
  • Vue3后臺管理系統(tǒng)之創(chuàng)建和配置項(xiàng)目

    Vue3后臺管理系統(tǒng)之創(chuàng)建和配置項(xiàng)目

    后臺管理系統(tǒng)是我們?nèi)粘i_發(fā)學(xué)習(xí)經(jīng)常遇到的一個項(xiàng)目,下面這篇文章主要給大家介紹了關(guān)于Vue3后臺管理系統(tǒng)之創(chuàng)建和配置項(xiàng)目的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-09-09
  • Vue中使用vue-pdf嵌入和展示PDF文件

    Vue中使用vue-pdf嵌入和展示PDF文件

    在vue中實(shí)現(xiàn)在線預(yù)覽PDF文件我們可以使用vue-pdf來實(shí)現(xiàn),下面這篇文章主要介紹了Vue中使用vue-pdf嵌入和展示PDF文件的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-10-10
  • Vue el-table 默認(rèn)展開某一行的實(shí)例

    Vue el-table 默認(rèn)展開某一行的實(shí)例

    這篇文章主要介紹了Vue el-table 默認(rèn)展開某一行的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • vue在線預(yù)覽word、excel、pdf、txt、圖片的方法實(shí)例

    vue在線預(yù)覽word、excel、pdf、txt、圖片的方法實(shí)例

    最近工作中遇到了一個需要在線預(yù)覽文件的需求,所以這篇文章主要給大家介紹了vue在線預(yù)覽word、excel、pdf、txt、圖片的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • Vue之Dep和Observer的用法及說明

    Vue之Dep和Observer的用法及說明

    這篇文章主要介紹了Vue之Dep和Observer的用法及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • vue-cli 項(xiàng)目打包完成后運(yùn)行文件路徑報(bào)錯問題

    vue-cli 項(xiàng)目打包完成后運(yùn)行文件路徑報(bào)錯問題

    這篇文章主要介紹了vue-cli 項(xiàng)目打包完成后運(yùn)行文件路徑報(bào)錯問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07

最新評論

始兴县| 沾益县| 托里县| 教育| 定南县| 保德县| 庆云县| 庄浪县| 邵阳市| 聊城市| 来凤县| 高州市| 成都市| 雅安市| 义马市| 英德市| 灵宝市| 嘉兴市| 九江县| 库伦旗| 台南市| 祥云县| 龙南县| 霸州市| 和顺县| 仲巴县| 克拉玛依市| 化州市| 长治市| 拜城县| 孝义市| 岳池县| 梁山县| 刚察县| 虞城县| 徐州市| 泰兴市| 富顺县| 确山县| 西华县| 吉隆县|