与Spring和Thymeleaf的注销链接;发布错误

时间:2018-10-18 06:38:35

标签: java spring

以下问题:

我用Spring创建了一个简单的注销:

    <form th:action="@{/logout-custom}" method="POST" name="logoutForm" 
    id="logout">
        <input type="submit"/>
    </form>   

安全性:

@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").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>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-test</artifactId>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>

当我单击注销按钮时,它显示“不支持请求方法'POST'”错误。

使用GET方法,它只是添加了一个“?”在我的网址后面签名,不显示错误或重定向。 我从html中删除了所有内容(表单除外),因为似乎某些脚本阻止了整个过程(这是以后的问题)。 我还尝试删除控制器,仅使用logout()。logoutUrl()。logoutSuccessUrl(),但这也行不通。

3 个答案:

答案 0 :(得分:1)

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即可达到目的。

答案 1 :(得分:0)

自从我在百里香上使用spring以来已经有一段时间了,但是我认为RedirectView必须指向一个URL。我自己尝试过您的示例,但确实没有用。进行了一些细微的更改,使其起作用:

如果您的控制器中不存在该网址,请添加该网址以登录:

@GetMapping("/loginForm")
public String loginForm() {
    return "loginForm";
}

更改您的redirecView:

return new RedirectView("/loginForm");

这是资源结构:

resources
  -> templates
    -> loginForm.html
    -> logout.html

答案 2 :(得分:0)

为什么使用torch.Tensor

同时使用.logout().logoutUrl("/logout").permitAll(),它们应该是相同的URL

或者简单点

@RequestMapping(value="/logout-custom", method = RequestMethod.POST)

在安全配置下使用。

<form th:action="@{/logout}" method="post">
         <button type="submit">Sign Out</button>        
 </form>

默认情况下,注销功能已经存在。