spring security中自定义authenticationProcessingFilter

本文介绍了一个基于Spring Security的自定义登录过滤器实现,该实现包括了对验证码的校验逻辑。通过配置文件定义了不同URL的访问权限,并实现了自定义的身份认证过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[url]http://hszdz.iteye.com/blog/337652[/url]
<http entry-point-ref="authenticationProcessingFilterEntryPoint">
<intercept-url pattern="/admin/**" access="ROLE_ADMIN"/>
<intercept-url pattern="/user/**" access="ROLE_USER,ROLE_ADMIN"/>
</http>
<authentication-provider user-service-ref="securityManager"/>
<beans:bean id="authenticationProcessingFilter" class="haotian.haocms.security.CustomAuthenticationProcessingFilter">
<custom-filter position="AUTHENTICATION_PROCESSING_FILTER"/>
<beans:property name="authenticationManager" ref="authenticationManager"/>
<beans:property name="defaultTargetUrl" value="/user/userList.action"/>
</beans:bean>
<authentication-manager alias="authenticationManager"/>
<beans:bean id="authenticationProcessingFilterEntryPoint" class="org.springframework.security.ui.webapp.AuthenticationProcessingFilterEntryPoint">
<beans:property name="loginFormUrl" value="/loginPage.action"/>
<beans:property name="forceHttps" value="false"/>
</beans:bean>

public Authentication attemptAuthentication(   
HttpServletRequest httpServletRequest)
throws AuthenticationException {
String check_code = httpServletRequest.getParameter("j_checkcode");
String sys_code = (String) httpServletRequest.getSession()
.getAttribute(checkCodeName);

if (sys_code == null) {
throw new AuthCodeValidationException("验证码不正确");
} else if (!sys_code.equals(check_code)) {
String username = obtainUsername(httpServletRequest);
httpServletRequest.getSession().setAttribute(
SPRING_SECURITY_LAST_USERNAME_KEY, username);
// 用户输入的值与看到的不一致,抛出异常
throw new AuthCodeValidationException("验证码输入不正确");
}
return super.attemptAuthentication(httpServletRequest);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值