迁移春季安全2到3

时间:2015-09-03 11:19:53

标签: spring spring-security

分配给访问角色的用户不仅仅是空白页面。只有少数工作的网址有filter =" none"。 没有错误或没有例外。 登录后出现空白页面。 网址如下:/ indexPage           /测试/ indexPage

securityContext.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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:sec="http://www.springframework.org/schema/security"
    xmlns:c="http://www.myPro.com/schema/system-config"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.3.xsd
        http://www.myPro.com/schema/system-config http://www.myPro.com/schema/system-config/system-config-1.0.xsd">

    <!-- enable method-level security via annotation -->
    <sec:global-method-security secured-annotations="enabled" jsr250-annotations="disabled"/>

    <!-- secure the web layer -->

    <sec:http  auto-config="false" entry-point-ref="myAuthenticationEntryPoint" lowercase-comparisons="false"> 
        <sec:custom-filter position="FORM_LOGIN_FILTER" ref="customizedFormLoginFilter"/>
        <sec:custom-filter after="FORM_LOGIN_FILTER" ref="rememberMeProcessingFilter"/> 
        <sec:custom-filter before="FILTER_SECURITY_INTERCEPTOR" ref="singleSignOnFilter"/>
        <sec:custom-filter after="REMEMBER_ME_FILTER" ref="logoutFilter" />
        <sec:intercept-url pattern="/login.jsp" filters="none" />
        <sec:intercept-url pattern="/**" access="ROLE_USER" />
        <sec:intercept-url pattern="/contract/ServiceContractPDFView.jsp" filters="none" />
        <sec:intercept-url pattern="/admin/unsubscribe_sbpqm_newsletter.jsp" filters="none" />
        <sec:intercept-url pattern="/admin/subscription_form.jsp" filters="none" />
        <sec:intercept-url pattern="/admin/subscription_thankyou.jsp" filters="none" />
        <sec:intercept-url pattern="/admin/related_analysts.jsp" filters="none" />
        <sec:intercept-url pattern="/favicon.ico" filters="none" />
        <sec:intercept-url pattern="/styles/**" filters="none" />
        <sec:intercept-url pattern="/images/**" filters="none" />
        <sec:intercept-url pattern="/qlogin.jsp" filters="none" />
        <sec:intercept-url pattern="/qloginWait/**" filters="none" />   
        <sec:intercept-url pattern="/js/**" filters="none" />
        <sec:intercept-url pattern="/scripts/**" filters="none" />
        <sec:anonymous username="anonymousUser" granted-authority="ROLE_ANONYMOUS"/>
    </sec:http>

    <!--name of my authenticationManager is authenticationManager-->
    <sec:authentication-manager alias="authenticationManager">
    <sec:authentication-provider user-service-ref="myUserDetailsService" />
    </sec:authentication-manager>


    <bean id="customizedFormLoginFilter" class="com.myPro.test.security.CustomAuthenticationProcessingFilter" >
        <!--Authentication failed? take him to error page-->
        <!--Here it is the custom authenticationManager, login magic goes here -->
        <property name="authenticationManager" ref="myAuthenticationManager"/> 
        <property name="authenticationFailureHandler" ref="failureHandler"/>
        <property name="authenticationSuccessHandler" ref="successHandler"/>
        <property name="rememberMeServices" ref="rememberMeServices" /> 
        <property name="allowSessionCreation" value="true" />
    </bean>

    <bean id="myAuthenticationManager" class="com.myPro.test.security.CustomAuthenticationManager" />

    <bean id="loggerListener" class="org.springframework.security.access.event.LoggerListener"/>

    <!--My authentication entry point, can be replaced easily if we are doing custom commence of invalid auths.-->
    <bean id="myAuthenticationEntryPoint"
    class="com.myPro.test.security.CustomAuthenticationEntryPoint" >
    <property name="loginFormUrl" value="/login.jsp"/>
    </bean>

    <bean id="successHandler" class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
        <property name="defaultTargetUrl" value="/indexCustomer.jsp"/>
    </bean>

    <bean id="failureHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
        <property name="defaultFailureUrl" value="/login.jsp?login_error=1"/>
    </bean>

    <!-- Override RememberMeProcessingFilter to allow application of other business logic (update login count when user returns to the site --> 

    <bean id="rememberMeProcessingFilter" class="com.myPro.test.security.CustomRememberMeProcessingFilter"> 
        <property name="rememberMeServices" ref="rememberMeServices"/> 
        <property name="authenticationManager" ref="authenticationManager" /> 
    </bean> 

    <bean id="signleSignOnService" class="com.myPro.sage.sso.dynamo.SsoDbStorage">
    </bean>
    <bean id="singleSignOnFilter"
          class="com.myPro.test.spring.SingleSignOnFilter">
        <property name="signleSignOnService" ref="signleSignOnService"/>
        <!--<property name="authenticationProviderFacade" ref="authenticationProviderFacade"/>-->
        <property name="userService" ref="myProUserServiceImpl"/>
        <property name="ssoUserUrl">
            <value>/sso</value>
        </property>
        <!-- Code Review Starts -->
        <property name="ssoTargetUrl">
            <value>/search/ServiceContractSearch.do</value>
        </property>
        <!-- Code Review Ends -->
        <property name="ssoFailureUrl">
            <value>/login.jsp</value>
        </property>
        <property name="order" value="123456"/>
    </bean>

    <!-- Remember me Authentication Defines which remember me implementation to use - in this case using a database table to log 'remembered' tokens --> 

    <bean id="myUserDetailsService" class="com.myPro.test.security.CustomUserDetailsService" > </bean>
    <bean id="rememberMeServices" class="org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices"> 
        <property name="tokenRepository" ref="jdbcTokenRepository" />
        <property name="userDetailsService" ref="myUserDetailsService" />   
        <property name="key" value="springRocks" /> 
        <property name="alwaysRemember" value="false" /> 
    </bean>


    <!-- Uses a database table to maintain a set of persistent login data --> 

    <bean id="jdbcTokenRepository" class="org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl"> 
        <property name="createTableOnStartup" value="false" /> 
        <property name="dataSource" ref="dataSource" /> 
    </bean> 

    <bean id="rememberMeAuthenticationProvider" class="org.springframework.security.authentication.RememberMeAuthenticationProvider"> 
<!--        <sec:custom-authentication-provider/> -->
        <property name="key" value="springRocks"/> 
    </bean>

    <bean id="securityContextLogoutHandler" class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler" >
        <property name="invalidateHttpSession" value="true" />
    </bean> 

    <bean id="mySecurityContextHandler" class="com.myPro.test.security.CustomSecurityContextLogoutHandler"/>  

    <bean id="logoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter"> 
        <constructor-arg value="/" /> 
         <constructor-arg> 
            <list>
                <ref bean="mySecurityContextHandler" /> 
                <ref bean="rememberMeServices" /> 
                <ref bean="securityContextLogoutHandler" />
            </list>
        </constructor-arg>
    </bean>

    <bean id="authenticationLoggerListener" class="org.springframework.security.access.event.LoggerListener"/>

    <bean id="_sessionFixationProtectionFilter" class="org.springframework.security.web.authentication.session.SessionFixationProtectionStrategy"> 
        <property name="migrateSessionAttributes" value="true" />
    </bean>
</beans>

1 个答案:

答案 0 :(得分:0)

有两件事是错的。

首先url-intercept模式的顺序很重要。它们的定义顺序也是它们的参考顺序!拥有/**会使每个url-intercept元素无效。所以`/ **应该永远是最后的。

在较新的Spring Security版本中,您不应该再使用filters="none"了。您应该为这些元素创建单独的<sec:http />元素。请参阅Spring安全参考指南的this部分。

<sec:http pattern="/login.jsp" security="none" />
<sec:http pattern="/js/**" security="none" />
<sec:http pattern="/scripts/**" security="none" />
<!--- All other patterns here. -->