Oauth2客户端注销不起作用

时间:2018-05-16 14:36:03

标签: java spring spring-cloud spring-security-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"/>

但它不起作用。行为如下:

成功登录应用程序后,我点击退出按钮 它会触发POST http://localhost:9999/client/logout
http://localhost:9999/client/logout重定向到http://localhost:9999/client,但此页面不存在。然后我访问localhost:8080/client/hello - 我看到了安全页面

P.S。

/client是应用程序上下文:

application.yml片段:

server:
  servlet:
    context-path: /client
gitub上的

源代码:
客户 - https://github.com/gredwhite/logour_social-auth-client(使用localhost:9999/client/hello网址)
服务器 - https://github.com/gredwhite/logout_social-auth-server

1 个答案:

答案 0 :(得分:0)

注销终结点应为/logout而不是/client/logout