1.security.xml配置
- <!--负责认证处理的filter -->
- <beans:bean id="authenticationProcessingFilter" class="com.XXX.security.filter.KMSAuthenticationProcessingFilter">
- <custom-filter before="AUTHENTICATION_PROCESSING_FILTER"/>
- <beans:property name="authenticationManager" ref="authenticationManager"/>
- <beans:property name="validateRandom" value="true"/>
- </beans:bean>
2.authenticationProcessingFilter
- package com.XXX.security.filter;
- import org.springframework.security.Authentication;
- import org.springframework.security.AuthenticationException;
- import org.springframework.security.SpringSecurityMessageSource;
- import org.springframework.security.providers.AbstractAuthenticationToken;
- import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
- import org.springframework.security.ui.webapp.AuthenticationProcessingFilter;
- import org.springframework.security.util.TextUtils;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
- /**
- * @author: jetyou@foxmail.com
- * @date: 2011-10-20
- * @time: 16:02:01
- * @desc:
- */
- public class KmsAuthenticationProcessingFilter extends AuthenticationProcessingFilter {
- public static final String SPRING_SECURITY_FORM_RANDOM_KEY = "j_random";
- public boolean isValidateRandom = true;
- public String typeParameter = SPRING_SECURITY_FORM_TYPE_KEY;
- public String randomParameter = SPRING_SECURITY_FORM_RANDOM_KEY;
- public Authentication attemptAuthentication(HttpServletRequest request) throws AuthenticationException {
- HttpSession session = request.getSession(false);
- //如果验证验证码
- if (isValidateRandom) {
- String requestRandom = request.getParameter(SPRING_SECURITY_FORM_TYPE_KEY);
- String sessionRandom = (String) session.getAttribute(SPRING_SECURITY_FORM_RANDOM_KEY);
- validateRandom(requestRandom, sessionRandom);
- }
- XXX;
- return this.getAuthenticationManager().authenticate(authRequest);
- }
- /**
- * Provided so that subclasses may configure what is put into the authentication request's details
- * property.
- *
- * @param request that an authentication request is being created for
- * @param authRequest the authentication request object that should have its details set
- */
- protected void setDetail(HttpServletRequest request, AbstractAuthenticationToken authRequest) {
- authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
- }
- /**
- * 验证验证码
- *
- * @param requestRandom
- * @param sessionRandom
- */
- protected void validateRandom(String requestRandom, String sessionRandom) {
- if (requestRandom == null || sessionRandom == null || requestRandom.trim().equals("") || sessionRandom.trim().equals(""))
- throw new BadRandomCodeException(messages.getMessage("KmsAuthenticationProcessingFilter.badRandom", "Bad Random Code"));
- if (!requestRandom.toLowerCase().equals(sessionRandom.toLowerCase()))
- throw new BadRandomCodeException(messages.getMessage("KmsAuthenticationProcessingFilter.badRandom", "Bad Random Code"));
- }
- protected String obtainRandom(HttpServletRequest request) {
- return request.getParameter(randomParameter);
- }
- public void setValidateRandom(boolean validateRandom) {
- isValidateRandom = validateRandom;
- }
- public void setRandomParameter(String randomParameter) {
- this.randomParameter = randomParameter;
- }
- }
3.web.xml
- <servlet>
- <servlet-name>Kaptcha</servlet-name>
- <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
- <init-param>
- <param-name>kaptcha.border</param-name>
- <param-value>no</param-value>
- </init-param>
- <init-param>
- <param-name>kaptcha.border.color</param-name>
- <param-value>red</param-value>
- </init-param>
- <init-param>
- <param-name>kaptcha.border.thickness</param-name>
- <param-value>4</param-value>
- </init-param>
- <init-param>
- <param-name>kaptcha.p_w_picpath.width</param-name>
- <param-value>60</param-value>
- </init-param>
- <init-param>
- <param-name>kaptcha.p_w_picpath.height</param-name>
- <param-value>30</param-value>
- </init-param>
- <init-param>
- <param-name>kaptcha.producer.impl</param-name>
- <param-value>com.google.code.kaptcha.impl.DefaultKaptcha </param-value>
- </init-param>
- <init-param>
- <param-name>kaptcha.textproducer.impl</param-name>
- <param-value>com.google.code.kaptcha.text.impl.DefaultTextCreator</param-value>
- </init-param>
- <init-param>
- <param-name>kaptcha.textproducer.char.string</param-name>
- <param-value>abcde2345678gfynmnpwx </param-value>
- </init-param>
- <init-param>
- <param-name>kaptcha.textproducer.char.length</param-name>
- <param-value>4</param-value>
- </init-param>
- <init-param>
- <param-name>kaptcha.textproducer.font.names</param-name>
- <param-value>Arial, Courier</param-value>
- </init-param>
- <init-param>
- <param-name>kaptcha.textproducer.font.size</param-name>
- <param-value>20</param-value>
- </init-param>
- <init-param>
- <param-name>kaptcha.textproducer.font.color</param-name>
- <param-value>black</param-value>
- </init-param>
- <init-param>
- <param-name>kaptcha.noise.impl</param-name>
- <param-value>com.google.code.kaptcha.impl.NoNoise </param-value>
- </init-param>
- <init-param>
- <param-name>kaptcha.noise.color</param-name>
- <param-value>black</param-value>
- </init-param>
- <init-param>
- <param-name>kaptcha.obscurificator.impl</param-name>
- <param-value>com.google.code.kaptcha.impl.ShadowGimpy</param-value>
- </init-param>
- <init-param>
- <param-name>kaptcha.background.impl</param-name>
- <param-value>com.google.code.kaptcha.impl.DefaultBackground</param-value>
- </init-param>
- <init-param>
- <param-name>kaptcha.background.clear.to</param-name>
- <param-value>white</param-value>
- </init-param>
- <init-param>
- <param-name>kaptcha.word.impl</param-name>
- <param-value>com.google.code.kaptcha.text.impl.DefaultWordRenderer</param-value>
- </init-param>
- <init-param>
- <!--设置session中 验证码的key值 -->
- <param-name>kaptcha.session.key</param-name>
- <param-value>KAPTCHA_SESSION_KEY</param-value>
- </init-param>
- <init-param>
- <!--设置session中 验证码的value值 -->
- <param-name>kaptcha.session.date</param-name>
- <param-value>KAPTCHA_SESSION_DATE</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>Kaptcha</servlet-name>
- <url-pattern>/Kaptcha.jpg</url-pattern>
- </servlet-mapping>
3.login.html
- <tr>
- <td height="34" align="right">验证码:</td>
- <td><input type="text" name="j_random" class="tclss" class="required tyno" minlength="4" /></td>
- <td><img id='kaptchaImage' src='Kaptcha.jpg' title="请点击刷新" alt="请点击刷新" style="cursor:pointer;"/>
- </td>
- </tr>