用于客户端和资源服务器的Spring OAuth2 XML配置

时间:2018-01-14 19:39:44

标签: spring-security oauth-2.0 openid-connect spring-oauth2 oidc

任何人都可以帮助我使用XML中的基本配置来将我的spring应用程序用作OAuth2 / OIDC资源服务器以及cilent。

我有什么?

使用Spring Secuirity LDAP身份验证的Spring Web MVC应用程序。

我想要达到什么目标?

  1. 如果用户尝试访问我的应用程序中的任何资源(例如index.html),则应该询问他的凭据(可以弹出或可以重定向到登录页面)。
  2. 应用程序应与第三方授权服务器连接,并获取OAuth2访问令牌和刷新令牌。
  3. 收到访问令牌后,应用程序应创建会话并在第一步中提供所需的资源。
  4. 当用户点击退出或会话过期时,流程从第一步开始。
  5. 到目前为止我尝试了什么?

    我已经尝试过使用Spring启动和OIDC。但我正在寻找一些很好的参考,以通过 XML配置实现上述目标。请注意,我不能使用Spring Boot或任何java配置。

    关于如何开始这一切的任何想法或建议?

    感谢。

1 个答案:

答案 0 :(得分:1)

首先,我必须说你可以在Spring's oAuth Samples部分找到好的例子。

无论如何,我在玩了一段时间后创建了一个oAuth-sample-project (GitHub),所以这里是有趣的部分。考虑到你必须从文档中学到一点,并深入研究代码......但我认为这对于一个起点是好的。

客户端XML:

<sec:http authentication-manager-ref="authenticationManager">
    <sec:intercept-url pattern="/secure/**" access="ROLE_USER" />
    <sec:anonymous/>

    <!-- sec:form-login/-->

    <sec:form-login 
        login-page="/login/login.htm" 
        authentication-failure-url="/login/login.htm?login_error=1" />


    <sec:custom-filter ref="oauth2ClientFilter" after="EXCEPTION_TRANSLATION_FILTER" />
</sec:http>


<sec:authentication-manager alias="authenticationManager">
    <sec:authentication-provider user-service-ref="userDetailsService"/>
</sec:authentication-manager>

<sec:user-service id="userDetailsService">
    <sec:user name="admin"  password="admin"  authorities="ROLE_USER,ROLE_ADMIN" />
</sec:user-service>



<!--apply the oauth client context-->
<oauth:client   id="oauth2ClientFilter" />


<oauth:resource id="butkecResource"
                type="authorization_code"
                client-id="${oauth2.client.id}"
                client-secret="${oauth2.client.secret}"
                access-token-uri="${oauth2.client.accessTokenUri}"
                user-authorization-uri="${oauth2.client.userAuthorizationUri}"
                scope="read"/>

<!--define an oauth2 resource for facebook. according to the facebook docs, the 'client-id' is the App ID, and the 'client-secret' 
    is the App Secret -->
<oauth:resource id="facebook" 
    type="authorization_code" 
    client-id="233668646673605" 
    client-secret="33b17e044ee6a4fa383f46ec6e28ea1d"
    authentication-scheme="query" 
    access-token-uri="https://graph.facebook.com/oauth/access_token" 
    user-authorization-uri="https://www.facebook.com/dialog/oauth"
    token-name="oauth_token" 
    client-authentication-scheme="form" />

完整代码段为here

资源服务器XML:

<security:http pattern="/index.html" security="none"/>
<security:http pattern="/browse" security="none"/>
<!-- security:http pattern="/welcome" security="none"/-->
<security:http pattern="/js/**" security="none"/>

<security:http  entry-point-ref="oauthAuthenticationEntryPoint"     
                access-decision-manager-ref="accessDecisionManager">
    <security:intercept-url pattern="/**" access="IS_AUTHENTICATED_FULLY" />
    <security:custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" />
    <security:access-denied-handler ref="oauthAccessDeniedHandler" />
    <security:anonymous />
</security:http>
...
...
<oauth:resource-server id="resourceServerFilter" 
                    token-services-ref="tokenServices" />


<bean id="tokenServices" class="org.springframework.security.oauth2.provider.token.DefaultTokenServices" >
    <property name="tokenStore" ref="tokenStore" />
</bean>


<bean id="tokenStore" class="com.ohadr.oauth.resource_server.token.MyTokenStore" />


<bean id="oauthAuthenticationEntryPoint" class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">
    <property name="realmName" value="butkec" />
</bean>

文件可以找到here

我认为这里不是解释每个位和字节的好地方,但是再次 - 在Spring docs中你可以找到很好的解释(我设法从那里学到所有......)