cha*_*ean 7 spring-security oauth-2.0 spring-security-oauth2
我尝试基于官方教程Sparklr2/Tonr2实现我自己的示例.一切看起来不错,但是当我从删除web.xml我的Tonr2实施,春季安全过滤器我有例外:
没有为当前请求建立重定向URI
我无法理解我应该使用哪个URL.这是我的代码,用于客户端实现:
<!--apply the oauth client context -->
<oauth:client id="oauth2ClientFilter" />
<!--define an oauth 2 resource for sparklr -->
<oauth:resource id="provider" type="authorization_code" client-id="client" client-secret="secret"
access-token-uri="http://localhost:8080/provider/oauth/token" user-authorization-uri="http://localhost:8080/provider/oauth/authorize" scope="read,write" />
<beans:bean id="clientController" class="com.aouth.client.ClientController">
<beans:property name="trustedClientRestTemplate">
<oauth:rest-template resource="provider" />
</beans:property>
</beans:bean>
Run Code Online (Sandbox Code Playgroud)
对于提供者:
<http 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 />
</http>
<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>
<!-- The OAuth2 protected resources are separated out into their own block so we can deal with authorization and error handling
separately. This isn't mandatory, but it makes it easier to control the behaviour. -->
<http pattern="/secured" create-session="never" access-decision-manager-ref="accessDecisionManager" xmlns="http://www.springframework.org/schema/security">
<anonymous enabled="false" />
<intercept-url pattern="/secured" access="ROLE_USER,SCOPE_READ" />
<custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" />
<http-basic />
</http>
<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>
<oauth:resource-server id="resourceServerFilter" resource-id="resource" token-services-ref="tokenServices" />
<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"/>
</bean>
<bean id="tokenStore" class="org.springframework.security.oauth2.provider.token.InMemoryTokenStore" />
<http auto-config="true" xmlns="http://www.springframework.org/schema/security">
<intercept-url pattern="/test" access="ROLE_USER" />
<intercept-url pattern="/" access="IS_AUTHENTICATED_ANONYMOUSLY" />
</http>
<authentication-manager alias="authenticationManager" xmlns="http://www.springframework.org/schema/security">
<authentication-provider>
<user-service>
<user name="pr" password="pr" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
<oauth:authorization-server client-details-service-ref="clientDetails" token-services-ref="tokenServices" >
<oauth:authorization-code />
<oauth:implicit />
<oauth:refresh-token />
<oauth:client-credentials />
<oauth:password />
</oauth:authorization-server>
<oauth:client-details-service id="clientDetails">
<oauth:client client-id="client" resource-ids="resource" authorized-grant-types="authorization_code, implicit"
authorities="ROLE_CLIENT" scope="read,write" secret="secret" />
</oauth:client-details-service>
Run Code Online (Sandbox Code Playgroud)
我只是希望我的客户在没有Spring安全的情况下工作.当我需要我的受保护资源时,我只想在提供者端登录.
Oha*_*adR 10
你在这里粘贴的第二个XML是oauth-provider和protected-resource的spring的XML ,在你的情况下运行在同一个webapp中.(当然,如果你愿意,你可以将它们分开).
客户端(第一个粘贴的XML)是一个不同的故事.如果我理解正确,你希望你的客户在没有Spring的帮助下运行(成为常规的webapp,而不是spring-security-oauth-client webapp).
您必须了解oAuth的工作原理:客户端尝试访问受保护的资源; 如果它没有访问令牌,则会将其重定向到oAuth-provider(显示登录页面并提供令牌).根据标准,访问令牌的请求必须包含"redirect-uri"参数,因此在成功登录后,oAuth提供者知道将客户端重定向到的位置.oAuth客户端为您完成,如果您从web.xml中删除"oauth客户端",您现在必须自己实现.
感谢您的回答.但我仍然不明白Spring安全性如何影响我的oAuth客户端.我可以使用没有弹簧安全的客户端spring-oauth(spring-mvc)吗?
在XML中编写此行时:
< oauth:client id="oauth2ClientFilter" />
Run Code Online (Sandbox Code Playgroud)
这意味着你使用spring-security-oauth,这是一个专门用于oauth的软件包,它建立在spring-security之上.如果你深入研究,它会在链中放置一个特殊的过滤器(OAuth2ClientContextFilter),用于处理与客户端相关的oAuth内容.其中一个是发送带有所有参数的请求("redirect-uri"就是其中之一).
如果你决定不使用spring-security-oauth,那么你必须自己实现这个逻辑......
希望有所帮助!
| 归档时间: |
|
| 查看次数: |
28567 次 |
| 最近记录: |