spring-security_3_自定义过滤器

配置基本环境

配置基本环境: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 Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,986评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,970评论 6 342
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,482评论 25 708
  • 季安与小谈刚回到公司,整理了自己的文件,就向万主管详诉自己的方案。 “路氏想要收购彩虹,最重要的,不仅要...
    西宫太后阅读 659评论 12 4
  • 又是一个双十一,传说中属于光棍的节日。细细想来,好奇怪,为什么给单身的人赋予这样一个带有某些歧义的“节日”,如果双...
    朵0云阅读 215评论 0 1