Spring security 基于OAuth2访问令牌自动登录到Hybris web应用程序

Spring security 基于OAuth2访问令牌自动登录到Hybris web应用程序,spring-security,oauth-2.0,hybris,Spring Security,Oauth 2.0,Hybris,背景 我们有一个使用SAP Hybris commerce运行的网站,用户可以登录(基本的Spring安全)并浏览该网站。我们还有一个本地移动应用程序,它将再次使用Oauth2对Hybris系统中的用户进行身份验证,并且无状态工作 问题陈述 用户登录到移动本机应用程序,需要在标准web浏览器中在web上执行一些经过身份验证的操作(即使用本机应用程序尚不支持的某些功能)。本机应用程序应打开浏览器,并确保用户使用其在本机应用程序中拥有的相同帐户登录到浏览器 当前网站安全配置.xml <http

背景
我们有一个使用SAP Hybris commerce运行的网站,用户可以登录(基本的Spring安全)并浏览该网站。我们还有一个本地移动应用程序,它将再次使用Oauth2对Hybris系统中的用户进行身份验证,并且无状态工作

问题陈述
用户登录到移动本机应用程序,需要在标准web浏览器中在web上执行一些经过身份验证的操作(即使用本机应用程序尚不支持的某些功能)。本机应用程序应打开浏览器,并确保用户使用其在本机应用程序中拥有的相同帐户登录到浏览器

当前网站安全配置.xml

<http access-decision-manager-ref="accessDecisionManager" use-expressions="false">
    <session-management session-authentication-strategy-ref="fixation"/>
    <intercept-url pattern="/login.jsp" access="PERMIT_ALL"/>
    <intercept-url pattern="/**" access="HYBRIS_NOT_INITIALIZED,ROLE_CUSTOMERGROUP"/>
    <http-basic />
    <form-login 
        always-use-default-target="false" 
        login-page="/login.jsp" 
        username-parameter="j_username"
        password-parameter="j_password"
        login-processing-url="/j_spring_security_check"
        authentication-failure-url="/login.jsp?login_error=1"
    />
    <remember-me services-ref="rememberMeServices" key="adminweb"/>
    <logout logout-url="/j_spring_security_logout" logout-success-url="/login.jsp"/>
    <csrf />
    <headers>
        <frame-options disabled="true"/>
    </headers>
</http>

Oauth2配置

<!-- Generating token -->
    <sec:http pattern="/oauth/token" create-session="stateless" entry-point-ref="oauthAuthenticationEntryPoint"
              authentication-manager-ref="clientAuthenticationManager" use-expressions="false">
        <sec:csrf disabled="true"/>
        <sec:intercept-url pattern="/oauth/token" access="IS_AUTHENTICATED_FULLY" requires-channel="${webservicescommons.required.channel:https}"/>
        <sec:anonymous enabled="false" />
        <sec:port-mappings>
            <sec:port-mapping http="#{configurationService.configuration.getInt('tomcat.http.port',9091)}"
                              https="#{configurationService.configuration.getInt('tomcat.ssl.port',9092)}" />
            <sec:port-mapping http="#{configurationService.configuration.getInt('embeddedserver.http.port',9091)}"
                              https="#{configurationService.configuration.getInt('embeddedserver.ssl.port',9092)}" />
        </sec:port-mappings>
        <sec:http-basic entry-point-ref="oauthAuthenticationEntryPoint" />
        <sec:custom-filter ref="clientCredentialsTokenEndpointFilter" after="BASIC_AUTH_FILTER" />
        <sec:access-denied-handler ref="oauthAccessDeniedHandler" />
        <sec:headers>
            <sec:frame-options disabled="true"/>
        </sec:headers>
    </sec:http>

<!-- Accessing APIs using V2 -->
    <http pattern="/v2/**" entry-point-ref="oauthAuthenticationEntryPointV2"
              access-decision-manager-ref="webSecurityAccessDecisionManager"
              xmlns="http://www.springframework.org/schema/security" create-session="stateless">

            <anonymous username="anonymous" granted-authority="ROLE_ANONYMOUS"/>
            <!--<session-management session-authentication-strategy-ref="fixation"/>-->

            <intercept-url pattern="/**" requires-channel="https"/>

            <port-mappings>
                <port-mapping http="#{configurationService.configuration.getProperty('tomcat.http.port')}"
                              https="#{configurationService.configuration.getProperty('tomcat.ssl.port')}"/>
                <port-mapping http="#{configurationService.configuration.getProperty('embeddedserver.http.port')}" 
                              https="#{configurationService.configuration.getProperty('embeddedserver.ssl.port')}" />
            </port-mappings>

            <custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER"/>
            <access-denied-handler ref="oauthAccessDeniedHandlerV2"/>

            <headers >
                <content-type-options />
                <hsts include-subdomains="true" max-age-seconds="16070400" />
                <xss-protection />
                <security:frame-options disabled="true"/>
            </headers>
            <security:csrf disabled="true"/>
        </http>

问题

  • 如何修改web安全配置,以便每当存在
    访问令牌
    请求参数时,它都会基于访问令牌对用户进行身份验证,并为同一用户创建会话
  • 我正在考虑使用公共控制器映射,比如
    /login/autologin?token=&redirect=
    ,它基本上对用户进行身份验证并调用autologin策略。但是,我不知道,我需要调用哪些类/策略来使用访问令牌对用户进行身份验证,以及如何调用这些类/策略

  • 还有其他建议吗?

    我已经处理了类似这样的需求

  • 编写一个控制器,捕获access_令牌并调用TokenAuthenticationValidator

  • 通过引用OAuth2AuthenticationProcessingFilter编写TokenAuthenticationValidator,您需要在其中提取令牌基身份验证对象并将其传递给OAuth2AuthenticationManager进行身份验证

  • 如果令牌经过身份验证,则调用自动登录用户的自动登录策略。您需要编写自定义自动登录策略,该策略无需密码检查即可自动登录用户。像

    oauthUserAutoLoginStrategy.login(authResult.getPrincipal().toString(),请求,响应)