是否可以将jsp登录表单用作Spring安全性的自定义登录表单

时间:2019-08-28 01:04:25

标签: java spring-boot jsp spring-security

我有一个spring boot项目,所有视图都是我尝试使用spring security的jsp页面,但是我无法以jsp形式使用它,但我搜索了类似的示例,但没有找到任何相关信息。我的问题是:是否可以将jsp表单用作自定义登录页面?

1 个答案:

答案 0 :(得分:0)

您可以在Spring security中创建自己的自定义登录页面

  1. 创建自定义页面。 例如-在名为login.jsp

  2. 的视图中的auth文件夹中创建自定义登录名
  3. 在Spring安全配置文件中

     @Configuration
     public class SecurityConfiguration extends WebMvcConfigurerAdapter {
        private static final String LOGIN_URL = "/";
        private static final String LOGOUT_URL = "/logout";
    
        /**
        * Associate the specified URL with the view corresponding to that URL (the same as {@ code mvc: view - controller}).
        * <p>
        * In this class implementation, we only associate the login screen URL with the login screen view.
        * You won't be able to add model attributes through the controller registry. Instead, configure your InternalResourceViewResolver to expose beans to the JSP
        *(through request attributes, which is equivalent to what happens to model attributes). 
        *
        * @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter#addViewControllers(org.springframework.web.servlet.config.annotation.ViewControllerRegistry)
        */
        @Override
            public void addViewControllers(final ViewControllerRegistry registry) {
                registry.addViewController(LOGIN_URL).setViewName("auth/login");
        }
    
    
        @Configuration
        @Order(SecurityProperties.BASIC_AUTH_ORDER)
        protected static class FormSecurity extends WebSecurityConfigurerAdapter {
    
            @Override
            protected void configure(HttpSecurity http) throws Exception {
    
                http.formLogin()
                    .loginPage(LOGIN_URL);
    
                http.logout()
                    .logoutRequestMatcher(new AntPathRequestMatcher(LOGOUT_URL))
                    .logoutSuccessUrl(LOGIN_URL)
                    .deleteCookies("JSESSIONID")
                    .invalidateHttpSession(true)
                    .clearAuthentication(true);
                }
            }
       }