美文网首页
spring-security_3_自定义过滤器

spring-security_3_自定义过滤器

作者: 凌云v | 来源:发表于2017-05-08 08:44 被阅读0次

    配置基本环境

    配置基本环境:spring-security基本环境配置
    项目目录结构如下图所示:

    项目目录结构

    实现UsernamePasswordAuthenticationFilter子类

    public class MUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
    
        @Override
        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
                throws IOException, ServletException {
    
            for (Object key : req.getParameterMap().keySet()) {
                System.out.println(key + ", " + req.getParameter(key.toString()));
            }
    
            super.doFilter(req, res, chain);
        }
    }
    

    修改security.xml文件

    <http pattern="/login.jsp" security="none" />
    
    
        <http use-expressions="true" entry-point-ref="loginUrlAuthenticationEntryPoint">
    
            <intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
    
            <csrf disabled="true" />
    
            <custom-filter ref="captchaAuthenticaionFilter"
                before="FORM_LOGIN_FILTER" />
        </http>
    
        <authentication-manager alias="authenticationManager">
            <authentication-provider>
                <user-service>
                    <user name="user" password="password" authorities="ROLE_USER" />
                    <user name="admin" password="admin" authorities="ROLE_USER" />
                </user-service>
            </authentication-provider>
        </authentication-manager>
    
    
        <b:bean id="loginUrlAuthenticationEntryPoint"
            class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
            <b:constructor-arg index="0" value="/login.jsp" />
        </b:bean>
    
        <b:bean id="captchaAuthenticaionFilter" class="com.sfq.MUsernamePasswordAuthenticationFilter">
            <b:property name="authenticationManager" ref="authenticationManager" />
            <b:property name="authenticationFailureHandler" ref="simpleUrlAuthenticationFailureHandler" />
            <b:property name="authenticationSuccessHandler" ref="loginLogAuthenticationSuccessHandler" />
            <b:property name="filterProcessesUrl" value="/j_spring_security_check" />
        </b:bean>
        <b:bean id="loginLogAuthenticationSuccessHandler"
            class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
            <b:property name="defaultTargetUrl" value="/index.jsp" />
        </b:bean>
        <b:bean id="simpleUrlAuthenticationFailureHandler"
            class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
            <b:property name="defaultFailureUrl" value="/login.jsp" />
        </b:bean>
    

    发布项目

    源码

    点击下载源码

    相关文章

      网友评论

          本文标题:spring-security_3_自定义过滤器

          本文链接:https://www.haomeiwen.com/subject/rksdtxtx.html