有没有一种方法可以使用SpringSecurity实现google登录和使用jwt令牌自定义登录?

ccrfmcuu  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(366)

因为我是新来的springboot,我被一个senario困住了,请帮帮我。我有一个自定义的登录页面,其中包含用户名和密码并进行验证。如果用户出现在我的数据库中,那么生成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);
        }

}
下面一个是解决我问题的唯一方法还是有解决问题的方法

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.

非常感谢。请帮帮我。

58wvjzkj

58wvjzkj1#

我在这里写过。您的配置方法可能类似于:

@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

相关问题