Spring security oauth 2简单示例

2023-11-25

我尝试根据官方教程实现我自己的示例Sparklr2/Tonr2。一切看起来都不错,但是当我从web.xml in my 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>

对于提供商来说:

<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>

我只是希望我的客户在没有 Spring Security 的情况下工作。当我需要受保护的资源时,我只想在提供者端登录。


您粘贴到此处的第二个 XML 是 spring 的 XMLoauth 提供者受保护资源,在您的情况下,它在同一个网络应用程序中运行。 (当然,如果您愿意,您可以将它们分开)。

客户端(第一个粘贴的 XML)是一个不同的故事。如果我理解正确的话,您希望您的客户端在没有 Spring 帮助的情况下运行(成为常规 web 应用程序,而不是 spring-security-oauth-client web 应用程序)。

您必须了解 oAuth 的工作原理:客户端尝试访问受保护的资源;如果它没有访问令牌,则会被重定向到 oAuth 提供程序(显示登录页面并提供令牌)。按标准,访问令牌的请求必须包含“redirect-uri”参数,因此成功登录后,oAuth-provider 知道将客户端重定向到哪里。 oAuth 客户端会为您完成此操作,如果您从 web.xml 中删除“oauth 客户端”,您现在必须自己实现此操作。

感谢您的回答。但我还是不明白春天是怎样的 安全性影响我的 oAuth 客户端。我可以用于客户端吗 spring-oauth (spring-mvc) 没有 spring-security?

当您在 XML 中写入以下行时:

< oauth:client id="oauth2ClientFilter" />

这意味着您使用 spring-security-oauth,这是一个专门用于 oauth 的包,基于 spring-security 构建。如果您深入研究,它会在处理与客户端相关的 oAuth 内容的链中放置一个特殊的过滤器 (OAuth2ClientContextFilter)。其中之一是发送带有所有参数的请求(“redirect-uri”就是其中之一)。

如果您决定不使用 spring-security-oauth,那么 - 您将必须自己实现此逻辑......

希望有帮助!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring security oauth 2简单示例 的相关文章

