第八节:cas4.0 添加验证码

  1. 注意修改:msxf.

    1.添加com.msxf.sso

    2.复制default_views.properties为msxf..properties,并把文件内的连接default修改为msxf.

    3.修改cas.properties中的cas.viewResolver.basename=msxf

    4.复制jsp下的default的文件为msxf。

  2. /** 
  3.  * @see ------------------------------------------------------------------------------------------------------------------------ 
  4.  * @see CAS登录页添加验证码 
  5.  * @see 0.这年头验证码一般用来防止帐号被暴力破解,如果我们的系统是走专线的,也就是说放在内网,那完全没必要搞验证码 
  6.  * @see 1.由于CAS使用了Spring Web Flow框架,所以我们想在表单加属性就直接找\WEB-INF\login-webflow.xml 
  7.  * @see 2.在84行<view-state id="viewLoginForm">中找到表单的两个属性,我们加一个<binding property="captcha"/> 
  8.  * @see   同样该标签中会发现model="credential"配置,所以我们就在该文件找credential对应的实体类配置 
  9.  * @see   发现是在27行设置的,其值为org.jasig.cas.authentication.UsernamePasswordCredential 
  10.  * @see   这是一个用来接收前台表单参数的JavaBean,我们这里要在表单上加一个参数captcha,所以继承它就行了 
  11.  * @see 3.创建com.msxf.sso.model.UsernamePasswordCaptchaCredential extends UsernamePasswordCredential 
  12.  * @see   再加上captcha属性,以及captcha对应的setter和getter 
  13.  * @see   再修改login-webflow.xml第27行credential对应实体类为com.msxf.sso.model.UsernamePasswordCaptchaCredential 
  14.  * @see 4.接下来添加校验验证码的流程 
  15.  * @see   继续看<view-state id="viewLoginForm">,这里我们会发现表单实际的提交等动作是由authenticationViaFormAction处理的 
  16.  * @see   authenticationViaFormAction是被配置在cas-servlet.xml中的第233行 
  17.  * @see   我们要在原有表单处理逻辑的基础上增加验证码,所以就扩展authenticationViaFormAction 
  18.  * @see   创建com.msxf.sso.authentication.AuthenticationViaCaptchaFormAction extends AuthenticationViaFormAction 
  19.  * @see   在AuthenticationViaCaptchaFormAction中增加一个validateCaptcha()方法用来校验验证码 
  20.  * @see   然后将cas-servlet.xml中的authenticationViaFormAction改为新扩展的AuthenticationViaCaptchaFormAction 
  21.  * @see   同样login-webflow.xml中的三处authenticationViaFormAction改为新扩展的AuthenticationViaCaptchaFormAction 
  22.  * @see 5.将messages.properties的一些提示文字改为中文 
  23.  * @see   required.username=必须输入帐号 
  24.  * @see   required.password=必须输入密码 
  25.  * @see   required.captcha=必须输入验证码 
  26.  * @see   error.authentication.captcha.bad=验证码不正确 
  27.  * @see   authenticationFailure.AccountNotFoundException=登录失败--帐号不正确 
  28.  * @see   authenticationFailure.FailedLoginException=登录失败--密码不正确 
  29.  * @see   authenticationFailure.UNKNOWN=未知错误 
  30.  * @see ------------------------------------------------------------------------------------------------------------------------ 
  31.  * @author wangyf 

下面是login-webflow.xml中的改动部分

  1. <!--  
  2. <var name="credential" class="org.jasig.cas.authentication.UsernamePasswordCredential"/> 
  3.  -->  
  4. <!-- 新加的用于接收前台表单验证码字段captcha的JavaBean -->  
  5. <var name="credential" class="com.msxf.sso.model.UsernamePasswordCaptchaCredential"/>  
  6.   
  7.   
  8. <view-state id="viewLoginForm" view="casLoginView" model="credential">  
  9.     <binder>  
  10.         <binding property="username"/>  
  11.         <binding property="password"/>  
  12.         <!-- 前台添加表单添加验证码字段captcha -->  
  13.         <binding property="captcha"/>  
  14.     </binder>  
  15.     <on-entry>  
  16.         <set name="viewScope.commandName" value="'credential'"/>  
  17.     </on-entry>  
  18.     <transition on="submit" bind="true" validate="true" to="validateCaptcha">  
  19.         <evaluate expression="authenticationViaCaptchaFormAction.doBind(flowRequestContext, flowScope.credential)"/>  
  20.     </transition>  
  21. </view-state>  
  22.   
  23.   
  24. <!-- 新添加的校验验证码 -->  
  25. <action-state id="validateCaptcha">  
  26.     <evaluate expression="authenticationViaCaptchaFormAction.validateCaptcha(flowRequestContext, flowScope.credential, messageContext)"/>  
  27.     <transition on="error" to="generateLoginTicket"/>  
  28.     <transition on="success" to="realSubmit"/>  
  29. </action-state>  
  30.   
  31.   
  32. <action-state id="realSubmit">  
  33.     <evaluate expression="authenticationViaCaptchaFormAction.submit(flowRequestContext, flowScope.credential, messageContext)"/>  
  34.     <transition on="warn" to="warn"/>  
  35.     <transition on="success" to="sendTicketGrantingTicket"/>  
  36.     <transition on="successWithWarnings" to="showMessages"/>  
  37.     <transition on="authenticationFailure" to="handleAuthenticationFailure"/>  
  38.     <transition on="error" to="generateLoginTicket"/>  
  39. </action-state>  

下面是扩展的UsernamePasswordCaptchaCredential.java

  1. package com.msxf.sso.model;  
  2.   
  3. import org.jasig.cas.authentication.UsernamePasswordCredential;  
  4.   
  5. /** 
  6.  * 自定义的接收登录验证码的实体类 
  7.  * @create 2016-6-3 下午14:13:35 
  8.  * @author wangyf
  9.  */  
  10. public class UsernamePasswordCaptchaCredential extends UsernamePasswordCredential {  
  11.     private static final long serialVersionUID = 8317889802836113837L;  
  12.       
  13.     private String captcha;  
  14.   
  15.     public String getCaptcha() {  
  16.         return captcha;  
  17.     }  
  18.   
  19.     public void setCaptcha(String captcha) {  
  20.         this.captcha = captcha;  
  21.     }  
  22. }  

