Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 与Spring和Thymeleaf的注销链接;后置错误_Java_Spring - Fatal编程技术网

Java 与Spring和Thymeleaf的注销链接;后置错误

Java 与Spring和Thymeleaf的注销链接;后置错误,java,spring,Java,Spring,以下问题: 我用Spring创建了一个简单的注销: <form th:action="@{/logout-custom}" method="POST" name="logoutForm" id="logout"> <input type="submit"/> </form> 控制器: //Logout @RequestMapping(value="/logout-custom", method = Reques

以下问题:

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

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

//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
@GetMapping("/loginForm")
public String loginForm() {
    return "loginForm";
}
依赖项:

               <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>

org.springframework.boot
弹簧靴起动器执行器
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧启动安全
org.apache.tomcat.embed
汤姆卡特·贾斯珀
假如
org.springframework.boot
弹簧靴开发工具
运行时
org.springframework.boot
弹簧起动试验
测试
org.springframework.boot
弹簧启动试验
朱尼特
朱尼特
当我点击注销按钮时,它会显示一个“Request method'POST'not supported”错误

使用GET方法,它只是在我的url后面添加了一个“?”符号,既没有显示错误,也没有重定向。 我删除了html中的所有内容,除了表单,因为似乎有些脚本阻止了整个过程(这是后面的问题)。
我还尝试删除控制器,只使用logout().logoutUrl().logoutSuccessUrl(),但这也不起作用。

我已经有一段时间没有将spring与thymeleaf一起使用了,但我认为RedirectView必须指向url。我自己也试过你的例子,但确实不起作用。然而,一些细微的变化使它起了作用:

如果控制器中尚不存在登录url,请将其添加到登录:

//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
@GetMapping("/loginForm")
public String loginForm() {
    return "loginForm";
}
更改您的视图:

return new RedirectView("/loginForm");
以下是资源结构:

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

默认情况下添加了
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就可以了。

为什么要使用
.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>


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

我知道。我认为这不必指向与控制器和注销表单相同的路径。是吗?我只是尝试将所有3个指向注销自定义。相同的错误..日志中的确切异常是什么,根本原因是什么?此消息可能属于stacktrace的最后一个异常。@benjaminc stacktrace是:不支持请求方法“POST”[nio-8080-exec-7].w.s.m.s.DefaultHandlerExceptionResolver:由处理程序执行引起的已解决异常:org.springframework.web.HttpRequestMethodNotSupportedException:请求方法'POST'不受支持您可以发布您的spring pom依赖项吗?@CrazySabPath当然。当我像您所说的那样做时,没有指定的注销页面?我按您所说的那样更改了它,但它仍然不起作用。我也试着在我的控制器和我的表单上给URL命名,但是没有改变任何东西。你是说,注销发生了,你需要在注销后显示页面吗?或者根本没有注销?我猜根本没有注销。它只是显示POST错误。请尝试我的解决方案并设置。logoutSuccessUrl(“/login”);例如,它将参考登录页面OK,谢谢。但是我没有logout.html我在home.html上注销,这是上面的一个文件夹。更改重定向视图(“/login success”)(这是我的登录重定向)对我不起作用。logout.html不重要,这只是一个例子,因为我没有你的.html文件,其中包括注销表单。我在你写的时候更改了它,但是没有CustomLogoutHandler。我在表单中将我的url更改为/logout,但它仍然不起作用。@temp操作和配置的注销url必须匹配,因此我更新了我的回答。我只是按照用户的建议做了下面的评论。即使是那个简单的解决方案也不起作用。我想我的表格可能有问题。。你知道为什么吗?@temp你能试着用一个链接来代替表单吗?哦,哇,终于成功了!我也注销了。可能是百里香叶出错了?原因正如我所尝试的:它不是一个普通的链接,而是不起作用。编辑:甚至表单在删除thymeleaf后也可以工作!