从Spring-Security 4.0.4升级到4.1.1后的身份验证问题

时间:2018-10-30 20:07:33

标签: java spring-security spring-security-oauth2

如果我将spring-security从4.0.4升级到4.1.1,则无法登录我的应用程序。我似乎完全忽略了我的CustomUserDetailsS​​ervice。 如果我从Web.xml注释掉我的REST服务(/WEB-INF/rest-dispatcher-servlet.xml和/WEB-INF/rest-dispatcher-servlet-security.xml)或降级到4.0.x,则可以登录精细。

我的应用程序以及基于wicket和spring的REST服务。

这是我的配置文件。

pom.xml

    <org.springframework.version>4.3.20.RELEASE</org.springframework.version>        
<org.springframework.sec.version>4.0.4.RELEASE</org.springframework.sec.version>
<org.springframework.sec.oauth2.version>2.3.4.RELEASE</org.springframework.sec.oauth2.version>  

...

applicatonContext-security.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"       
       xmlns:sec="http://www.springframework.org/schema/security"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/security
       http://www.springframework.org/schema/security/spring-security.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.3.xsd">


    <sec:http create-session="never" auto-config="true" use-expressions="false">       
        <sec:intercept-url pattern="/**"  />        
        <sec:form-login login-page="/login"          
                        login-processing-url="/" />                                
        <sec:csrf disabled="true"/>
        <sec:headers>
            <sec:frame-options policy="SAMEORIGIN"></sec:frame-options>                    
        </sec:headers>
    </sec:http>

    <bean class="org.springframework.security.authentication.encoding.Md5PasswordEncoder" id="passwordEncoder"/>        

    <authentication-manager alias="authenticationManager"  xmlns="http://www.springframework.org/schema/security">
        <authentication-provider  user-service-ref="CustomUserDetailsService">
            <password-encoder ref="passwordEncoder"/>
        </authentication-provider>
    </authentication-manager>

    <bean id="CustomUserDetailsService" class="com.xxx.zzz.services.CustomUserDetailsService">
        <property name="url" value="${jdbc.url}"/>
    </bean>
    <sec:global-method-security pre-post-annotations="enabled" secured-annotations="enabled" jsr250-annotations="enabled"/>
</beans>

rest-dispatcher-servlet-security.xml

        <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
           xmlns:sec="http://www.springframework.org/schema/security"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security.xsd
           http://www.springframework.org/schema/security/oauth2
           http://www.springframework.org/schema/security/spring-security-oauth2.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-4.3.xsd">



    <http use-expressions="false" pattern="/oauth/token" create-session="stateless" authentication-manager-ref="clientAuthenticationManager"  xmlns="http://www.springframework.org/schema/security">

        <intercept-url pattern="/oauth/token" access="IS_AUTHENTICATED_FULLY"/>
        <anonymous enabled="false"/>
        <http-basic entry-point-ref="clientAuthenticationEntryPoint"/>
        <custom-filter ref="clientCredentialsTokenEndpointFilter" after="BASIC_AUTH_FILTER"/>
        <access-denied-handler ref="oauthAccessDeniedHandler"/>
        <sec:csrf disabled="true"/>
    </http>

    <http use-expressions="false" pattern="/ws/api/**"
          create-session="never"
          entry-point-ref="oauthAuthenticationEntryPoint"
          access-decision-manager-ref="accessDecisionManager"
          xmlns="http://www.springframework.org/schema/security">
        <anonymous enabled="false"/>
        <intercept-url pattern="/ws/api/**"
                       access="ROLE_USER"/>
        <custom-filter ref="resourceServerFilter"
                       before="PRE_AUTH_FILTER"/>
        <access-denied-handler
                ref="oauthAccessDeniedHandler"/>
        <sec:csrf disabled="true"/>
    </http>

    <bean id="oauthAuthenticationEntryPoint"
          class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">
        <property name="realmName" value="xxx"/>
    </bean>
    <bean id="clientAuthenticationEntryPoint"
          class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">
        <property name="realmName" value="xxx/client"/>
        <property name="typeName" value="Basic"/>
    </bean>
    <bean id="oauthAccessDeniedHandler"
          class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler"/>

    <bean id="clientCredentialsTokenEndpointFilter"
          class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter">
        <property name="authenticationManager" ref="clientAuthenticationManager"/>

    </bean>
    <bean id="accessDecisionManager" class="org.springframework.security.access.vote.UnanimousBased"
          xmlns="http://www.springframework.org/schema/beans">
        <constructor-arg>
            <list>
                <bean class="org.springframework.security.oauth2.provider.vote.ScopeVoter"/>
                <bean class="org.springframework.security.access.vote.RoleVoter"/>
                <bean class="org.springframework.security.access.vote.AuthenticatedVoter"/>
            </list>
        </constructor-arg>
    </bean>
    <authentication-manager id="clientAuthenticationManager" xmlns="http://www.springframework.org/schema/security">
        <authentication-provider user-service-ref="clientDetailsUserService"/>      
    </authentication-manager>
    <bean id="clientDetailsUserService"
          class="org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService">
        <constructor-arg ref="clientDetails"/>
    </bean>
    <bean id="tokenStore" class="org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore"/>
    <bean id="tokenServices" class="org.springframework.security.oauth2.provider.token.DefaultTokenServices">
        <property name="tokenStore" ref="tokenStore"/>
        <property name="supportRefreshToken" value="true"/>
        <property name="clientDetailsService" ref="clientDetails"/>
        <property name="accessTokenValiditySeconds" value="10"/>
    </bean>
    <bean id="userApprovalHandler"
          class="org.springframework.security.oauth2.provider.approval.TokenStoreUserApprovalHandler">
        <property name="tokenStore" ref="tokenStore"/>    
        <property name="requestFactory" ref="oAuth2RequestFactory"/>    

    </bean>
    <oauth:authorization-server client-details-service-ref="clientDetails" token-services-ref="tokenServices"
                                user-approval-handler-ref="userApprovalHandler" >

        <oauth:authorization-code/>
        <oauth:implicit/>
        <oauth:refresh-token/>
        <oauth:client-credentials/>
        <oauth:password/>

    </oauth:authorization-server>
    <oauth:resource-server id="resourceServerFilter"
                           resource-id="xxx"                      
                           token-services-ref="tokenServices"/>
    <oauth:client-details-service id="clientDetails">
        <oauth:client client-id="client"
                      authorized-grant-types="password,authorization_code,refresh_token,implicit,redirect"
                      authorities="ROLE_USER, ROLE_TRUSTED_USER"
                      redirect-uri="/"
                      scope="read,write,trust"
                      access-token-validity="300"
                      refresh-token-validity="300"/>
    </oauth:client-details-service>

    <bean class="org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory" id="oAuth2RequestFactory">   
        <constructor-arg ref="clientDetails" />     
    </bean>
    </beans>

