Spring Security 5.3.2 - 如何将 oauth2-login 添加到现有的安全配置中?



我有一个现有的应用程序,它有自己的身份验证方案,它基本上像这样做一个包罗万象:

<sec:http use-expressions="true" auto-config="true" entry-point-ref="myAuthenticationEntryPoint">
<sec:intercept-url pattern="/**" access="isAuthenticated()"/>
... form login config ...

现在我想使用 OAuth2-Login 向此应用程序添加一个新模块,我做到了:

<sec:http pattern="/module/**" use-expressions="true" auto-config="false">
<sec:intercept-url pattern="/module/**" access="authenticated"/>
<sec:oauth2-login/>
</sec:http>
... more OAuth2 Config ...

当我访问像/module/something这样的URL时,我现在被重定向到/oauth2/authorization/myClientId,我相信这是一个Spring Security内部URL。但是,捕获所有配置会获取该请求并将我发送到其表单登录页面。如何告诉 Spring 安全性绕过其自己的内部 URL 的其他配置?

我已经尝试过这样的事情:

<sec:http pattern="/oauth2/**" auto-config="true">
<sec:intercept-url pattern="/oauth2/**" access="permitAll"/>
</sec:http>

但这并没有继续OAuth2登录过程。请求转到某个不相关的控制器,该控制器抛出,因为没有经过身份验证的用户(显然(。

那么我可以在上面的配置中放一些东西来告诉Spring Security继续执行OAuth2-Login过程吗?我假设 Spring Security 中的某个地方有一个内部控制器来处理/oauth2/**请求,我必须以某种方式将我的请求发送给它。

顺便说一下,我的OAuth2-Login配置确实可以独立工作,是集成难倒了我。

经过深思熟虑,我发现我甚至不需要拦截任何 URL 或做一个 permitAll。我只需要告诉 Spring 要使用的登录机制。因此,诀窍是对所有内部 OAuth2 URL 重复 oauth2-login 配置:

<sec:http pattern="/oauth2/**" auto-config="false">
<sec:oauth2-login access-token-response-client-ref="accessTokenResponseClient"/>
</sec:http>
<sec:http pattern="/login/oauth2/**" auto-config="false">
<sec:oauth2-login access-token-response-client-ref="accessTokenResponseClient"/>
</sec:http>

注意:这只是一个示例,您的 oauth2-login 配置可能看起来不同。特别是,您可能没有自定义的OAuth2AccessTokenResponseClient。

最新更新