下面是扩展的AuthenticationViaCaptchaFormAction.java

  1. package com.msxf.sso.authentication;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4. import javax.servlet.http.HttpSession;  
  5.   
  6. import org.jasig.cas.authentication.Credential;  
  7. import org.jasig.cas.web.flow.AuthenticationViaFormAction;  
  8. import org.jasig.cas.web.support.WebUtils;  
  9. import org.springframework.binding.message.MessageBuilder;  
  10. import org.springframework.binding.message.MessageContext;  
  11. import org.springframework.util.StringUtils;  
  12. import org.springframework.webflow.execution.RequestContext;  
  13.   
  14. import com.msxf.sso.model.UsernamePasswordCaptchaCredential;  
  15.   
  16. /** 
  17.  * 自定义的处理验证码登录逻辑的Action 
  18.  * @author wangyf
  19.  */  
  20. public class AuthenticationViaCaptchaFormAction extends AuthenticationViaFormAction {  
  21.     public final String validateCaptcha(final RequestContext context, final Credential credential, final MessageContext messageContext){  
  22.         final HttpServletRequest request = WebUtils.getHttpServletRequest(context);  
  23.         HttpSession session = request.getSession();  
  24.         String rand = (String)session.getAttribute("rand");  
  25.         session.removeAttribute("rand");  
  26.           
  27.         UsernamePasswordCaptchaCredential upc = (UsernamePasswordCaptchaCredential)credential;  
  28.         String captcha = upc.getCaptcha();  
  29.           
  30.         System.out.println("获取Session验证码-->" + rand);  
  31.         System.out.println("获取表单输入验证码-->" + captcha);  
  32.   
  33.         if(!StringUtils.hasText(rand) || !StringUtils.hasText(captcha)){  
  34.             messageContext.addMessage(new MessageBuilder().error().code("required.captcha").build());  
  35.             return "error";  
  36.         }  
  37.         if(captcha.equals(rand)){  
  38.             return "success";  
  39.         }  
  40.         //这段网上这么写的messageContext.addMessage(new MessageBuilder().code("required.captcha").build());  
  41.         //实际上这么写是org.springframework.binding.message.INFO级别的,这会导致前台表单无法显示这里的错误信息  
  42.         messageContext.addMessage(new MessageBuilder().error().code("error.authentication.captcha.bad").build());  
  43.         return "error";  
  44.     }  
  45. }  

