3.4.11
配置Jdbc访问
配置验证码
参考连接 效果如下
创建验证码访问controller
验证码生成工具类,见代码1
public class AuthCode {
public static final int AUTHCODE_LENGTH =
4;
public static final int SINGLECODE_WIDTH =
15;
public static final int SINGLECODE_HEIGHT =
30;
public static final int SINGLECODE_GAP =
4;
public static final int IMG_WIDTH = AUTHCODE_LENGTH * (SINGLECODE_WIDTH + SINGLECODE_GAP);
public static final int IMG_HEIGHT = SINGLECODE_HEIGHT;
public static String
getAuthCode() {
String authCode =
"";
for (
int i =
0; i < AUTHCODE_LENGTH; i++) {
authCode += (
new Random()).nextInt(
10);
}
return authCode;
}
public static BufferedImage
getAuthImg(String authCode) {
BufferedImage img =
new BufferedImage(IMG_WIDTH, IMG_HEIGHT, BufferedImage.TYPE_INT_BGR);
Graphics g = img.getGraphics();
g.setColor(Color.decode(
"#5cbdaa"));
g.fillRect(
0,
0, IMG_WIDTH, IMG_HEIGHT);
g.setColor(Color.BLACK);
g.setFont(
new Font(
"宋体", Font.BOLD, SINGLECODE_HEIGHT +
5));
char c;
for (
int i =
0; i < authCode.toCharArray().length; i++) {
c = authCode.charAt(i);
g.drawString(c +
"", i * (SINGLECODE_WIDTH + SINGLECODE_GAP) + SINGLECODE_GAP /
2, IMG_HEIGHT);
}
Random random =
new Random();
for (
int i =
0; i <
20; i++) {
int x = random.nextInt(IMG_WIDTH);
int y = random.nextInt(IMG_HEIGHT);
int x2 = random.nextInt(IMG_WIDTH);
int y2 = random.nextInt(IMG_HEIGHT);
g.drawLine(x, y, x + x2, y + y2);
}
return img;
}
}
controller
public class getAuthCodeController implements Controller, InitializingBean {
@Override
public void afterPropertiesSet()
throws Exception {
}
@Override
public ModelAndView
handleRequest(HttpServletRequest request, HttpServletResponse response)
throws Exception {
String authCode = AuthCode.getAuthCode();
request.getSession().setAttribute(
"vcode", authCode);
response.setHeader(
"Pragma",
"no-cache");
response.setHeader(
"Cache-Control",
"no-cache");
response.setDateHeader(
"Expires",
0);
response.setContentType(
"image/jpeg");
try {
ImageIO.write(AuthCode.getAuthImg(authCode),
"JPEG", response.getOutputStream());
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
配置cas-server.xml
配置 <bean id="handlerMappingC" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 加入 <prop key="/authcode">authCodeController</prop> 新增一个bean <bean id="authCodeController" class="com.xxx.authcode.getAuthCodeController" />
配置web.xml 在web.xml的403.hml下加入路径,大概在130行
<servlet-mapping>
<servlet-name>cas
</servlet-name>
<url-pattern>/403.html
</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>cas
</servlet-name>
<url-pattern>/authcode
</url-pattern>
</servlet-mapping>
添加验证码验证功能
修改页面 接收表单的java bean加上vcode属性 CAS接收和验证登录表单的java bean是UsernamePasswordCredentials,这里我是增加了一个自己的UsernamePasswordVCodeCredentials的类,当然这里要继承UsernamePasswordCredentials类;
public class UsernamePasswordVCodeCredentials extends UsernamePasswordCredentials {
private static final long serialVersionUID =
1L;
@NotNull
@Size(min=
1,message =
"required.vcode")
private String vcode;
public String
getVcode() {
return vcode;
}
public void setVcode(String vcode) {
this.vcode = vcode;
}
}
更改login-webflow.xml配置 将<var name="credentials" class="org.jasig.cas.authentication.principal.UsernamePasswordCredentials" /> 改为<var name="credentials" class="com.xxx.UsernamePasswordVCodeCredentials" />增加对vcode的校验方法 增加一个类,就叫MyAuthenticationViaFormAction
public class MyAuthenticationViaFormAction extends AuthenticationViaFormAction {
private final String ERROR =
"error";
private final String SUCCESS =
"success";
public final String
validatorCode(
final RequestContext context,
final Credentials credentials,
final MessageContext messageContext)
throws Exception {
String vcode = (String) WebUtils.getHttpServletRequest(context).getSession().getAttribute(
"vcode");
UsernamePasswordVCodeCredentials upv = (UsernamePasswordVCodeCredentials) credentials;
if (StringUtils.isBlank(upv.getVcode()) || StringUtils.isBlank(vcode))
return ERROR;
if (upv.getVcode().toLowerCase().equals(vcode.toLowerCase())) {
return SUCCESS;
}
MessageBuilder msgBuilder =
new MessageBuilder();
msgBuilder.defaultText(
"验证码有误!");
messageContext.addMessage(msgBuilder.error().build());
return ERROR;
}
}
修改cas-servlet.xml 将<bean id="authenticationViaFormAction" class="org.jasig.cas.web.flow.AuthenticationViaFormAction" p:centralAuthenticationService-ref="centralAuthenticationService" p:warnCookieGenerator-ref="warnCookieGenerator"/>
改成<bean id="authenticationViaFormAction" class="com.xxx.MyAuthenticationViaFormAction" p:centralAuthenticationService-ref="centralAuthenticationService" p:warnCookieGenerator-ref="warnCookieGenerator" />
修改login-webflow.xml 找到id=”viewLoginForm”这个位置,将这个view-state换成;
<view-state id="viewLoginForm" view="casLoginView" model="credentials">
<binder>
<binding property="username" />
<binding property="password" />
<binding property="vcode" />
</binder>
<on-entry>
<set name="viewScope.commandName" value="'credentials'" />
</on-entry>
<transition on="submit" bind="true" validate="true" to="validatorCode">
<evaluate expression="authenticationViaFormAction.doBind(flowRequestContext, flowScope.credentials)" />
</transition>
</view-state>
在下面加入:
<action-state id="validatorCode">
<evaluate expression="authenticationViaFormAction.validatorCode(flowRequestContext, flowScope.credentials, messageContext)" />
<transition on="error" to="generateLoginTicket" />
<transition on="success" to="realSubmit" />
</action-state>
到此,验证码功能添加完成
转载请注明原文地址: https://ju.6miu.com/read-8764.html