Ниже приведено то, что я настроил, чтобы заставить работать основной поток OAuth 2.0 (по сути, такой же, как в демонстрационной версии Tonr / Sparklr). Наши настройки безопасности сложны, поэтому я воспроизведу только соответствующие фрагменты ниже.
Сначала порядок цепочек фильтров:
BasicUserApprovalFilter, SecurityContextPersistenceFilter, LogoutFilter, UsernamePasswordAuthenticationFilter, BasicAuthenticationFilter, RequestCacheAwareFilter, SecurityContextHolderAwareRequestFilter, AnonymousAuthenticationFilter, SessionManagementFilter, ExceptionTranslationFilter, OAuth2ExceptionHandlerFilter, VerificationCodeFilter, OAuth2AuthorizationFilter, OAuth2ProtectedResourceFilter, FilterSecurityInterceptor
Обратите внимание, что AnonymousAuthenticationFilter
абсолютно необходим , даже если вы его не используете где-либо еще.
Теперь поддерживающие бины:
<bean id="oauth2ExceptionTranslationFilter" class="org.springframework.security.oauth2.provider.OAuth2ExceptionHandlerFilter"/>
<bean id="oauth2VerificationCodeFilter" class="org.springframework.security.oauth2.provider.verification.VerificationCodeFilter">
<property name="clientDetailsService" ref="clientDetailsService"/>
<property name="verificationServices" ref="verificationCodeServices"/>
<property name="userApprovalHandler" ref="oauth2UserApprovalFilter"/>
<property name="unapprovedAuthenticationHandler">
<bean class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
<!-- This is where you define your confirmation page -->
<property name="defaultFailureUrl" value="/oauth/confirm.action"/>
</bean>
</property>
</bean>
<bean id="oauth2AuthorizationFilter" class="org.springframework.security.oauth2.provider.OAuth2AuthorizationFilter">
<property name="authenticationManager" ref="authenticationManager"/>
<property name="authenticationSuccessHandler">
<bean class="org.springframework.security.oauth2.provider.OAuth2AuthorizationSuccessHandler">
<property name="tokenServices" ref="tokenServices"/>
</bean>
</property>
</bean>
<bean id="oauth2ProtectedResourceFilter" class="org.springframework.security.oauth2.provider.OAuth2ProtectedResourceFilter">
<property name="tokenServices" ref="tokenServices"/>
</bean>
<bean id="tokenServices" class="org.springframework.security.oauth2.provider.token.InMemoryOAuth2ProviderTokenServices">
<property name="supportRefreshToken" value="true"/>
</bean>
<bean id="clientDetailsService" class="org.springframework.security.oauth2.provider.InMemoryClientDetailsService">
<property name="clientDetailsStore">
<map>
<entry key="tonr">
<bean class="org.springframework.security.oauth2.provider.BaseClientDetails">
<property name="clientId" value="tonr"/>
<property name="authorizedGrantTypes">
<list>
<value>authorization_code</value>
<value>refresh_token</value>
</list>
</property>
</bean>
</entry>
</map>
</property>
</bean>
<bean id="verificationCodeServices" class="org.springframework.security.oauth2.provider.verification.InMemoryVerificationCodeServices"/>
<bean id="oauth2VerificationAuthenticationProvider" class="org.springframework.security.oauth2.provider.verification.VerificationCodeAuthenticationProvider">
<property name="verificationServices" ref="verificationCodeServices"/>
</bean>
<bean id="oauth2AccessGrantAuthenticationProvider" class="org.springframework.security.oauth2.provider.AccessGrantAuthenticationProvider">
<property name="clientDetailsService" ref="clientDetailsService"/>
</bean>
<bean id="oauth2RefreshAuthenticationProvider" class="org.springframework.security.oauth2.provider.refresh.RefreshAuthenticationProvider"/>
Обратите внимание, что службы (клиент, токен, проверочный код) просто поставляются в версиях памяти. Вам нужно будет создать свои собственные версии, чтобы быть постоянными.
Наконец, вам нужно связать провайдеров с вашим менеджером аутентификации:
<bean id="authenticationManager" class="org.springframework.security.authentication.ProviderManager">
<property name="providers">
<list>
<ref local="daoAuthenticationProvider"/>
<ref local="oauth2AccessGrantAuthenticationProvider"/>
<ref local="oauth2VerificationAuthenticationProvider"/>
<ref local="oauth2RefreshAuthenticationProvider"/>
<bean class="org.springframework.security.authentication.AnonymousAuthenticationProvider">
<property name="key" value="mykey"/>
</bean>
</list>
</property>
</bean>