当前位置: 首页 > 知识库问答 >
问题:

只登录特定的网址

归鸿朗
2023-03-14

我正在尝试通过Spring Security进行登录的oAuth2配置设置。但仅限于特定的网址。

我的安全配置如下。

@Override
public void configure(HttpSecurity http) throws Exception {
    http
            .antMatcher("/secured/**")
            .authorizeRequests()
            .anyRequest()
            .authenticated()
    .and()
    .oauth2Login()
    .clientRegistrationRepository(clientRegistrationRepository())
    .authorizedClientService(authorizedClientService());
}

基本上,我只希望oauth2Login成为以/secured开头的URL的触发器。它似乎几乎可以工作,唯一的问题是每当Spring试图将我的会话重新定向到google进行身份验证时,它就会触发404。

oauth身份验证的标准重定向应该触发到http://localhost:8080/oauth2/authorization/google,我的应用程序试图这样做,但它是404的。

我假设http://localhost:8080/oauth2/authorization/googleurl被某种类型的安全配置阻止了?但我一辈子都搞不懂为什么。

我猜我需要找到正确的组合。permitAll()对于任何指向“/oAuth2/授权/**”的请求

我在下面试过这个。

    @Override
public void configure(HttpSecurity http) throws Exception {
    http
            .antMatcher("/secured/**")
            .authorizeRequests()
            .antMatchers("/oauth2/authorization/**")
            .permitAll()
            .anyRequest()
            .authenticated()
    .and()
    .oauth2Login()
    .clientRegistrationRepository(clientRegistrationRepository())
    .authorizedClientService(authorizedClientService());
}

但这行不通。。。。。有人看到我的问题了吗?我没有其他与此冲突的安全配置,我有点不知所措。

共有3个答案

奚无尘
2023-03-14

@eleftheria stein kousathana的回答让我想到了另一个可能的解决方案。

如上所述,知道OAuthendpoint不会被调用是很重要的,因为它们的安全配置直接绑定到HttpSecurity对象后面的antMatcher()调用。

但是,正如文档中所述,您还可以更改重定向endpoint和授权endpoint的基Uris

@Override
public void configure(HttpSecurity http) throws Exception {
    http
            .antMatcher("/secured/**")
            .authorizeRequests()
            .anyRequest()
            .authenticated()
    .and()
    .oauth2Login()
       //New Code Starting here
       .authorizationEndpoint()
          .baseUri("/secured/oauth2/authorization")
        .and()
        .redirectionEndpoint()
          .baseUri("/secured/oauth2/code/*")
        .and()
       //new code ending here
    .clientRegistrationRepository(clientRegistrationRepository())
    .authorizedClientService(authorizedClientService());
}

请务必更改您的客户注册的reDirectUriTemboard以及在您的AuthorizationServer配置的重定向uri

并且一定要添加星号在重定向Endpoint.baseUri的结尾...

尉迟卓
2023-03-14

理解这个问题的第一步是理解http之间的区别。antMatcher()和http。授权请求()

让我们看看下面的配置。
(我使用lambda风格的配置,从Spring Security 5.2开始可用,以使其更具可读性)

java prettyprint-override">@Override
public void configure(HttpSecurity http) throws Exception {
    http
            .antMatcher("/secured/**")
            .authorizeRequests(authorize -> authorize
                    .anyRequest().authenticated()
            )
            .oauth2Login(oauth2Login -> oauth2Login
                    .clientRegistrationRepository(clientRegistrationRepository())
                    .authorizedClientService(authorizedClientService())
            );
}

在这里,我们指定HTTP安全性仅在匹配"/安全/**"时调用。

换句话说,如果请求与“/secured/**”匹配,则该请求将仅由SecurityFilterChain处理。

这是一个问题,因为SecurityFilterChain是从“/oauth2/Authorization/google”发起授权请求的。

但是,由于“/oauth2/authorization/google”“/secured/**”不匹配,因此未调用安全过滤器链。

请考虑以下配置。

@Override
public void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests(authorize -> authorize
                    .antMatchers("/secured/**").authenticated()
                    .anyRequest().permitAll()
            )
            .oauth2Login(oauth2Login -> oauth2Login
                    .clientRegistrationRepository(clientRegistrationRepository())
                    .authorizedClientService(authorizedClientService())
            );
}

在这里,将为所有请求调用HTTP安全性。

但是,只有符合“/secured/**”的请求才会要求用户进行身份验证。

曹振
2023-03-14

根据之前的回答,没有lambda样式的版本应该是这样的吧?

@Override
public void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests
            .antMatchers("/secured/**").authenticated()
            .anyRequest().authenticated()
            .and()
            .oauth2Login()
            .clientRegistrationRepository(clientRegistrationRepository())
            .authorizedClientService(authorizedClientService());
}

如果我们只希望oAuth2Login为"/安全/**",为什么我们要使用验证()antMatcher("/安全/**")anyRequest()(而不是permitAll()anyRequest())?

谢谢你。

 类似资料:
  • 1. 申请应用 参考文章:twitter三方登录的实现 (opens new window),只需关注创建应用部分即可。 copy以下三个信息:App ID、App Key和网站回调域。 友情提示:twitter现不支持个人用户创建应用 重要提示 “应用密钥”可保护你应用程序的安全,因此请确保其不会泄露!也不要与任何人共享你的“应用密钥”!!! 2. 集成JustAuth 2.1 引入依赖 <de

  • 本文向大家介绍magento 登录网址,包括了magento 登录网址的使用技巧和注意事项,需要的朋友参考一下 示例 $this->helper('customer/data')->getLoginUrl(); 要么 法师:: helper('customer / data')-> getLoginUrl();

  • 我有一个logger配置,它基本上将根记录器的所有日志写入一个基本日志文件。我总是使用此处。 对于我想记录的一些特定事件,请使用不同的文件。我怎么能在属性文件中配置这样一个记录器(例如给它一个名字,然后我可以从代码中引用)?

  • 问题内容: 使用Jsoup登录网站需要什么?我相信我的代码是正确的,但是我从未使用Jsoup成功登录到网站,因此我可能会丢失一些东西。这是我的代码: 返回一个显示未成功登录的登录页面。有一个名为的输入值,我认为这可能是为什么它不起作用的原因。这个值会随着时间的推移而上升。我运行了两次代码,时间变量返回了和。我的代码需要大约10秒钟的时间来打印文档,因此,变量在发送发布请求时是否已经更改?我不确定这

  • 我使用nginx作为反向代理。我正在使用别名重写目录。在我的alias目录中,我有Angular 2构建文件,它们使用html5路由。所以当用户刷新页面时,它应该重写到索引中。html仅存在于该目录中。 “控件窗格被别名覆盖” 我已经尝试过“try_files$uri$uri//index.html=404但这是指指数。主目录中的html。 我也试着给出一条绝对路径,但仍然不起作用。

  • 在我的laravel 5.2应用程序中,我正在努力解决我的路由问题。 预期的行为是: 未注册用户试图访问受保护的页面 重定向到登录 重定向(成功)到预期页面 当然,如果没有预期的页面(当用户主动登录时),则应将其重定向到“仪表板” 例子: 用户将被重定向到登录页面,但是,登录后,他将被重定向到我在authcontroller中定义为$redirectTo的页面 有没有办法将重定向设置为类似(int