有没有办法使用 Spring Security 实现 google 登录和使用 jwt 令牌自定义登录?

Posted

技术标签:

【中文标题】有没有办法使用 Spring Security 实现 google 登录和使用 jwt 令牌自定义登录?【英文标题】:Is there a way to implement google sign in with and custom login with jwt token using spring security? 【发布时间】:2021-08-19 00:27:05 【问题描述】:

由于我是新的 springboot,我遇到了一个问题,请帮助我。 我有自定义登录页面,它需要用户名和密码并对其进行验证。如果用户 目前,在我的数据库中然后生成 jwt 令牌,我已经实现了这个,这种情况是 在职的。现在我的问题是

    I am trying to integrate the google sign-in. But while integrating the google sign-in, I am 
    getting authorized as anonymous user and I couldn't able proceed further. This is not the
   case I dont want.
   when ever user logged with google sign in option user must be able to sign in and could able to 
   generate the jwt token. How  can I solve my problem. I am using technology springboot and reactjs.

我的安全配置代码。

 public class Springsec extends WebSecurityConfigurerAdapter 
@Override
public void configure(HttpSecurity http) throws Exception
    http
    .csrf()
    .disable()
    .antMatcher("/**")
    .authorizeRequests()
    .antMatchers("/", "/index.html","/error/**")
    .permitAll()
    .anyRequest().authenticated().and().formLogin();
  /*  http
    .exceptionHandling()
    .authenticationEntryPoint(auth)
    .and()
    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    http.addFilterBefore(securityFilter, UsernamePasswordAuthenticationFilter.class);
        

Is below one 是解决我的问题的唯一方法,或者有什么方法可以解决问题

         google sign in must me authorized by reactjs then we have to send access token to the server 
         and generate the jwt token.

       If there is any other way pls help out. If possible send me sample code.
        If My guess is the only way to solve the problem, Then simply say yes.
    

谢谢。 请帮帮我。

【问题讨论】:

【参考方案1】:

我已经写过这个here。您的配置方法可能类似于:

    @Override                                                                                                                                                                                                                      
        protected void configure(HttpSecurity http) throws Exception                                                                                                                                                                  
            http.antMatcher("/**")                                                                                                                                                                                                     
                .authorizeRequests(t -> t.anyRequest().authenticated())                                                                                                                                                                
                .formLogin(t -> t.loginPage("/login").permitAll())                                                                                                                                                                     
                .logout(t -> t.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))                                                                                                                                              
                              .logoutSuccessUrl("/").permitAll());                                                                                                                                                                     
                                                                                                                                                                                                                                       
            try                                                                                                                                                                                                                       
                ClientRegistrationRepository repository =                                                                                                                                                                              
                    getApplicationContext().getBean(ClientRegistrationRepository.class);                                                                                                                                               
                                                                                                                                                                                                                                       
                if (repository != null)                                                                                                                                                                                               
                    http.oauth2Login(t -> t.clientRegistrationRepository(repository)                                                                                                                                                   
                                           .userInfoEndpoint(u -> u.oidcUserService(oidcUserService))                                                                                                                                  
                                           .loginPage("/login").permitAll());                                                                                                                                                          
                                                                                                                                                                                                                                      
             catch (Exception exception)                                                                                                                                                                                             
                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                       
            http.sessionManagement(t -> t.maximumSessions(-1).sessionRegistry(sessionRegistry()));                                                                                                                                     
        

以及 OAuth2 配置(在 application.yaml 中):

---
spring:
  security:
    oauth2:
      client:
        registration:
          google:
            client-id: XXXXXXXXXXXXXXXXXXXX
            client-secret: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
        provider:
          google:
            user-name-attribute: email

【讨论】:

非常感谢您的回复,但是我们如何为表单和 oauth2 登录添加 jwt 令牌。我们如何验证 osuth2 的 jwt 令牌 我不知道有任何 Spring/Oauth2 提供者可以简单地加入。如果共存的表单登录和 Oauth2/OIDC 不是您想要的,那么我建议使用类似的服务Okta 为您的应用程序提供 SSO。 This 可能就是您要找的。​​span> 感谢您的回答解决了我的问题

以上是关于有没有办法使用 Spring Security 实现 google 登录和使用 jwt 令牌自定义登录?的主要内容,如果未能解决你的问题,请参考以下文章

#私藏项目实操分享# Spring专题「开发实战」Spring Security与JWT实现权限管控以及登录认证指南

#私藏项目实操分享#Spring专题「技术原理」Spring Security的核心功能和加载运行流程的原理分析

有没有办法在 Spring Security 插件的属性文件中定义角色而不是硬编码它们?

使用 Spring Security 的 CSRF 保护

使用 Spring Security 的 CSRF 保护

没有 Spring 标记库 (JSTL) 的 Spring Security