繁体   English   中英

使用JSP作为视图引擎注销的Spring Boot安全性不起作用

[英]spring boot security using JSP as view engine logout does not work

嗨,我正在尝试使用JSP作为视图引擎来实现Spring Boot Web应用程序。 我将本教程作为基本教程: http : //www.mkyong.com/spring-security/spring-security-hello-world-annotation-example/我知道该教程未使用spring boot。 无论如何,我设法实现了一个基本的登录功能。 但是问题是,当我尝试注销时,它不起作用,并显示“白色标签错误页面”,并显示消息“此应用程序没有针对/ error的显式映射,因此您将其视为后备”。 并且不显示任何其他特定消息。

例如,当我尝试登录到受保护页面的“管理员”页面时。 出现登录页面,我可以成功登录。但是,当我单击注销时,会发生上述错误,并且在控制台上也未显示任何错误。 当我转到上一个URL“ / admin”时,它仍然显示它是使用前一个用户登录的。

由于它在控制台和网页上均未显示任何错误,因此我无法调试该问题。 我是Spring Web应用程序的新手,所以请向我解释问题所在以及如何解决此问题。

这是我的主要Application.java文件

@ComponentScan
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {
    private static final Logger LOGGER = LoggerFactory.getLogger(Application.class.getName());

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) {
        LOGGER.info("Starting Main Application...");
        SpringApplication.run(Application.class, args);
        LOGGER.info("Access URLs: http://127.0.0.1:8080\n");
    }

}

这是我的application.properties文件

spring.mvc.view.prefix= /WEB-INF/jsp/
spring.mvc.view.suffix= .jsp

这是我的SecurityConfig.java文件

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("admin").password("admin").roles("ADMIN");
        auth.inMemoryAuthentication().withUser("user").password("user").roles("USER");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/admin/**").access("hasRole('ADMIN')")
            .antMatchers("/user/**").access("hasRole('ADMIN') or hasRole('USER')")
            .antMatchers("/index").permitAll()
            .antMatchers("/").permitAll()
        .and()
            .formLogin()
        .and()
            .logout()
            .logoutSuccessUrl("/");
    }
}

这是我的MainController.java

@Controller
public class MainController {
    private static final Logger LOGGER = LoggerFactory.getLogger(MainController.class);

    @RequestMapping({"/index", "/"})
    public String index(Model model) {
        model.addAttribute("title", "Spring Web Application example");
        model.addAttribute("message", " This is Spring Web Application example using Spring boot, JSP");
        LOGGER.debug("Inside MainController.index() method");
        return "index";
    }

    @RequestMapping(value = "/admin**", method = RequestMethod.GET)
    public String adminPage(Model model) {
        model.addAttribute("title", "Spring Security Web Application example");
        model.addAttribute("message", "This is a protected page");
        model.addAttribute("h2", "only user with ADMIN role should be able to access this page!");
        return "admin";
    }

    @RequestMapping(value = "/user**", method = RequestMethod.GET)
    public String userPage(Model model) {
        model.addAttribute("title", "Spring Security Web Application example");
        model.addAttribute("message", "This is a protected page");
        model.addAttribute("h2", "only user with either USER or ADMIN role should be able to access this page!");
        return "user";
    }

}

我排除了jsp文件和pom文件,如果您想查看整个项目,请查看我的github存储库https://github.com/agthumoe/spring-boot-security

谢谢

文档中所述 ,默认情况下启用CSRF保护。 这意味着, /logaout仅可用于post请求。 发布请求必须包含CSRF令牌。 因此,您可以执行以下操作:

<form action="/logout" method="post">
    <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
    <input type="submit" value="Log out" />
</form>

此表格只是示例。 当然,您可以通过单击链接删除按钮并使用javascript提交。

作为另一种解决方案,可以禁用CSRF保护(就安全性而言,这不是一个好主意):

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    ....

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.
            .csrf().disable()
            .authorizeRequests()
            ....;        
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM