加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Spring security oauth2客户端

发布时间:2020-12-15 04:59:06 所属栏目:Java 来源:网络整理
导读:我已经设置了具有 spring安全性的OAuth2服务器.我想编写客户端应用程序以使用具有spring安全性的此oauth服务器而不保护任何资源.意味着我只想用spring security 3.1从客户端运行oauth2.我编写了以下配置,但在重定向到oauth2服务器授权页面之前要求提供凭据.
我已经设置了具有 spring安全性的OAuth2服务器.我想编写客户端应用程序以使用具有spring安全性的此oauth服务器而不保护任何资源.意味着我只想用spring security 3.1从客户端运行oauth2.我编写了以下配置,但在重定向到oauth2服务器授权页面之前要求提供凭据.但是我想在从客户端询问任何凭据之前将用户重定向到oauth2服务器授权页面.我正在使用以下配置

<http auto-config='true' xmlns="http://www.springframework.org/schema/security">
    <intercept-url pattern="/product/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />
    <custom-filter ref="oauth2ClientFilter" after="EXCEPTION_TRANSLATION_FILTER" />
</http>

<authentication-manager xmlns="http://www.springframework.org/schema/security">
    <authentication-provider>
        <user-service>
            <user name="jimi" password="jimi" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>
</authentication-manager>

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


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


 <bean id="dService" class="com.abc.service.DServiceImpl">
    <property name="dURL" value="${dURL}"></property>
    <property name="dRestTemplate">
        <oauth:rest-template resource="fooClient" />
    </property>

 </bean>

所以我只想/产品网址应该访问oauth2服务器.其余的URL映射应该没有这个.
并且用户应该对客户端匿名(无需在客户端显示登录).

但是当我运行我的应用程序“http:// localhost / client-sample / product / 1”时,它会显示“http:// localhost / client-sample / spring_security_login”.但我希望用户应该重定向到oaut2服务器页面.

解决方法

Spring安全性可防止匿名用户获取访问令牌.但是,如果您仍然希望在应用程序中使用此功能,则必须扩展org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails类并覆盖isClientOnly()方法.

import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails;

public class ExtendedBaSEOAuth2ProtectedResourceDetails extends
    AuthorizationCodeResourceDetails {

public boolean isClientOnly() {
    return true;
}
}

默认情况下,此方法返回false.所以你必须覆盖这个方法才能返回true.
然后在root-context.xml文件中,你必须像这样定义oaut2资源.

<bean id="fooClient" class="com.abc.service.ExtendedBaSEOAuth2ProtectedResourceDetails">
  <property name="clientId" value="foo"></property>
  <property name="clientSecret" value="secret"></property>
  <property name="accessTokenUri" value="${accessTokenUri}"></property>
  <property name="userAuthorizationUri" value="${userAuthorizationUri}"></property>
  <property name="scope" value="#{{'read','write'}}">   </property>
</bean>

<bean id="dService" class="com.abc.service.DServiceImpl">
  <property name="dURL" value="${dURL}"></property>
  <property name="dRestTemplate">
      <oauth:rest-template resource="fooClient" />
  </property>
</bean>

在将用户重定向到oauth2提供程序授权页面之前,这不会在客户端请求授权.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读