spring security3拦截器问题

问题描述

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>

解决方案

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

时间: 2024-08-03 18:20:17

spring security3拦截器问题的相关文章

Spring MVC拦截器实现分析

Spring MVC拦截器实现分析 一.Servlet Filter与Spring interceptor的执行顺序 Filter有顺序吗?我们怎么控制filter的执行顺序.通过Tomcat的代码分析,servlet在Filter执行完成后才调用,如有多个filter怎么控制执行顺序,首先会想到在web.xml配置某个参数,例如order之类的,但查找一下一番,servlet并没有这个参数.试试filter Mapping的配置的先后顺序,果然有效,原来filter的执行顺序就考filter

spring事务-spring AOP 拦截器方式配置事务失效

问题描述 spring AOP 拦截器方式配置事务失效 Spring.xml配置如下 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:conte

spring aop 拦截器 ehcache 缓存加载

问题描述 spring aop 拦截器 ehcache 缓存加载 最近想利用spring aop 与 ehcache 设计一个缓存框架,我的想法是,配置ehcache.xml参数,然后在applicationContext.xml中配置脚本: <bean id="defaultCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> </be

spring mvc-SpringMvc拦截器配置怎么配置拦截一个网站

问题描述 SpringMvc拦截器配置怎么配置拦截一个网站 我想拦截一个网址,比如页面的地址是www.XXXX.com.配置文件怎么配? 不拦截全部,只拦截单个网址的那种 解决方案 在controller层中来配置拦截的URL @RequestMapping("configSettRule") public class SettRuleController extends BaseController{ @Resource private SettRuleManager settRul

Spring MVC拦截器+注解方式实现防止表单重复提交

原理:在新建页面中Session保存token随机码,当保存时验证,通过后删除,当再次点击保存时由于服务器端的Session中已经不存在了,所有无法验证通过. 1.新建注解: ? /**  * <p>  * 防止重复提交注解,用于方法上<br/>  * 在新建页面方法上,设置needSaveToken()为true,此时拦截器会在Session中保存一个token,  * 同时需要在新建的页面中添加  * <input type="hidden" name

自定义 spring mvc 拦截器(近期项目需求实现)

          需求背景:特定文件夹下任何文件不经过登录,全部拦截强制跳转登录,并客户端禁止下载服务器定制文件夹文件           经过1天多时间的各种尝试,自定义式的强大拦截器实现了,废话不说了,直接贴代码啦.    demo:       1>   根目录下 index.html 内容:               <a href="html/index.html">index</a><br/>              <

spring mvc-springmvc拦截器拦截可以处理非法字符吗?

问题描述 springmvc拦截器拦截可以处理非法字符吗? 我拦截请求,并对请求的参数做了过滤 如图: 然后到了controller里面 参数并没有改变 ,请问是我解决方法的问题吗?这种方法行的通吗?还是少了些东西?谢谢大神 解决方案 你这个拦截器有没有配置呢,调试下这个拦截器的代码有没有执行呢? 解决方案二: 你的拦截器进去了没啊?参考 http://examples.javacodegeeks.com/enterprise-java/spring/mvc/spring-mvc-handler

spring MVC拦截器01

spring MVC拦截 作用:身份校验,权限检查,防止非法访问. 场景:一个bbs系统,用户没有登录就无法发帖或者删除评论; 一个博客系统,没有登录就无法发表博文,无法增加分类,无法删除博文. spring MVC 拦截实现分为2步 (1)编写拦截器类,必须继承org.springframework.web.servlet.HandlerInterceptor 核心方法: public boolean preHandle(HttpServletRequest request, HttpServ

spring mvc拦截器无法拦截DWZ的请求,求指点。。。

问题描述 拦截器是这样配置的: <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/*" > </mvc:mapping>如果不配置或/*,将拦截所有的Controller <bean class="com.hangzhou.controller.interceptor.LoginInterceptor"></bean> </m