web.xml

    <?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         metadata-complete="true" 
>
    <display-name>xxx</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/applicationContext.xml  
            /WEB-INF/rest-dispatcher-servlet.xml
            /WEB-INF/rest-dispatcher-servlet-security.xml
            /WEB-INF/applicationContext-security.xml                                                             
        </param-value>
    </context-param>


    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <filter>
        <filter-name>wicket</filter-name>
        <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
        <init-param>
            <param-name>ignorePaths</param-name>
            <param-value>/ws</param-value>
        </init-param>
        <init-param>
            <param-name>applicationClassName</param-name>
            <param-value>com.xxx.zzz.main.Application</param-value>
        </init-param>
        <init-param>
            <param-name>configuration</param-name>
            <param-value>development</param-value>
        </init-param>
    </filter>
    <filter>
        <filter-name>SessionFilter</filter-name>
        <filter-class>org.apache.wicket.protocol.http.servlet.WicketSessionFilter</filter-class>        
        <init-param>            
            <param-name>filterName</param-name>
            <param-value>myfilter</param-value>            
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>wicket</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>ERROR</dispatcher>        
    </filter-mapping>
    <servlet>
        <servlet-name>ws</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>ws</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>rest-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>rest-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>
    <absolute-ordering></absolute-ordering>

</web-app>

1 个答案:

答案 0 :(得分:0)

最后我找到了罪魁祸首。在applicationContext-security.xml中,我的身份验证管理器使用别名=“ authenticationManager”而不是id =“ authenticationManager”。 当图片中出现rest-dispatcher-servlet-security.xml时,它取代了applicationContext-security.xml中的定义,但它已加载得较早。 因此,对我来说尚不清楚,但我对结果感到非常满意:)。

rest-dispatcher-servlet-security.xml

<authentication-manager id="clientAuthenticationManager" xmlns="http://www.springframework.org/schema/security">
<authentication-provider user-service-ref="clientDetailsUserService"/>        

applicationContext-security

<authentication-manager id="authenticationManager"  xmlns="http://www.springframework.org/schema/security">
    <authentication-provider  user-service-ref="CustomUserDetailsService">
        <password-encoder ref="passwordEncoder"/>
    </authentication-provider>
</authentication-manager>
相关问题