亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

與 Spring 和 Thymeleaf 的注銷鏈接;發布錯誤

與 Spring 和 Thymeleaf 的注銷鏈接;發布錯誤

肥皂起泡泡 2021-11-24 18:27:33
以下問題:我用 Spring 創建了一個簡單的注銷:    <form th:action="@{/logout-custom}" method="POST" name="logoutForm"     id="logout">        <input type="submit"/>    </form>   安全:@Overrideprotected void configure(HttpSecurity http) throws Exception {    http        .authorizeRequests()            .antMatchers("/cont/**").access("hasRole('USER')")            .and()            .formLogin()            .loginPage("/login")            .defaultSuccessUrl("/login-success", true)            .failureUrl("/failLogin.html")            .permitAll()            .and()            .logout().logoutUrl("/logout").permitAll()        .and()        .csrf()        .disable();}    控制器://Logout@RequestMapping(value="/logout-custom", method = RequestMethod.POST)public RedirectView logoutPage (HttpServletRequest request, HttpServletResponse response) {    Authentication auth = SecurityContextHolder.getContext().getAuthentication();    if (auth != null){        new SecurityContextLogoutHandler().logout(request, response, auth);    }    return new RedirectView("/loginForm.html");}   // redirecting to login Page依賴項:               <dependency>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-starter-actuator</artifactId>            </dependency>            <dependency>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-starter-web</artifactId>            </dependency>            <dependency>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-starter-security</artifactId>            </dependency>    <dependency>        <groupId>org.apache.tomcat.embed</groupId>        <artifactId>tomcat-embed-jasper</artifactId>        <scope>provided</scope>    </dependency>當我單擊注銷按鈕時,它顯示“不支持請求方法‘POST’”錯誤。使用 GET 方法,它只是添加了一個“?” 在我的 url 后面簽名既不顯示錯誤也不重定向。我從我的 html 中刪除了所有內容,除了表單,因為似乎有些腳本阻止了整個事情(這是后來的問題)。我還嘗試刪除控制器并僅使用 logout().logoutUrl().logoutSuccessUrl() 但這也不起作用。
查看完整描述

3 回答

?
當年話下

TA貢獻1890條經驗 獲得超9個贊

在SecurityContextLogoutHandler默認情況下添加,所以你并不需要實現自定義注銷它。


如果你想添加其他LogoutHandler你可以在你的配置中做到:


@Override

protected void configure(HttpSecurity http) throws Exception {

    http

        .authorizeRequests()

            .antMatchers("/cont/**").access("hasRole('USER')")

            .and()


            .formLogin()

            .loginPage("/login")

            .defaultSuccessUrl("/login-success", true)

            .failureUrl("/failLogin.html")

            .permitAll()


            .and()

            .logout().logoutUrl("/logout-custom")

            .logoutSuccessUrl("/login")

            .addLogoutHandler(new CustomLogoutHandler())

            .permitAll()


        .and()

        .csrf()

        .disable();

}

logoutSuccessUrl("/login")成功注銷后,將用戶重定向到登錄。


更新:另外刪除 th: 并使用純 HTML 可以解決問題。


查看完整回答
反對 回復 2021-11-24
?
阿晨1998

TA貢獻2037條經驗 獲得超6個贊

自從我使用 spring 和 thymeleaf 已經有一段時間了,但我認為 RedirectView 必須指向一個 url。我自己試過你的例子,它確實沒有用。然而,一些細微的變化使它起作用:


如果您的控制器中尚不存在 url 以登錄:


@GetMapping("/loginForm")

public String loginForm() {

    return "loginForm";

}

更改您的重定向視圖:


return new RedirectView("/loginForm");

這是資源結構:


resources

  -> templates

    -> loginForm.html

    -> logout.html


查看完整回答
反對 回復 2021-11-24
?
一只萌萌小番薯

TA貢獻1795條經驗 獲得超7個贊

為什么使用 .logout().logoutUrl("/logout").permitAll()


同時,使用@RequestMapping(value="/logout-custom", method = RequestMethod.POST),它們應該是相同的 URL


或者讓它變得簡單


<form th:action="@{/logout}" method="post">

         <button type="submit">Sign Out</button>        

 </form>

并在安全配置上使用。


   .logout().permitAll();

默認情況下,注銷功能已經存在。


查看完整回答
反對 回復 2021-11-24
  • 3 回答
  • 0 關注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號