将google Oauth2登录名添加到已存在的JWT密码流登录名中

时间:2019-07-15 02:50:47

标签: java spring oauth-2.0

我有一个使用密码流和JWT模式提供身份验证的代码。但是,我想添加Google oauth2登录的实现以一起工作。我该怎么办?

我尝试了Internet上的许多教程,例如https://spring.io/guides/tutorials/spring-boot-oauth2/#_social_login_manual等。但是,方法已经配置好(JWT),我不打算如何合并这两种登录类型。

@Configuration
@EnableResourceServer
@RestController
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Order(-100)
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {




    @Override
    public void configure(HttpSecurity httpSecurity) throws Exception {

        httpSecurity.authorizeRequests()
            .antMatchers("/login**","/login/oauth2/code/google","/oauth2/authorization/google", "/webjars/**","/","/oauth/authorize" ,"/apis/*","/error**","/login/google","/oauth/token").permitAll()
            .anyRequest().authenticated()
            //.and().formLogin().loginPage("/").loginProcessingUrl("/formlogin")
            .and()
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
            .csrf().disable()
            .oauth2Login().loginPage("/");

    }


    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.stateless(true);
    }

    @Bean
    public MethodSecurityExpressionHandler createExpressionHandler() {
        return new OAuth2MethodSecurityExpressionHandler();
    }

}

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private UserDetailsService userDetailsService;


    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
            .withClient("react")
            .secret("***")
            .scopes("read","write")
            .authorizedGrantTypes("authorization_code","password","refresh_token")
            .accessTokenValiditySeconds(1800)
            .refreshTokenValiditySeconds(3600*24);
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

        endpoints.tokenStore(tokenStore())
                 .accessTokenConverter(accessTokenConverter())
                 .reuseRefreshTokens(false)
                 .userDetailsService(userDetailsService)
                 .authenticationManager(authenticationManager);


    }

    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {

        JwtAccessTokenConverter accessTokenConverter = new JwtAccessTokenConverter();
        accessTokenConverter.setSigningKey("secret");

        return accessTokenConverter;
    }

    @Bean
    public TokenStore tokenStore() {
        // TODO Auto-generated method stub
        return new JwtTokenStore(accessTokenConverter());
    }

}

非常感谢您的关注。

0 个答案:

没有答案
相关问题