Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.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 是否允许仅通过重定向访问页面?_Java_Spring_Spring Security_Thymeleaf - Fatal编程技术网

Java 是否允许仅通过重定向访问页面?

Java 是否允许仅通过重定向访问页面?,java,spring,spring-security,thymeleaf,Java,Spring,Spring Security,Thymeleaf,我正在使用Spring Boot和Thymeleaf开发一个应用程序 我的自定义登录页面中有以下代码段: <p th:if="${param.logout}">Logged out successfully</p> 因此,注销后,用户被重定向到/login?logout,并显示注销消息 我的问题是,当用户只需编写http://myserver:8080/login?logout进入浏览器 有没有可能阻止用户直接访问/login?注销(从而仅在实际注销时显示消息)?尝试此

我正在使用Spring Boot和Thymeleaf开发一个应用程序

我的自定义登录页面中有以下代码段:

<p th:if="${param.logout}">Logged out successfully</p>
因此,注销后,用户被重定向到
/login?logout
,并显示注销消息

我的问题是,当用户只需编写
http://myserver:8080/login?logout
进入浏览器

有没有可能阻止用户直接访问
/login?注销
(从而仅在实际注销时显示消息)?

尝试此操作

像follow一样创建类

AppConstant

Util

登录方法

页面重定向


对每个页面重定向功能使用此方法。

另一种可能是执行转发:

http
    .logout()
        .logoutSuccessHandler(
            (request, response, authentication) -> {
                request.setAttribute("loggedOut");
                request.getRequestDispatcher("/logged-out")
                       .forward(request, response);
            });

// ...

@Controller
class LoggedOutController {
    @PostMapping("/logged-out")
    public String loggedOut() {
        return "login";
    }
}

其中,
/logged out
呈现登录页面,只需使用请求属性而不是参数来显示适当的消息。

经过一些研究,我想出了使用
重定向属性
添加FlashAttribute()
在注销时将数据传递到我的登录页面

因此,我没有将
logout
设置为查询参数,而是从安全配置中删除了Spring的默认注销处理:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
            .antMatchers("/", "*.css").permitAll()
            .antMatchers("/myendpoint").authenticated()
            .and()
            .formLogin().loginPage("/login").permitAll()
            .and()
            .logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/login?logout")
                .permitAll();
}
http
    .authorizeRequests()
    .antMatchers("/", "*.css").permitAll()
    .antMatchers("/myendpoint").authenticated()
    .and()
    .formLogin().loginPage("/login").permitAll();
并在我的控制器中创建了以下自定义注销终结点:

@PostMapping("/logout-user")
public String logout(HttpServletRequest request, RedirectAttributes attrs) {
    new SecurityContextLogoutHandler().logout(request, null, null);

    // this attribute will be received in /login
    attrs.addFlashAttribute("logout", true);

    return "redirect:/login";
}
因此,我将注销请求发送到此端点,并检查
/login
中的
注销
属性,以便在适当时显示注销消息:

<p th:if="${logout}">Logged out successfully</p>

成功注销


它的工作似乎完美无缺。

使用寿命很短的cookie而不是查询参数……如果用户未登录,您可能希望将用户从注销页面重定向回登录页面。
http
    .logout()
        .logoutSuccessHandler(
            (request, response, authentication) -> {
                request.setAttribute("loggedOut");
                request.getRequestDispatcher("/logged-out")
                       .forward(request, response);
            });

// ...

@Controller
class LoggedOutController {
    @PostMapping("/logged-out")
    public String loggedOut() {
        return "login";
    }
}
http
    .authorizeRequests()
    .antMatchers("/", "*.css").permitAll()
    .antMatchers("/myendpoint").authenticated()
    .and()
    .formLogin().loginPage("/login").permitAll();
@PostMapping("/logout-user")
public String logout(HttpServletRequest request, RedirectAttributes attrs) {
    new SecurityContextLogoutHandler().logout(request, null, null);

    // this attribute will be received in /login
    attrs.addFlashAttribute("logout", true);

    return "redirect:/login";
}
<p th:if="${logout}">Logged out successfully</p>