baidu_26208223 2015-02-27 09:04 采纳率: 0%
浏览 4529

spring security3拦截器问题

未登录系统的情况下,第一次访问页面会跳转到登录页面,第二次访问就能够访问
配置如下:

 <http entry-point-ref="loginAuthenticationEntryPoint" >
        <!-- UsernamePasswordAuthenticationFilter 
        default-target-url 指定了从登录页面登录后进行跳转的页面 always-use-default-target true表示登录成功后强制跳转 
        authentication-failure-url 表示验证失败后进入的页面 login-processing-url 设置验证登录验证地址,如果不设置,默认是j_spring_security_check 
        username-parameter,password-parameter 设置登录用户名和密码的请求name,默认:j_username,j_password 
        <form-login login-page="/login" 
            default-target-url="/index" authentication-failure-url="/login?error=1" login-processing-url="/logined" 
            username-parameter="loginUser" password-parameter="password" /> -->
        <logout  logout-success-url="/login" invalidate-session="true" delete-cookies="JSESSIONID"/>
        <!-- 尝试访问没有权限的页面时跳转的页面 -->
        <access-denied-handler error-page="/permission" />
        <!-- session-fixation-protection session固化保护
        none使得 session 固化攻击失效,不会配置 SessionManagementFilter (除非其它的 <session-management> 属性不是默认值)
        migrateSession当用户经过认证后分配一个新的 session ,它保证原 session 的所有属性移到新 session 中。我们将在后面的章节中讲解,通过基于 bean 的方式如何进行这样的配置。
        newSession当用户认证后,建立一个新的 session ,原(未认证时) session 的属性不会进行移到新 session 中来。-->
        <session-management invalid-session-url="/login"
            session-authentication-strategy-ref="compositeSessionAuthenticationStrategy" />
        <custom-filter position="FORM_LOGIN_FILTER" ref="formloginFilter" />
        <custom-filter position="CONCURRENT_SESSION_FILTER" ref="concurrencyFilter" />
        <!-- 增加一个filter,这点与 Acegi是不一样的,不能修改默认的filter了, 这个filter位于FILTER_SECURITY_INTERCEPTOR之前 -->
        <custom-filter before="FILTER_SECURITY_INTERCEPTOR" ref="myFilter" />
    </http>
    <!-- ConcurrentSessionFilter过滤器配置(主要设置账户session过期路径) 并发会话处理包所需要的过滤器。该过滤器具有双重作用。 
        首先,它会调用sessionregistry.refreshlastrequest为每个请求注册session总是有一个正确的最后更新时间。
         第二,它会为每个请求从sessionregistry中获取sessioninformation并且检查session是否已被标记为已过期。 
        如果它被标记为已过期,配置注销处理程序将被调用(如同logoutfilter),重定向到指定的expiredurl,session失效将导致httpsessiondestroyedevent被触发 
        通过在web.xml注册httpsessioneventpublisher。 -->
    <beans:bean id="concurrencyFilter"
        class="org.springframework.security.web.session.ConcurrentSessionFilter">
        <beans:property name="sessionRegistry" ref="sessionRegistry" />
        <beans:property name="expiredUrl" value="/login" />
    </beans:bean>
    <!-- 登录过滤器(相当于<form-login/>) -->
    <beans:bean id="formloginFilter"
        class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
        <beans:property name="usernameParameter" value="loginUser"></beans:property>
        <beans:property name="passwordParameter" value="password"></beans:property>
        <beans:property name="sessionAuthenticationStrategy"
            ref="compositeSessionAuthenticationStrategy" />
        <!--处理登录的action -->
        <beans:property name="filterProcessesUrl" value="/logined"></beans:property>
        <!--验证成功后的处理 -->
        <beans:property name="authenticationSuccessHandler"
            ref="loginLogAuthenticationSuccessHandler"></beans:property>
        <!--验证失败后的处理 -->
        <beans:property name="authenticationFailureHandler"
            ref="simpleUrlAuthenticationFailureHandler"></beans:property>
        <beans:property name="authenticationManager" ref="authenticationManager"></beans:property>
    </beans:bean>

    <!-- 混合session授权策略 -->
    <beans:bean id="compositeSessionAuthenticationStrategy"
        class="org.springframework.security.web.authentication.session.CompositeSessionAuthenticationStrategy">
        <beans:constructor-arg>
            <beans:list>
                <beans:bean
                    class="org.springframework.security.web.authentication.session.ConcurrentSessionControlAuthenticationStrategy">
                    <beans:constructor-arg ref="sessionRegistry" />
                    <beans:property name="maximumSessions" value="1" />
                    <beans:property name="exceptionIfMaximumExceeded"
                        value="true" />
                </beans:bean>
                <beans:bean
                    class="org.springframework.security.web.authentication.session.SessionFixationProtectionStrategy">
                </beans:bean>
                <beans:bean
                    class="org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy">
                    <beans:constructor-arg ref="sessionRegistry" />
                </beans:bean>
            </beans:list>
        </beans:constructor-arg>
    </beans:bean>

    <!--SessionRegistry的默认实现,它会在spring应用上下文中监听SessionDestroyedEvents事件 -->
    <beans:bean id="sessionRegistry"
        class="org.springframework.security.core.session.SessionRegistryImpl" />


    <!-- 登录点 -->
    <beans:bean id="loginAuthenticationEntryPoint"
        class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
        <beans:property name="loginFormUrl" value="/login"></beans:property>
    </beans:bean>
    <!-- 验证成功后的处理 -->
    <beans:bean id="loginLogAuthenticationSuccessHandler"
        class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
        <beans:property name="defaultTargetUrl" value="/index"></beans:property>
    </beans:bean>
    <!-- 验证失败后的处理 -->
    <beans:bean id="simpleUrlAuthenticationFailureHandler"
        class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
        <!--可以配置相应的跳转方式。属性forwardToDestination为true采用forward false为sendRedirect -->
        <beans:property name="defaultFailureUrl" value="/login?error=1"></beans:property>
    </beans:bean>
    <!-- 一个自定义的filter,必须包含 authenticationManager,accessDecisionManager,securityMetadataSource三个属性, 
        我们的所有控制将在这三个类中实现,解释详见具体配置 -->
    <beans:bean id="myFilter"
        class="com.sanchuan.erp.security.MyFilterSecurityInterceptor">
        <beans:property name="authenticationManager" ref="authenticationManager" />
        <beans:property name="accessDecisionManager" ref="myAccessDecisionManagerBean" />
        <beans:property name="securityMetadataSource" ref="mySecurityMetadataSource" />
    </beans:bean>
    <!-- 验证配置 , 认证管理器,实现用户认证的入口,主要实现UserDetailsService接口即可 -->
    <authentication-manager alias="authenticationManager">
        <authentication-provider user-service-ref="myUserDetailsService">
            <!-- <s:password-encoder hash="sha" /> -->
        </authentication-provider>
    </authentication-manager>
    <!-- 项目实现的用户查询服务,将用户信息查询出来 -->
    <beans:bean id="myUserDetailsService"
        class="com.sanchuan.erp.security.MyUserDetailService">
        <beans:property name="userService" ref="userServiceImpl"></beans:property>
        <beans:property name="roleService" ref="roleServiceImpl"></beans:property>
    </beans:bean>
    <!-- 访问决策器,决定某个用户具有的角色,是否有足够的权限去访问某个资源 -->
    <beans:bean id="myAccessDecisionManagerBean"
        class="com.sanchuan.erp.security.MyAccessDecisionManager">
    </beans:bean>
    <!-- 资源源数据定义,将所有的资源和权限对应关系建立起来,即定义某一资源可以被哪些角色访问 -->
    <beans:bean id="mySecurityMetadataSource"
        class="com.sanchuan.erp.security.MyInvocationSecurityMetadataSourceService">
    </beans:bean>

  • 写回答

1条回答

  • baidu_26208223 2015-02-28 00:46
    关注

    ......顶起......

    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误