随机推荐

  • 从时间序列数据中获取间隔

    我有一个相当奇怪的问题 我目前正在处理时间序列数据 并且我的数据集中有几个峰值 该数据是使用中子密度测井机收集的 它描述了传感器在一段时间内连续记录的事件 数据中的峰值对应于该机器下入钻孔时的一些有趣的间隔 所以说 巅峰很重要 然而 重要的
  • 最新版本上的 FirebaseInstallationsException

    每次我将应用程序的 Firebase 依赖项更新为比 2020 年 2 月 27 日更新更新的版本时 我在运行应用程序时就会开始看到此异常 04 29 18 21 58 170 3314 3541 E Firebase Installati
  • 如何使用 Android Studio 将项目同步到 GitHub?

    我正在尝试将 Android Studio 文件夹中的项目同步到 GitHub 但除了在选项菜单中添加凭据之外 我不完全确定该怎么做 有人可以给我一个快速指南吗 在Android Studio中打开需要推送的项目 Click VCS gt
  • Opencv Python 使用 Numpy 数组裁剪图像

    我正在使用 OpenCV 3 1 0 dev 和 python 2 7 我正在尝试裁剪掉我缝合的图像的黑色外部 困难在于图像中还有其他黑色像素 因此 cv2 findcontours 返回一个非常有趣的 numpy 数组 第一张图片是我所拥
  • 从 .NET 应用程序中读取和解码存储在图像或 PDF 文件中的 PDF-417 条形码

    我正在寻找一个能够解码来自PDF 417条码嵌入图像文件或 PDF 中 此时 我只能找到一个Java版本 and a C版 理想情况下 这个库应该是开源且免费的 但我怀疑这样的解码器是否存在 我愿意尝试您可能已经使用过的现有产品的演示 这让
  • Python请求,如何为每个请求绑定不同的源ip? [复制]

    这个问题在这里已经有答案了 我正在尝试学习一些Python 但我在要测试的内容中遇到了逻辑问题 目前 我的代码编写方式是在进程启动时绑定到 source address 不会改变 import socket import requests
  • 如何通过 JavaScript 禁用 Chrome 的已保存密码提示设置

    有没有办法借助 JavaScript 或 jQuery 来操作 Chrome 设置 我想使用 JavaScript 禁用保存密码弹出气泡 这个怎么做 现在我将回答我自己的问题 它可以在 chrome 和 mozilla firefox 中完
  • 将 ForEachAsync 与 Action 内的 wait 一起使用时不等待

    以下应该返回 C 但它返回 B using System Data Entity var state A var qry from f in db myTable select f await qry ForEachAsync async
  • 如何告诉 Visual Studio 在出现特定异常时不要中断?

    我有一个特定类型的异常 我希望 Visual Studio 能够处理该异常不继续并显示异常助手屏幕 本质上 我希望它只是让我的正常异常处理基础设施来处理它 该异常是 System Exception 的继承者 我编写了它并拥有其源代码 任何
  • Phonegap - 如何使状态栏变黑?

    非常简单的一个问题 我似乎找不到答案 我如何将 iPhone 状态栏 顶部的细栏 带有接收 电池等 从默认灰色更改为黑色PhoneGapiPhone 应用程序 谢谢 格伦 PhoneGap iPhone 应用程序只是一个常规的 Xcode
  • WPF DataGrid SelectedItem 绑定在项目更改后停止工作

    我的问题 情况非常类似于Wpf DataGrid SelectedItem 在单元格编辑后失去绑定但我没有使用任何 自定义 WPF 框架 我有一个实现的模型INotifyPropertyChanged and IEditableObject
  • 重用异步套接字:后续连接尝试失败

    我试图在异步 HTTP 客户端中重用套接字 但我无法第二次连接到主机 我基本上将异步 HTTP 客户端视为具有以下状态的状态机 可用 插座可供使用 正在连接 套接字正在连接到端点 发送 套接字正在向端点发送数据 正在接收 套接字正在从端点接
  • 为什么书上说“编译器在内存中为变量分配空间”?

    为什么书上说 编译器在内存中为变量分配空间 这不是可执行文件吗 我的意思是 例如 如果我编写以下程序 include
  • 无法循环打开 png 设备

    我一直在摆弄 R 中的一个函数 长话短说 我有一个for loop 在每一步 我使用保存一个图png 然后立即readPNG这样我就可以提取RGB信息 然后我制作第二个情节 然后readPNG这样我就可以比较两个图像的 RGB 问题是我不断
  • Snowflake (LEFT JOIN) LATERAL:无法评估不支持的子查询类型

    横向连接 在 FROM 子句中 LATERAL 关键字允许内联视图引用该内联视图之前的表表达式中的列 横向连接的行为更像是相关子查询 而不是大多数连接 让我们稍微调整一下文档中提供的代码 CREATE TABLE departments d
  • 导入错误:未找到 MagickWand 共享库 [windows]

    早上好 经过多次尝试运行 from wand image import Image 我收到以下错误 Traceback most recent call last File C Users XXXXX PycharmProjects PDF
  • Botframework:如何使用机器人处理长时间运行的任务?

    如何处理机器人上长时间运行的任务 以便客户端不会在 15 秒后再次尝试发送消息 我有一个带有 botframework v3 的机器人 并通过直线连接客户端 The 直达专线通道连接器本身不会重试发送消息 如果它在向您的机器人发送消息后 1
  • 获取 .NET 对象的内存地址 (C#)

    我试图追踪单声道运行时中的一个错误 其中一个变量似乎分配给一个有效对象 然后稍后重新分配给一个虚假对象 特别是 early in code I allocate fine var o new object valid allocation
  • 两个 ddev 项目之间的通信

    我有两个需要相互交互的 ddev 项目 当遇到一些问题时 我会检查连接的已解析 IP 我通过 ssh 进入 project1 并 ping project2 来完成此操作 ping project2 ddev local 域名解析为 127
  • Spring security oauth 2简单示例

    我尝试根据官方教程实现我自己的示例Sparklr2 Tonr2 一切看起来都不错 但是当我从web xml in my Tonr2实现 弹簧安全过滤器我有例外 尚未为当前请求建立重定向 URI 我不明白我应该使用什么 URL 这是我的代码