Spring Security 3.0 登录表单自定义验证

    原文地址:http://www.xx566.com/detail/173.html

    之前的两篇关于Spring Security多登陆入口的实现与配置:Spring Security 2.0  多登录入口的实现与配置Spring Security 3.0 多登录入口的实现与配置 中,我们简单的介绍了通过Spring Security 2和 3 分别配置的登录验证,不过使用的都是spring security内部提供的过滤器,很多情况下,我们的登录表单提交不仅仅是用户名和密码等信息,可能还需要额外的处理一些表单参数,这时我们可以通过自 定义表单验证来处理表单参数信息,本篇,我们就来简单的实现一个表单的自定义验证。

    首先, 我们看一下Spring Security3中使用的登录验证的过滤配置,如下:

<!-- 前台用户登陆 -->
<bean id="frontLoginFilter"
      class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
    <property name="authenticationManager" ref="authenticationManager"/>
    <property name="filterProcessesUrl" value="/j_spring_security_check"/>
    <property name="authenticationFailureHandler" ref="frontFailureHandler"/>
    <property name="authenticationSuccessHandler" ref="frontSuccessHandler"/>
</bean>
 
<!-- 后台用户登陆 -->
<bean id="adminLoginFilter"
      class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
    <property name="authenticationManager" ref="authenticationManager"/>
    <property name="filterProcessesUrl" value="/admin/j_spring_security_check"/>
    <property name="authenticationFailureHandler" ref="adminFailureHandler"/>
    <property name="authenticationSuccessHandler" ref="adminSuccessHandler"/>
</bean>

    这里使用到了org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter,我们需要做的就是自定义一个Fliter,继承UsernamePasswordAuthenticationFilter,以实现对表单参数的控制与获取处理,如下:

package security;
 
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
 
public class MyLoginAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
 
    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
            throws AuthenticationException {
 
        String url = request.getRequestURI();
 
        // 获取用户名、密码数据
        String username = obtainUsername(request);
 
        //根据用户名获取真实用户信息
        SecurityUser user = new SecurityUser();
//      SecurityUser user = this.securityUserSpecMapper.selectByUsername(username);
 
        // 判断用户相关信息
        if (user == null) {
            throw new UsernameNotFoundException("用户" + username + "不存在");
        }
 
        //假设需要进行用户权限的判断
        Collection<GrantedAuthority> authorities = user.getAuthorities();
        if (url.contains("admin")) {
            // 如果通过后台链接,只有admin角色能够登录
            if (!authorities.contains("ROLE_ADMIN")) {
                throw new BadCredentialsException("对不起,您没有权限通过此页面登录!");
            }
        } else {
            // 如果通过前台链接,只有front角色能够登录
            if (!authorities.contains("ROLE_FRONT")) {
                throw new BadCredentialsException("对不起,您没有权限通过此页面登录!");
            }
        }
        return super.attemptAuthentication(request, response);
    }
 
}

    

    最后,我们只需要简单的对Spring Security配置文件中的class进行修改即可。

    相关代码地址:http://git.oschina.net/realfighter/xx566-diary/tree/master/src/security

你可能感兴趣的:(spring,登录,自定义,验证,security3)