복붙노트

[SPRING] Oauth2 클라이언트 로그 아웃이 작동하지 않습니다.

SPRING

Oauth2 클라이언트 로그 아웃이 작동하지 않습니다.

https://spring.io/guides/tutorials/spring-boot-oauth2/#_social_login_logout에 설명 된 방법을 사용하려고합니다.

그래서 나는 백엔드 코드베이스를 따라왔다.

@EnableAutoConfiguration
@Configuration
@EnableOAuth2Sso
@Controller
public class ClientApplication extends WebSecurityConfigurerAdapter {
    private Logger logger = LoggerFactory.getLogger(ClientApplication.class);

    @RequestMapping("/hello")
    public String home(Principal user, HttpServletRequest request, HttpServletResponse response, Model model) throws ServletException {
        model.addAttribute("name", user.getName());
        return "hello";
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        http.antMatcher("/**")
                .authorizeRequests()
                .antMatchers( "/login**", "/webjars/**", "/error**").permitAll()
                .anyRequest()
                .authenticated()
                .and().logout().logoutSuccessUrl("/").permitAll()
                .and()
                    .csrf()
                    .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
        // @formatter:on
    }

    public static void main(String[] args) {
        new SpringApplicationBuilder(ClientApplication.class)
                .properties("spring.config.name=application").run(args);
    }
}

프론트 엔드 다음 :

js :

<script type="text/javascript">
        $.ajaxSetup({
            beforeSend: function (xhr, settings) {
                if (settings.type == 'POST' || settings.type == 'PUT'
                    || settings.type == 'DELETE') {
                    if (!(/^http:.*/.test(settings.url) || /^https:.*/
                            .test(settings.url))) {
                        // Only send the token to relative URLs i.e. locally.
                        xhr.setRequestHeader("X-XSRF-TOKEN",
                            Cookies.get('XSRF-TOKEN'));
                    }
                }
            }
        });
        var logout = function () {
            $.post("/client/logout", function () {
                $("#user").html('');
                $(".unauthenticated").show();
                $(".authenticated").hide();
            });
            return true;
        };
        $(function() {
            $("#logoutButton").on("click", function () {
                logout();
            });
        });

    </script>

및 html :

<input type="button" id="logoutButton" value="Logout"/>

하지만 작동하지 않습니다. 다음과 같이 행동하십시오.

애플리케이션 로그인 성공 후 로그 아웃 버튼을 클릭합니다. POSThttp : // localhost : 9999 / client / logout을 실행합니다. http : // localhost : 9999 / client / logout은 http : // localhost : 9999 / client로 리디렉션되지만이 페이지는 존재하지 않습니다. 그런 다음 localhost : 8080 / client / hello에 액세스합니다 - 보안 페이지가 표시됩니다.

/ client는 응용 프로그램 컨텍스트입니다.

application.yml 프래그먼트 :

server:
  servlet:
    context-path: /client

gitub의 소스 코드 : 클라이언트 - https://github.com/gredwhite/logour_social-auth-client (localhost : 9999 / client / hello url 사용) 서버 - https://github.com/gredwhite/logout_social-auth-server

해결법

  1. ==============================

    1.로그 아웃 엔드 포인트는 / client / logout 대신 / logout이어야합니다.

    로그 아웃 엔드 포인트는 / client / logout 대신 / logout이어야합니다.

  2. from https://stackoverflow.com/questions/50373750/oauth2-client-logout-doesnt-work by cc-by-sa and MIT license