springoauth2与google-redirection问题联系起来

insrf1ej  于 2021-07-23  发布在  Java
关注(0)|答案(0)|浏览(137)

所以我正在学习oauth2,目前我正在尝试使用google登录。
问题是我不知道如何成功地处理已登录客户端的请求。现在我的应用程序的工作方式是,它只与直接请求到它的来源(我不知道如何配置我的React客户端,它的请求到服务器根本不工作)重定向每个请求(通过302)到https://accounts.google.com,重定向回我的应用程序。
应用程序属性

spring.security.oauth2.client.registration.google.client-id=123
spring.security.oauth2.client.registration.google.client-secret=456
spring.security.oauth2.resource.filter-order = 3

证券配置

@Configuration
    @Order(2)
    public static class OauthSecurityConfig extends WebSecurityConfigurerAdapter {

        public OauthSecurityConfig() {
            super();
        }

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.cors()
                    .and()
                    .authorizeRequests()
                    .antMatchers(HttpMethod.GET, "/oauth/**")
                    .authenticated()
                    .anyRequest()
                    .permitAll()
                    .and()
                    .oauth2Login();
        }
    }

控制器

@Autowired
    private OAuth2AuthorizedClientService authorizedClientService;

    Random random = new Random(23);

    @GetMapping
    public void getData(HttpServletResponse httpServletResponse, OAuth2AuthenticationToken authentication) throws InterruptedException {
        OAuth2AuthorizedClient client = authorizedClientService.loadAuthorizedClient(authentication.getAuthorizedClientRegistrationId(), authentication.getName());
        String token = client.getAccessToken().getTokenValue();

        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException ie) {
            throw new InterruptedException("InterruptedException");
        }

        httpServletResponse.setHeader("Location", "http://localhost:3000/random?num="+String.valueOf(random.nextInt(100))+"&token="+token);
        httpServletResponse.setStatus(302);
    }

这个控制器的方法实现最好地解释了我的问题,这是我现在唯一能让它工作的方法。它只在我直接访问服务器时才起作用( http://localhost:8081 )并重定向回客户端( http://localhost:3000 )...

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题