1

我有一个 spring cloud oauth2 服务器启动并运行 jwt 和一个配置如下所示的客户端

@EnableOAuth2Sso
public class PortalApplication {

    public static void main(String[] args) {
        SpringApplication.run(PortalApplication.class, args);
    }

    @Component
    public static class LoginConfigurer extends OAuth2SsoConfigurerAdapter {

        @Override
        public void match(RequestMatchers matchers) {
            matchers.antMatchers("/**");
        }

        @Override
        public void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests()
                    .anyRequest()
                    .authenticated()
                .and()
                .csrf()
                    .csrfTokenRepository(csrfTokenRepository())
                .and()
                .addFilterAfter(csrfHeaderFilter(), CsrfFilter.class);
        }

        private Filter csrfHeaderFilter() {
            return new OncePerRequestFilter() {
                @Override
                protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
                    CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
                    if (csrf != null) {
                        Cookie cookie = new Cookie("XSRF-TOKEN", csrf.getToken());
                        cookie.setPath("/");
                        response.addCookie(cookie);
                    }
                    filterChain.doFilter(request, response);
                }
            };
        }

        private CsrfTokenRepository csrfTokenRepository() {
            HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
            repository.setHeaderName("X-XSRF-TOKEN");
            return repository;
        }
    }
}

这一切都很好,应用程序重定向到身份验证服务器并在完成后重定向回 Portal 应用程序。

客户二号

但现在我有了另一个客户,它是一家网店。

如果用户在身份验证服务器上登录,那么网上商店应在顶部显示一个栏,其中包含指向用户先前订单和个人资料的链接。

如果用户没有登录,那么在他们必须登录或创建帐户的结帐步骤之前什么都不会发生。

问题

如果我已经登录到身份验证服务器,如何自动登录,否则允许匿名用户?

我可以以某种方式使用 spring.oauth2.resource.userInfoUri 来实现这一点吗?

4

0 回答 0