【问题标题】:Oauth2 client logout doesn't workOauth2 客户端注销不起作用
【发布时间】:2018-10-26 16:15:33
【问题描述】:

我尝试使用这里描述的方法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

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

【问题讨论】:

    标签: java spring spring-cloud spring-security-oauth2


    【解决方案1】:

    注销端点应该是/logout 而不是/client/logout

    【讨论】:

    • /client 是应用程序上下文
    • 在问题中,您使用了不匹配的绝对 URL 和相对 URL。因此,尚不清楚这些是否正确。如果/client 是应用程序上下文,/hello 无效。
    • 我还不能评论你的问题,所以我把我的评论放在这里...如果你把.logoutSuccessUrl("/")改成.logoutSuccessUrl("/hello")你应该直接进入正确的页面
    • 不幸的是这不是事实
    • 我尝试了你的建议。如果您有时间了解 - 我可以用我的示例分享 github repo。
    猜你喜欢
    • 2018-10-25
    • 1970-01-01
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    • 2019-10-14
    • 2018-12-27
    • 1970-01-01
    • 2020-04-10
    相关资源
    最近更新 更多