下面是cas-servlet.xml中的改动部分

  1. <!--   
  2. <bean id="authenticationViaFormAction" class="org.jasig.cas.web.flow.AuthenticationViaFormAction"  
  3.         p:centralAuthenticationService-ref="centralAuthenticationService"  
  4.         p:warnCookieGenerator-ref="warnCookieGenerator"  
  5.         p:ticketRegistry-ref="ticketRegistry"/>  
  6.  -->  
  7. <!-- 新添加的用于校验验证码的Action -->  
  8. <bean id="authenticationViaCaptchaFormAction" class="com.msxf.sso.authentication.AuthenticationViaCaptchaFormAction"  
  9.         p:centralAuthenticationService-ref="centralAuthenticationService"  
  10.         p:warnCookieGenerator-ref="warnCookieGenerator"  
  11.         p:ticketRegistry-ref="ticketRegistry"/>  

这里要把jsp下的default的文件复制为msxf。下面是我的登录页\\WEB-INF\\view\\jsp\\msxf\\ui\\casLoginView.jsp

  1. <%@ page pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
  3. <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>  
  4.   
  5. <c:set var="ctx" value="${pageContext.request.contextPath}" scope="session"/>  
  6.   
  7. <!DOCTYPE HTML>  
  8. <html>  
  9. <head>  
  10.     <meta charset="UTF-8"/>  
  11.     <title>CAS单点登录系统</title>  
  12.     <link rel="icon" type="image/x-icon" href="${ctx}/favicon.ico"/>  
  13.     <script type="text/javascript" src="${ctx}/js/jquery-1.10.2.min.js"></script>  
  14.     <script type="text/javascript" src="${ctx}/js/jquery-ui-1.10.2.min.js"></script>  
  15.     <script type="text/javascript" src="${ctx}/js/cas.js"></script>  
  16.     <!--[if lt IE 9]>  
  17.         <script src="${ctx}/js/html5shiv-3.7.2.min.js" type="text/javascript"></script>  
  18.     <![endif]-->  
  19. </head>  
  20.   
  21. <style>  
  22. body {background-color: #CBE0C9;}  
  23. #msg {padding:20px; margin-bottom:10px;}  
  24. #msg.errors {border:1px dotted #BB0000; color:#BB0000; padding-left:100px; background:url(${ctx}/images/error.gif) no-repeat 20px center;}  
  25. </style>  
  26.   
  27. <body>  
  28. <c:if test="${not pageContext.request.secure}">  
  29.     <div id="msg" class="errors">  
  30.         <h2>Non-secure Connection</h2>  
  31.         <p>You are currently accessing CAS over a non-secure connection.  Single Sign On WILL NOT WORK.  In order to have single sign on work, you MUST log in over HTTPS.</p>  
  32.     </div>  
  33. </c:if>  
  34. <form:form method="post" commandName="${commandName}" htmlEscape="true">  
  35.     <!--   
  36.     cssClass用于指定表单元素CSS样式名,相当于HTML元素的class属性  
  37.     cssStyle用于指定表单元素样式,相当于HTML元素的style属性  
  38.     cssErrorClass用于指定表单元素发生错误时对应的样式  
  39.     path属性用于绑定表单对象的属性值,它支持级联属性,比如path="user.userName"将调用表单对象getUser.getUserName()绑定表单对象的属性值  
  40.      -->  
  41.     <form:errors path="*" id="msg" cssClass="errors" element="div" htmlEscape="false"/>  
  42.     <input type="hidden" name="lt" value="${loginTicket}"/>  
  43.     <input type="hidden" name="execution" value="${flowExecutionKey}"/>  
  44.     <input type="hidden" name="_eventId" value="submit"/>  
  45.     <table border="9">  
  46.         <tr>  
  47.             <td>  
  48.                 <c:if test="${not empty sessionScope.openIdLocalId}">  
  49.                     <strong>${sessionScope.openIdLocalId}</strong>  
  50.                     <input type="hidden" name="username" value="${sessionScope.openIdLocalId}"/>  
  51.                 </c:if>  
  52.                 <c:if test="${empty sessionScope.openIdLocalId}">  
  53.                     <form:input tabindex="1" path="username" placeholder="帐号" htmlEscape="true" maxlength="16" size="25"/>  
  54.                 </c:if>  
  55.             </td>  
  56.         </tr>  
  57.         <tr>  
  58.             <td>  
  59.                 <form:password tabindex="2" path="password" placeholder="密码" htmlEscape="true" maxlength="16" size="25"/>  
  60.             </td>  
  61.         </tr>  
  62.         <tr>  
  63.             <td>  
  64.                 <form:input tabindex="3" path="captcha" placeholder="验证码" htmlEscape="true" maxlength="4" size="15"/>  
  65.                 <img style="cursor:pointer; vertical-align:middle;" src="captcha.jsp" onClick="this.src='captcha.jsp?time'+Math.random();">  
  66.             </td>  
  67.         </tr>  
  68.         <%--  
  69.         <tr>  
  70.             <td>  
  71.                 <input type="checkbox" tabindex="3" name="warn" value="true"/>  
  72.                 <label for="warn">转向其他站点前提示我</label>  
  73.             </td>  
  74.         </tr>  
  75.         --%>  
  76.         <tr>  
  77.             <td>  
  78.                 <input type="submit" tabindex="3" value="登录"/>  
  79.             </td>  
  80.         </tr>  
  81.     </table>  
  82. </form:form>  
  83. </body>  
  84. </html>  

下面是我的登录成功页\\WEB-INF\\view\\jsp\\msxf\\ui\\casGenericSuccess.jsp

  1. <%@ page pageEncoding="UTF-8"%>  
  2. <body style="background-color:#CBE0C9;">  
  3.     <span style="color:red; font-size:64px; font-weight:bold;">登录成功</span>  
  4. </body>  

最后是用到的用于生成验证码的\\WebRoot\\captcha.jsp

  1. <%@ page contentType="image/jpeg; charset=UTF-8" pageEncoding="UTF-8"%>  
  2. <%@ page import="java.awt.Color"%>  
  3. <%@ page import="java.util.Random"%>  
  4. <%@ page import="java.awt.image.BufferedImage"%>  
  5. <%@ page import="java.awt.Graphics"%>  
  6. <%@ page import="java.awt.Font"%>  
  7. <%@ page import="javax.imageio.ImageIO"%>  
  8.   
  9. <%--  
  10. 这是一个用于生成随机验证码图片的JSP文件  
  11. 这里contentType="image/jpeg"用来告诉容器:该JSP文件的输出格式为图片格式  
  12. 登录网站时,通常要求输入随机生成的验证码,这是为了防止有些软件会自动生成破解密码  
  13. 这些验证码一般都是通过图片显示出来的,并且图片上有很多不规则的线条或者图案来干扰,使得软件不容易识别图案上的验证码  
  14. --%>  
  15.   
  16. <%!  
  17. /**  
  18.  * 定义验证码类型  
  19.  * @see 1--纯数字,2--纯汉字  
  20.  * @see 这里也支持数字和英文字母组合,但考虑到不好辨认,故注释了这部分代码,详见69行  
  21.  */  
  22. int captchaType = 1;  
  23.   
  24. /**  
  25.  * 生成给定范围内的随机颜色  
  26.  */  
  27. Color getRandColor(Random random, int fc, int bc){  
  28.     if(fc>255) fc = 255;  
  29.     if(bc>255) bc = 255;  
  30.     int r = fc + random.nextInt(bc-fc);  
  31.     int g = fc + random.nextInt(bc-fc);  
  32.     int b = fc + random.nextInt(bc-fc);  
  33.     return new Color(r, g, b);  
  34. }  
  35. %>  
  36.   
  37. <%  
  38. //设置页面不缓存  
  39. response.setHeader("Pragma", "No-cache");  
  40. response.setHeader("Cache-Control", "no-cache");  
  41. response.setDateHeader("Expires", 0);  
  42.   
  43. //创建随机类实例  
  44. Random random = new Random();  
  45. //定义图片尺寸  
  46. int width=60*this.captchaType, height=(this.captchaType==1)?20:30;  
  47. //创建内存图像  
  48. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
  49. //获取图形上下文  
  50. Graphics g = image.getGraphics();  
  51. //设定背景色  
  52. g.setColor(this.getRandColor(random, 200, 250));  
  53. //设定图形的矩形坐标及尺寸  
  54. g.fillRect(0, 0, width, height);  
  55.   
  56. String sRand = "";  
  57. if(this.captchaType == 1){  
  58.     //图片背景随机产生50条干扰线作为噪点  
  59.     g.setColor(this.getRandColor(random, 160, 200));  
  60.     g.setFont(new Font("Times New Roman", Font.PLAIN, 18));  
  61.     for(int i=0; i<50; i++){  
  62.         int x11 = random.nextInt(width);  
  63.         int y11 = random.nextInt(height);  
  64.         int x22 = random.nextInt(width);  
  65.         int y22 = random.nextInt(height);  
  66.         g.drawLine(x11, y11, x11+x22, y11+y22);  
  67.     }  
  68.     //取随机产生的4个数字作为验证码  
  69.     //String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";  
  70.     //String str = "abcdefghkmnpqrstwxyABCDEFGHJKLMNPRSTWXYZ123456789";  
  71.     for(int i=0; i<4; i++){  
  72.         //String rand = String.valueOf(str.charAt(random.nextInt(62)));  
  73.         //String rand = String.valueOf(str.charAt(random.nextInt(49)));  
  74.         String rand = String.valueOf(random.nextInt(10));  
  75.         sRand += rand;  
  76.         g.setColor(this.getRandColor(random, 10, 150));  
  77.         //将此数字画到图片上  
  78.         g.drawString(rand, 13*i+6, 16);  
  79.     }  
  80. }else{  
  81.     //设定备选汉字  
  82.     String base = "\u7684\u4e00\u4e86\u662f\u6211\u4e0d\u5728\u4eba\u4eec\u6709\u6765\u4ed6\u8fd9\u4e0a\u7740" +  
  83.                   "\u4e2a\u5730\u5230\u5927\u91cc\u8bf4\u5c31\u53bb\u5b50\u5f97\u4e5f\u548c\u90a3\u8981\u4e0b" +  
  84.                   "\u770b\u5929\u65f6\u8fc7\u51fa\u5c0f\u4e48\u8d77\u4f60\u90fd\u628a\u597d\u8fd8\u591a\u6ca1" +  
  85.                   "\u4e3a\u53c8\u53ef\u5bb6\u5b66\u53ea\u4ee5\u4e3b\u4f1a\u6837\u5e74\u60f3\u751f\u540c\u8001" +  
  86.                   "\u4e2d\u5341\u4ece\u81ea\u9762\u524d\u5934\u9053\u5b83\u540e\u7136\u8d70\u5f88\u50cf\u89c1" +  
  87.                   "\u4e24\u7528\u5979\u56fd\u52a8\u8fdb\u6210\u56de\u4ec0\u8fb9\u4f5c\u5bf9\u5f00\u800c\u5df1" +  
  88.                   "\u4e9b\u73b0\u5c71\u6c11\u5019\u7ecf\u53d1\u5de5\u5411\u4e8b\u547d\u7ed9\u957f\u6c34\u51e0" +  
  89.                   "\u4e49\u4e09\u58f0\u4e8e\u9ad8\u624b\u77e5\u7406\u773c\u5fd7\u70b9\u5fc3\u6218\u4e8c\u95ee" +  
  90.                   "\u4f46\u8eab\u65b9\u5b9e\u5403\u505a\u53eb\u5f53\u4f4f\u542c\u9769\u6253\u5462\u771f\u5168" +  
  91.                   "\u624d\u56db\u5df2\u6240\u654c\u4e4b\u6700\u5149\u4ea7\u60c5\u8def\u5206\u603b\u6761\u767d" +  
  92.                   "\u8bdd\u4e1c\u5e2d\u6b21\u4eb2\u5982\u88ab\u82b1\u53e3\u653e\u513f\u5e38\u6c14\u4e94\u7b2c" +  
  93.                   "\u4f7f\u5199\u519b\u5427\u6587\u8fd0\u518d\u679c\u600e\u5b9a\u8bb8\u5feb\u660e\u884c\u56e0" +  
  94.                   "\u522b\u98de\u5916\u6811\u7269\u6d3b\u90e8\u95e8\u65e0\u5f80\u8239\u671b\u65b0\u5e26\u961f" +  
  95.                   "\u5148\u529b\u5b8c\u5374\u7ad9\u4ee3\u5458\u673a\u66f4\u4e5d\u60a8\u6bcf\u98ce\u7ea7\u8ddf" +  
  96.                   "\u7b11\u554a\u5b69\u4e07\u5c11\u76f4\u610f\u591c\u6bd4\u9636\u8fde\u8f66\u91cd\u4fbf\u6597" +  
  97.                   "\u9a6c\u54ea\u5316\u592a\u6307\u53d8\u793e\u4f3c\u58eb\u8005\u5e72\u77f3\u6ee1\u65e5\u51b3" +  
  98.                   "\u767e\u539f\u62ff\u7fa4\u7a76\u5404\u516d\u672c\u601d\u89e3\u7acb\u6cb3\u6751\u516b\u96be" +  
  99.                   "\u65e9\u8bba\u5417\u6839\u5171\u8ba9\u76f8\u7814\u4eca\u5176\u4e66\u5750\u63a5\u5e94\u5173" +  
  100.                   "\u4fe1\u89c9\u6b65\u53cd\u5904\u8bb0\u5c06\u5343\u627e\u4e89\u9886\u6216\u5e08\u7ed3\u5757" +  
  101.                   "\u8dd1\u8c01\u8349\u8d8a\u5b57\u52a0\u811a\u7d27\u7231\u7b49\u4e60\u9635\u6015\u6708\u9752" +  
  102.                   "\u534a\u706b\u6cd5\u9898\u5efa\u8d76\u4f4d\u5531\u6d77\u4e03\u5973\u4efb\u4ef6\u611f\u51c6" +  
  103.                   "\u5f20\u56e2\u5c4b\u79bb\u8272\u8138\u7247\u79d1\u5012\u775b\u5229\u4e16\u521a\u4e14\u7531" +  
  104.                   "\u9001\u5207\u661f\u5bfc\u665a\u8868\u591f\u6574\u8ba4\u54cd\u96ea\u6d41\u672a\u573a\u8be5" +  
  105.                   "\u5e76\u5e95\u6df1\u523b\u5e73\u4f1f\u5fd9\u63d0\u786e\u8fd1\u4eae\u8f7b\u8bb2\u519c\u53e4" +  
  106.                   "\u9ed1\u544a\u754c\u62c9\u540d\u5440\u571f\u6e05\u9633\u7167\u529e\u53f2\u6539\u5386\u8f6c" +  
  107.                   "\u753b\u9020\u5634\u6b64\u6cbb\u5317\u5fc5\u670d\u96e8\u7a7f\u5185\u8bc6\u9a8c\u4f20\u4e1a" +  
  108.                   "\u83dc\u722c\u7761\u5174\u5f62\u91cf\u54b1\u89c2\u82e6\u4f53\u4f17\u901a\u51b2\u5408\u7834" +  
  109.                   "\u53cb\u5ea6\u672f\u996d\u516c\u65c1\u623f\u6781\u5357\u67aa\u8bfb\u6c99\u5c81\u7ebf\u91ce" +  
  110.                   "\u575a\u7a7a\u6536\u7b97\u81f3\u653f\u57ce\u52b3\u843d\u94b1\u7279\u56f4\u5f1f\u80dc\u6559" +  
  111.                   "\u70ed\u5c55\u5305\u6b4c\u7c7b\u6e10\u5f3a\u6570\u4e61\u547c\u6027\u97f3\u7b54\u54e5\u9645" +  
  112.                   "\u65e7\u795e\u5ea7\u7ae0\u5e2e\u5566\u53d7\u7cfb\u4ee4\u8df3\u975e\u4f55\u725b\u53d6\u5165" +  
  113.                   "\u5cb8\u6562\u6389\u5ffd\u79cd\u88c5\u9876\u6025\u6797\u505c\u606f\u53e5\u533a\u8863\u822c" +  
  114.                   "\u62a5\u53f6\u538b\u6162\u53d4\u80cc\u7ec6";  
  115.     //图片背景增加噪点  
  116.     g.setColor(this.getRandColor(random, 160, 200));  
  117.     g.setFont(new Font("Times New Roman", Font.PLAIN, 14));   
  118.     for(int i=0; i<6; i++){  
  119.         g.drawString("*********************************************", 0, 5*(i+2));  
  120.     }  
  121.     //设定验证码汉字的备选字体{"宋体", "新宋体", "黑体", "楷体", "隶书"}  
  122.     String[] fontTypes = {"\u5b8b\u4f53", "\u65b0\u5b8b\u4f53", "\u9ed1\u4f53", "\u6977\u4f53", "\u96b6\u4e66"};  
  123.     //取随机产生的4个汉字作为验证码  
  124.     for(int i=0; i<4; i++){  
  125.         int start = random.nextInt(base.length());  
  126.         String rand = base.substring(start, start+1);  
  127.         sRand += rand;  
  128.         g.setColor(this.getRandColor(random, 10, 150));  
  129.         g.setFont(new Font(fontTypes[random.nextInt(fontTypes.length)], Font.BOLD, 18+random.nextInt(4)));  
  130.         //将此汉字画到图片上  
  131.         g.drawString(rand, 24*i+10+random.nextInt(8), 24);  
  132.     }  
  133. }  
  134.   
  135. //将验证码存入SESSION  
  136. session.setAttribute("rand", sRand);  
  137. //图像生效  
  138. g.dispose();  
  139. //输出图像到页面  
  140. ImageIO.write(image, "PNG", response.getOutputStream());  
  141.   
  142. //若无下面两行代码,则每次请求生成验证码图片时,尽管不会影响到图片的生成以及验证码的校验,但控制台都会滚动下面的异常  
  143. //java.lang.IllegalStateException: getOutputStream() has already been called for this response  
  144. out.clear();  
  145. out = pageContext.pushBody();  
  146. %>  

注意修改:msxf.

1.添加com.msxf.sso

2.复制default_views.properties为msxf..properties,并把文件内的连接default修改为msxf.

3.修改cas.properties中的cas.viewResolver.basename=msxf

4.复制jsp下的default的文件为msxf。

猜你喜欢

转载自starbhhc.iteye.com/blog/2303136