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

通过MultiHttpSecurityConfig实现oAuth2Login()和http Basic()相同的页面

董和泽
2023-03-14

如果用户单击 /api/*,它将加载“formLogin()”页面;否则加载“http pBasic()”此设置工作正常。下面是它的代码。

@Configuration
    public class SecurityConfig {

    @Configuration
    @Order(1)
    public static class SpecialSecurityConfig extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                .antMatcher("/api/**")
                .authorizeRequests()
                    .anyRequest().authenticated()
                    .and()
                .formLogin()
                    .loginPage("/api/login");         
        }
        
        @Override
        public void configure(WebSecurity web) throws Exception {
          web.ignoring().antMatchers("/", "/css/**");
        }
    }

    @Configuration
    public static class RegularSecurityConfig extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests()
                    .anyRequest().authenticated()
                    .and()
                .httpBasic();
        }
        
        @Override
        public void configure(WebSecurity web) throws Exception {
          web.ignoring().antMatchers("/", "/css/**");
        }
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("user")
                .password("{noop}pass")
                .roles("USER");
    }
}

现在我想删除“formLogin()”并将其替换为“oauth2Login()之后,当我点击谷歌链接时,它会加载“httpBasic()”登录页面。如果用户点击谷歌,它应该进入谷歌登录页面。请帮我解决这个问题。下面是它的代码。

    http
    .antMatcher("/api/**")
    .authorizeRequests()
    .anyRequest().authenticated()
    .and()
    .oauth2Login()
    .loginPage("/api/oauth_login")
    .permitAll();

oauth_login.html

<body>
<div class="container">
<h1>Social Login</h1>
<p><a href="/oauth2/authorization/google">Google</a></p>
</div>
</body>

共有3个答案

方季同
2023-03-14

禁用基本登录和表单登录。示例安全配置如下;

 .formLogin()
                .disable()
            .httpBasic()
                .disable()
            .exceptionHandling()
                .authenticationEntryPoint(new RestAuthenticationEntryPoint())
                .and()
            .authorizeRequests()
                .antMatchers("/auth/**", "/oauth2/**")
                    .permitAll()
                .anyRequest()
                    .authenticated()
                .and()
            .oauth2Login()
                .authorizationEndpoint()
                    .baseUri("/oauth2/authorize")
                    .and()
                .redirectionEndpoint()
                .baseUri("/oauth2/callback/*")

从前端调用此endpoint(Google/Facebook按钮应链接到下面的链接)
http://localhost:8080/oauth2/authorize/{provider}?重定向_uri=

遵循这个指南。这正是你想要的。

公孙弘图
2023-03-14

我参考下面的URL并修改我的代码

仅针对特定URL的Oauth2Login

http 
        .authorizeRequests()
        .antMatchers("/api/**").authenticated()        
        .anyRequest().authenticated()
        .and()
        .oauth2Login()
        .loginPage("/api/oauth_login")
        .defaultSuccessUrl("/api/home")
        .permitAll();

普通URL(除了 /api/*)也加载谷歌登录页面。

从劲
2023-03-14

您指定匹配"/api/**"的请求应由使用OAuth 2登录的特别安全配置保护,所有其他请求应由使用HTTP基本的定期安全配置保护。

由于“/oauth2/authorization/google”“/api/**”不匹配,因此使用HTTP basic对其进行保护。

一个选项是将用于授权请求的基本URI更改为以“/api/”开头(默认值为“/oauth2/authorization/{registrationId}”)。

您可能还需要自定义loginProcessingUrlauthorizationRequestResolver

public void configure(HttpSecurity http) throws Exception {
    http
        .antMatcher("/api/**")
        .authorizeRequests(authorize -> authorize
            .anyRequest().authenticated()
        )
        .oauth2Login(oauth2 -> oauth2
            .loginProcessingUrl("/api/login/oauth2/code/*")
            .loginPage("/api/oauth_login")
            .authorizationEndpoint(ae -> ae
                .baseUri("/api/oauth2/authorization/{registrationId}")
                .authorizationRequestResolver(getAuthorizationRequestResolver())
            )
        );
}

private OAuth2AuthorizationRequestResolver getAuthorizationRequestResolver() {
    return new DefaultOAuth2AuthorizationRequestResolver(
        this.clientRegistrationRepository,
        "/api/oauth2/authorization");
}

然后你也会更新你的登录表单

<p><a href="/api/oauth2/authorization/google">Google</a></p>
 类似资料:
  • OAuth2Login 是  ASP.NET C# 开源库,抽象了 Facebook, Google, Twitter, PayPal 社交账号登录。 特性: 向后兼容,Microsoft 提供的是面向 OWIN 和 KatanaBackward,你的网站也可以使用现有的,但当你只需要简单的网站登录时,可能就不需要植入所有的新技术。 独立性,Independency -微软的 OAuth 提供者的

  • 本文向大家介绍nodejs通过phantomjs实现下载网页,包括了nodejs通过phantomjs实现下载网页的使用技巧和注意事项,需要的朋友参考一下 功能其实很见简单,通过 phantomjs.exe 采集 url 加载的资源,通过子进程的方式,启动nodejs 加载所有的资源,对于css的资源,匹配css内容,下载里面的url资源 当然功能还是很简单的,在响应式设计和异步加载的情况下,还是

  • 本文向大家介绍通过 Django Pagination 实现简单分页功能,包括了通过 Django Pagination 实现简单分页功能的使用技巧和注意事项,需要的朋友参考一下 作者:HelloGitHub-追梦人物 文中所涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 当博客上发布的文章越来越多时,通常需要进行分页显示,以免所有的文章都堆积在一个页面,影响用户体验。 Dj

  • 预期: 博客首页是/blog 通过/blog?page=1&category=1返回不同的数据 但我用link跳转后发现数据不刷新,但我手动刷新有效果,我看官方文档(https://nextjs.zhd.icu/docs/routing/shallow-routing)说使用shallow,但我试完并没效果,跳转后getServerSideProps依然没有触发,请问这是为什么?有大佬告知吗?

  • 我创建了一个云表单模板并成功部署了它。我在同一 VPC、同一子网但安全组不同,有两个 EC2 实例。其中一个EC2实例是MongoDB服务器安装的,另一个实例正在运行节点服务器。我能够毫无问题地访问这两个实例,当我尝试从节点服务器连接到MongoDB时,会出现问题。它不起作用。我已经深入研究了两个服务器无法相互连接的问题。以下是我的安全组 < li >数据库服务器 < li >应用程序服务器 在这